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
elmaschingondeguateelmaschingondeguate 

Update current record with ID of the new record created.

Hey guys, any help on the following will be greatly appreciated.

 

I have a trigger that creates a case when a lead has been set to certain status, this works fine but I also want to update a field on the current lead(the one who caused the trigger to fire) with the Case Number of the case created. How can I achieve this?

 

This is the trigger:

 

 

trigger CasofromLead on Lead (Before update) { 
   
List<case> cs= new List<case>();

    for (Lead updatedLead: Trigger.New)
    
         if (updatedLead.Status == 'Sent to Support'){
                 cs.add(new Case(
                     subject = 'Support request from Sales ',
                     Origin = 'Phone',
                     status = 'New',
                     description='Company: '+ updatedLead.Company +'Name: '
                     )
                 );   
        
         }
      
   insert cs;
        
  

 }

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
trigger CasofromLead on Lead (Before update) { 
  // Map leads to cases
  map<id,case> cases = new map<id,case>();
  // for all leads with the right status, and no case created, create
  // a new case
  for (Lead updatedLead: Trigger.New)
    if(updatedLead.Status == 'Sent to Support' &&
        updatedLead.CaseId__c==null)
      cases.put(updatedlead.id,
        new Case(
          subject='Support request from Sales ',
          Origin='Phone',
          status='New',
          description='Company: '+updatedLead.Company +'Name: '));
  // insert the cases
  insert cases.values();
  // store the case id that was created in each lead
  for(id leadid:cases.keyset())
    trigger.newmap.get(leadid).caseid__c=cases.get(leadid).id;
}

This this out.

All Answers

AmitSahuAmitSahu

You can try : 

 

trigger CasofromLead on Lead (Before update) { 
   
List<case> cs= new List<case>();

    for (Lead updatedLead: Trigger.New)
    
         if (updatedLead.Status == 'Sent to Support'){
                 cs.add(new Case(
                     subject = 'Support request from Sales ',
                     Origin = 'Phone',
                     status = 'New',
                     description='Company: '+ updatedLead.Company +'Name: '
                     )
                 );   
        
         }
      
   insert cs;
        
  Field of Lead  = cs.ID;


 }

elmaschingondeguateelmaschingondeguate

Thanks but that doesn't work, I get an error " Compile Error: Variable does not exist", if you notice, cs is a list so I don't think is as simple as that.

 

Also, I want the case number as well not just the ID. How can I query on the case number of the cases(or case) I just created and update it's value on any field on the lead(s)?

 

Thanks in advance

 


AmitSahuAmitSahu

When you are dealing with a single record, there is no need to have a list..

 

You can also get the first element of the list you just added ..

 

cs[0].id   will give you the id of the case you just created ......you can query on that id to get any field you need.

 

But also you may try if this works for you ...

 

cs[0].caseNumber

 

 

sfdcfoxsfdcfox
trigger CasofromLead on Lead (Before update) { 
  // Map leads to cases
  map<id,case> cases = new map<id,case>();
  // for all leads with the right status, and no case created, create
  // a new case
  for (Lead updatedLead: Trigger.New)
    if(updatedLead.Status == 'Sent to Support' &&
        updatedLead.CaseId__c==null)
      cases.put(updatedlead.id,
        new Case(
          subject='Support request from Sales ',
          Origin='Phone',
          status='New',
          description='Company: '+updatedLead.Company +'Name: '));
  // insert the cases
  insert cases.values();
  // store the case id that was created in each lead
  for(id leadid:cases.keyset())
    trigger.newmap.get(leadid).caseid__c=cases.get(leadid).id;
}

This this out.

This was selected as the best answer
elmaschingondeguateelmaschingondeguate

Awesome, I had time until today to test your trigger, even though I got mine working yours is better so I think I will use it. Thanks again for the help.