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
test codetest code 

Converting a Record Into Child Record

Hi I am using Contact and Lead. In both Contact and Lead I have a custom field called TEB_ID__c. Now when I am creating a Contact record I am comparing all the TEB_ID_c of Customer with all the records Lead for similar TEB_ID__c. If TEB_ID_c of  Lead and Customer matches the Status__c field of Lead is updated to Close and that Lead record whose Status__c field has been updated will become a child record of customer for which TEB_ID_c is matching.

Now my issue is in my code I am able to update the Status__c field to Close but I am not able to get any clue how to add this updated Lead as child of Contact.

 

My code for to update Status__c field: 

 

trigger CloseLead2NEW on Contact (before update, before insert) {


List<Lead> lee=new List<Lead>();

Lead leeList;

Contact cc;

List<Lead> Updatelee=new List<Lead>();

if (Trigger.isUpdate||Trigger.isInsert)

{

    cc=Trigger.New[0];
    lee=[select id,TEB_ID__c,Citizenship_Number__c,Tax_Number__c from Lead where TEB_ID__c=:cc.TEB_ID__c OR Citizenship_Number__c=:cc.Citizenship_Number__c ];
  if(lee.size()>0)
 {

        for(Lead l:lee)
        {
       
                            l.Status='Closed';
                 
                             leeList= new Lead(id=l.id,Status=l.Status);
            Updatelee.add(leeList);
          
  
}
}
}
 

update Updatelee;

 

}

 

Help me to cretate this updated Lead as child of Contact record which has triggered the Lead record to get updated.

 

Thanks.

Starz26Starz26

You will have to convert to an after insert trigger and add the contact id to the lead contactid field.

 

And get those soql outside of the for loop :)

test codetest code

Hi I am pretty new to Apex . Can you please specify bit more about the contact id to the lead contactid fields?

Here I have created a look up field on Lead so that after closing Lead can appear under Contact. So now my problem is that via Apex I am able to close the Lead but how to get that Lead as related Item of that particular Contact.

Starz26Starz26

When updating the lead from the contact trigger, put the id of the contact in the file on lead that is related to contact

test codetest code

Sorry to say that still I am not getting. My problem is that Lead is already in the system now I am updating Customer record and as soon as its TEB_ID_c matches with the existing Lead record that particular Lead's Status field should get updated to Closed and that Lead should become the related item of that particular Customer. How to achieve this thing?

Starz26Starz26

All the info you need is within the answers to all your questions.

 

Please take a stab at it and post new code here with questions.