• Steve Thurston
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 13
    Replies
I am trying to convert the contents of a Long Text Area into a String to include in an HTML Email.  The line of code I am using is:

emailHTML = emailHTML.replace('%%Opportunity.Dispatch_Notes__c%%', Opportunity.Dispatch_Notes__c);

emailHTML is the full HTML content of the email.  '%%Opportunity.Dispatch_Notes__c%%' is the tag in the HTML that I am trying to replace with the Opportunity's 'Dispatch_Notes__c' field.  That field's data type is Long Text Area.

When I try to save the Apex class in IDE, I get the error message on that line:

"Save error: Method does not exist or incorrect signature: [String].replace(String, Schema.SObjectField)"

I have any number of calls to .replace() on that 'emailHTML' string, so the method presumably isn't the problem, which means the signature is.  Apparently, it doesn't like replacing contents in a String ('emailHTML') with the contents of a Long Text Area field.

How do you include Long Text Area field content in HTML?  Is there some way to cast the Long Text Area type into a String type?
Ok, this is weird.  I have a trigger that has been working happily for a few months now.  Today, I went to add some functionality to it in Sandbox, and am getting a 'List index out of bounds' error where (a) it previously worked in Sandbox, and (b) it currently works in Production.  I get this error when trying to create a new Lead.

I threw a bunch of system.debug statements around it to try and identify the problem, and blast it, I can see what line it goes wrong on, but don't know why!  Basically, I am getting a Lead ID from trigger.new and using it in a SOQL statement to retrieve the relevant fields.  This is an 'after insert' trigger where I am updating some fields.  I am successfully retrieving the list of Lead ID's from the trigger.new 'for' loop.  When I check for the size of this list outside of the for loop, it says it has a size of 1, which is correct.  I can show that ID in the debug log.

But then, when I make my SOQL query using this ID list, I get a return set of 0 size!

This makes no sense.  It has been working fine, and I can see that the list I get from trigger.new is NOT zero.  But for some reason the SOQL query doesn't return anything.

Here are the relevant codes of line and results:
-----------------------------------------------
ERROR RECEIVED:
Error: Invalid Data.
...execution of AfterInsert caused by: System.ListException: List index out of bounds: 0... (Points to the last line of code given below)

RELEVANT CODE:

List<Lead> submittedLeadList = new List<Lead>();
List<Id> theLeadID = new List<Id>();

system.debug('Entering the FOR loop to read trigger.new Lead');

for (Lead leadList : trigger.new) 
     {
        theLeadID.add(leadList.Id); 

        system.debug('Lead ID:  ' + leadList.Id);
        system.debug('Lead Name:  ' + leadList.FirstName + ' ' + leadList.LastName);
     }

system.debug('Exiting FOR loop');
system.debug('Lead ID after exiting loop:  ' + theLeadID[0]);
system.debug('Number of Leads retrieved from trigger.new:  ' + theLeadID.size());

submittedLeadList = [SELECT Id, FirstName, LastName, Company, Requested_Quote_Types__c, State, New_Lead__c FROM Lead WHERE Id IN : theLeadID LIMIT 1];

system.debug('Lead List Size:  ' + submittedLeadList.size());

system.debug('SOQL Query for submittedLeadList, for Lead:  ' + submittedLeadList[0].FirstName + ' ' + submittedLeadList[0].LastName);

DEBUG RESULTS:
USER_DEBUG|[32]|DEBUG|Entering the FOR loop to read trigger.new Lead
USER_DEBUG|[38]|DEBUG|Lead ID:  00Q63000003cPewEAE
USER_DEBUG|[39]|DEBUG|Lead Name:  Test Test1
USER_DEBUG|[42]|DEBUG|Exiting FOR loop
USER_DEBUG|[43]|DEBUG|Lead ID after exiting loop:  00Q63000003cPewEAE
USER_DEBUG|[44]|DEBUG|Number of Leads retrieved from trigger.new:  1
SOQL_EXECUTE_BEGIN|[46]|Aggregations:0|SELECT Id, FirstName, LastName, Company, Requested_Quote_Types__c, State, New_Lead__c FROM Lead WHERE Id IN :tmpVar1 LIMIT 1
SOQL_EXECUTE_END|[46]|Rows:0
USER_DEBUG|[47]|DEBUG|Lead List Size:  0
FATAL_ERROR|System.ListException: List index out of bounds: 0

This is an "after insert" trigger, so the record has to exist, and its ID has to be in trigger.new.  So how can I pass a valid ID to the SOQL statement and yet not get a result?  And how is it that this same code has worked in Sandbox before, and is currently working in Production?
I'm experiencing an unexpected error in my test class for an 'after insert' trigger.  First, the trigger works just fine in Sandbox.  So the issue has something to do with how I'm setting up my test class.  When I run the test on the test class in the Developer Console, I get the error:

-------------------------
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, leadsRoundRobin: execution of AfterInsert

caused by: System.ListException: List index out of bounds: 0

Trigger.leadsRoundRobin: line 34, column 1: []
-------------------------

If I look at the trigger leadsRoundRobin on line 34, I see that it's trying to assign the Trigger.New Lead to a variable inside the trigger:
(This is a simplified version of my trigger, including only the relevant bits)

-------------------------
trigger leadsRoundRobin on Lead (after insert) {

List<Id> triggerIds= new List<Id>();
Lead submittedLead = new Lead();

for (Lead triggerLead: trigger.new) 
        {
            triggerIds.add(triggerLead.Id);
        }

    // Get Submitted Lead Record
    submittedLeadList = [SELECT Id, FirstName, LastName, Company, Requested_Quote_Types__c, State, New_Lead__c FROM Lead WHERE Id IN : triggerIds];
    submittedLead = submittedLeadList[0];    <-- THIS IS LINE 34

etc....
-------------------------
Note that all this is doing, essentially, is assigning the Trigger.New Lead to 'submittedLead'.  It seems to me that the only way I could get the error 'List index out of bounds: 0' is if there IS no Trigger.New object!  That makes no sense.

Again, this is working perfectly fine running it in the Sandbox.  It's only trying to trigger it from the test class that gives this error.  The relevant bits of the Test class (I'm using 'SeeAllData=true' because my trigger references a Custom Setting):

-------------------------

@isTest(SeeAllData=true)
public class test_RoundRobin 
     { 
        // Add a Lead - Auto
            Lead testLead1 = new Lead();
                testLead1.Status = 'Open';
                testLead1.Company = 'Test Company 1';
                testLead1.LastName = 'Lead';
                testLead1.FirstName = 'Test1';
                if(contactRecordTypeMap.containsKey('Customer')) 
                    {
                        testLead1.RecordTypeId = leadRecordTypeMap.get('Customer').getRecordTypeId();
                    }
                testLead1.Phone = '1112223333';
                testLead1.Email = 'test@test.com';
                testLead1.Preferred_Contact_Method__c = 'Phone';
                testLead1.State = 'CA';
                testLead1.Requested_Quote_Types__c = 'Auto,';
                testLead1.New_Lead__c = FALSE;
            insert testLead1;   

            etc....
-------------------------

I can't figure it out.  It seems like the test class's 'insert testLead1' statement is firing the trigger, but not passing the Lead!

Any idea what I'm doing wrong?  I have a sneaky suspicion it has to do with 'SeeAllData=true', which I've never used before, but I just don't know.

Thanks!
I have the scenario where I am trying to convert a Lead but can't because there are pending time-based workflows.

The standard solution I come across is to create a custom checkbox field that, when it changes, causes the workflow criteria to no longer be true, and the pending workflow action quietly deletes itself.  It is then recommended that you either manually change this field value or override the conversion button to do this for you.

However, I have a complication that is preventing me from doing this.  The conversion is being triggered by a third party API call, not by a person.  So I need to have Apex change the value of this field.  And I can't get that to work.

I have tried a "before update" on the Lead, but that doesn't seem to do anything.  Investigation seems to indicate that the reason is that, before conversion, "isConverted" hasn't been set to TRUE, so I can't restrict my flag change to only conversions.

Paradoxically, I'm not sure I can use an "after update" either.  In that case, I am getting the error that I can't modify a converted Lead!  I know it is supposed to be possible to update converted Lead fields now, but I only have one User (I'm actually trying to do this in a Sandbox), and I can't modify my Profile with the necessary permission.  In any event, I'm not sure that would work either.  I think the workflow should fire after the apex code updates the field, but I'm not sure.  (And I can't test until I get the permissions sorted out.)

Surely someone has had to deal with this situation before?  To recap:

I have Leads that are getting converted by an API call.  The conversions are failing because there are pending time based workflows.  I need to use Apex to delete these time based workflows.  It must be Apex:  it cannot be done manually or via a Visualforce button.

How can this be done?

Thanks!