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
miku1051miku1051 

trigger help.....how to get contact id....

the scenario looks like this...when i create a lead record ,a contact record is inserted based on the lead field values(some mapping is there like name.address..etc).the problem is i want a get contact id of the record which is inserted when lead is created..how do i get that...there is a lookup field on 3rd  object where i want the contact id.  please help....

 

trigger test1 on Lead (after insert)
{
    
     Set<ID> ls = new Set<ID> ();
     for(Lead l:Trigger.new)
    {
        ls.add(l.id);  
    }
    
    List<Lead> lis=[Select Id,Name, Status,Email,Company,fax,from Lead where id in:ls];
       List<Contact> con=[Select Id,Name ,Email_2__c,fax from Contact ];
       List<Contact> lstContact = new List<Contact>();

          
       
   
       for(Lead ld:lis)
       {
           for(Contact cn:con)
           {
              
                   
                   Contact objC= new Contact();    
                objC.FirstName=ld.FirstName;
                objC.LastName=ld.LastName;
                lstContact.add(objC);
 
           }    
               customobj.lookupfield=...?????(here i want the contact id)
       }
           
           if(lstContact.size() > 0 )
        insert lstContact;
      
                   
}

Best Answer chosen by Admin (Salesforce Developers) 
dmchengdmcheng

You have to insert the contacts first before you can get the IDs.  You are trying to get the IDs before you insert.

All Answers

AmitSahuAmitSahu

You can easily achieve this by creating another trigger on contact insert. This way you will get the id easily.

miku1051miku1051

thanks for rplying...

 

i know ican do that....but i want the the contact id in this trigger only....may be u have not undertood my question....pl help...

AmitSahuAmitSahu

as long as you have not inserted the contact record how you will get the id ? Probably i am not getting what you require...

miku1051miku1051

Thanks for replying ...pl bear i am new to apex....

 

when i create a lead record....a contact record is created...there are field mapping like name of lead to name of contact etc.....

for eg i create a lead of name :sam

a contact record is inserted automaticaly of name :sam....i want id of this record...

 

i want to get contact id of the record which is inserted when lead record  is created...

and i want to pass that id in some field.....

dmchengdmcheng

1.  Is there a reason why you are not just converting the Lead to a Contact?

 

2.  Once you insert a list of objects, each object in the list will have its ID, so you can reference the IDs like this:

conList[0].Id

conList[1].Id

 

etc

 

miku1051miku1051

 

trigger test1 on Lead (after insert)
{
    
     Set<ID> ls = new Set<ID> ();
     for(Lead l:Trigger.new)
    {
        ls.add(l.id);  
    }
    
    List<Lead> lis=[Select Id,Name, Status,Email,Company,fax,from Lead where id in:ls];
       List<Contact> con=[Select Id,Name ,Email_2__c,fax from Contact ];
       List<Contact> lstContact = new List<Contact>();

          
       
   
       for(Lead ld:lis)
       {
           for(Contact cn:con)
           {
              
                   
                   Contact objC= new Contact();    
                objC.FirstName=ld.FirstName;
                objC.LastName=ld.LastName;
                lstContact.add(objC);
 
           }    
               customobj.lookupfield=objC.Id;//Here i am getting nothing ie id is null here...can u pl help
       }
           
           if(lstContact.size() > 0 )
        insert lstContact;
      
                   
}

 

i am getting nothing ie id is null here...can u pl help..i want to get the recently created id there..did you understand what i am asking for...thanks for replying....:)

dmchengdmcheng

You have to insert the contacts first before you can get the IDs.  You are trying to get the IDs before you insert.

This was selected as the best answer
miku1051miku1051

thanks i got it...i didnt see that.... thanks for your help....:) but can you tell me how do i change t he code as insert(Dml Operation) should be outside the for loop....& if i take this outside    customobj.lookupfield=objC.Id;...     then my condition changes..i am new to apex..pl bear with me....

miku1051miku1051

thanks for helping me... ..it works...:):)

miku1051miku1051

thanks..it works  for me...:):)