function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tomeka WrayTomeka Wray 

retrieving field values after insert

I have created an after insert trigger for Case object that creates a corresponding lead when it is accepted from a Salesforce to Salesforce Connection. However, when I try to retrieve the field values after the insert they are null except for the ID. Can you please take a look at my code to see what I am doing wrong.

trigger createCaseLead on Case (after insert) {
    List<Case> updatedCases = Trigger.new;
    system.debug('Number of cases =' + updatedCases.size());
    List<Lead> caseLead = new List<Lead>();
     //Loops Through all of the new Cases and Creates
    //a corresponding lead
    for (Case c : updatedCases){
        system.debug('ID = ' + c.ID);
        system.debug('LastName = ' + c.Contact_Name_Copy__c); //This is returning null
        system.debug('Company = '+ c.Org_Name__c); //This is returning null
        caseLead.add(new Lead(Company = c.Org_Name__c,
                              Street = c.Org_Street__c,
                              City = c.Org_City__c,
                              State = c.Org_State__c,
                              Country = c.Org_Country__c,
                              email = c.Copy_Contact_Email__c,
                              LastName = c.Contact_Name_Copy__c,
                              LeadSource = 'Department of Commerce Customers',
                              Case_ID__c = c.Id,
                             How_did_you_find_this_Company1__c = 'DOC - ' + c.Fee_Based_Service__c
                             ));

    }
    insert caseLead;
 
}
Best Answer chosen by Tomeka Wray
Temoc MunozTemoc Munoz
Hi Tomeka.

Please take a look at this link:
https://help.salesforce.com/HTViewHelpDoc?id=business_network_lead_inbox.htm&language=en_US
 
​From Salesforce: When you manually accept a record, it takes some time for all mapped fields on the record to be updated. If an error occurs when accepting a record, see the Connection History for details.

If this is the case, you may need to move your code to a future call.

All Answers

Naval Sharma4Naval Sharma4
Hi Tomeka,

If these are not relationship fields then they should be populated in trigger.

Thanks,
Naval
Temoc MunozTemoc Munoz
Hi Tomeka.

Are those fields relationships?

Also, I suggest you put your trigger logic in a separate class (i.e. a Trigger handler). For my answer, I'm going to modify your existing code.

Could you try the following code:
trigger createCaseLead on Case (after insert) {
     Set<Id> caseIds = new Set<Id>();
     for(Case cs : Trigger.new)
     { 
        caseIds.add(cs.Id);
     }
     // Please add the rest of your fields as needed
     List<Case> updatedCases = [Select Id, Contact_Name_Copy__c, Org_Name__c,... where id in :   caseIds];

    system.debug('Number of cases =' + updatedCases.size());
    List<Lead> caseLead = new List<Lead>();
     //Loops Through all of the new Cases and Creates
    //a corresponding lead
    for (Case c : updatedCases){
        system.debug('ID = ' + c.ID);
        system.debug('LastName = ' + c.Contact_Name_Copy__c); //This is returning null
        system.debug('Company = '+ c.Org_Name__c); //This is returning null
        caseLead.add(new Lead(Company = c.Org_Name__c,
                              Street = c.Org_Street__c,
                              City = c.Org_City__c,
                              State = c.Org_State__c,
                              Country = c.Org_Country__c,
                              email = c.Copy_Contact_Email__c,
                              LastName = c.Contact_Name_Copy__c,
                              LeadSource = 'Department of Commerce Customers',
                              Case_ID__c = c.Id,
                             How_did_you_find_this_Company1__c = 'DOC - ' + c.Fee_Based_Service__c
                             ));

    }
    insert caseLead;
 
}

 
Mahesh DMahesh D
Hi Tomeka,

Please find the below code:

Here I considered:

(1) Optimized the code.
trigger createCaseLead on Case (after insert) {
     
     // Please add the rest of your fields as needed
     List<Case> updatedCases = [Select Id, Contact_Name_Copy__c, Org_Name__c, Org_Street__c, Org_City__c, Org_State__c, Org_Country__c, 
									Copy_Contact_Email__c, Fee_Based_Service__c from Case where id IN : Trigger.newMap.keySet()];

    system.debug('Number of cases =' + updatedCases.size());
    List<Lead> caseLead = new List<Lead>();
     //Loops Through all of the new Cases and Creates
    //a corresponding lead
    for (Case c : updatedCases){
        system.debug('ID = ' + c.ID);
        system.debug('LastName = ' + c.Contact_Name_Copy__c); //This is returning null
        system.debug('Company = '+ c.Org_Name__c); //This is returning null
        caseLead.add(new Lead(Company = c.Org_Name__c,
                              Street = c.Org_Street__c,
                              City = c.Org_City__c,
                              State = c.Org_State__c,
                              Country = c.Org_Country__c,
                              email = c.Copy_Contact_Email__c,
                              LastName = c.Contact_Name_Copy__c,
                              LeadSource = 'Department of Commerce Customers',
                              Case_ID__c = c.Id,
                             How_did_you_find_this_Company1__c = 'DOC - ' + c.Fee_Based_Service__c
                             ));

    }
    insert caseLead; 
}
Please do let me know if it helps you.

Regards,
Mahesh

 
Tomeka WrayTomeka Wray
Thank you both Mahesh and Temoc. I tried both of your suggestions with no success. If I create a case from within the Sandbox it works perfectly. However, when I receive a case from a Salesforce to Salesforce connection by accepting the record, it fails. It's like the records that are accepted from a connection aren't really initialized yet. However, if I go to the developer console, and write a SOQL statement the fields have the values. It has to be something with the Saleseforce to Salesforce record insert.
Temoc MunozTemoc Munoz
Hi Tomeka.

Please take a look at this link:
https://help.salesforce.com/HTViewHelpDoc?id=business_network_lead_inbox.htm&language=en_US
 
​From Salesforce: When you manually accept a record, it takes some time for all mapped fields on the record to be updated. If an error occurs when accepting a record, see the Connection History for details.

If this is the case, you may need to move your code to a future call.
This was selected as the best answer
Tomeka WrayTomeka Wray
Temoc,
Thank you for this. This delay is what was causing the issue, even with the @future call. The work around that I am going to use is to schedule a job that runs once a day to create the leads.
Temoc MunozTemoc Munoz
Good catch Tomeka!