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
Supriyo Ghosh 9Supriyo Ghosh 9 

trigger for three object

Hi,

account obj is parent of Lead and opportunity.I want to copy Lead status from lead obj to opportunity obj with matching of account id.Please help.
Supriyo Ghosh 9Supriyo Ghosh 9
I have tried this below code.

trigger CopyleadvaluetoOpp on Opportunity (after insert) {
   List<Account> accounts = new List<Account>(); 
   for (Opportunity Opp: Trigger.new)
    {
        If(Opp.Lead_Exception__c == null) 
        {
            List<Lead> Ld = [SELECT Id,Status FROM Lead];
            List<Account> Acc = [Select Id FROM Account];
                                         
            for(Lead L: Ld)
        {
          If(Opp.Account.Id == Ld.Account.Id)
          {
               Opp.Id = L.Id;
               Opp.Lead_Exception__c = L.Status;
               }
            }
        }
    } 


But it is still not working.Please help me out
ANUTEJANUTEJ (Salesforce Developers) 
Hi Supriyo,

Can you try the below trigger and if the only field you are updating is a status field then I would suggest you to use before trigger, using after trigger you would be using a soql query update statement unnecessarily, to check the difference between after and before triggers then you can check these below links:

>> https://salesforce.stackexchange.com/questions/96263/what-is-the-exact-difference-between-before-update-and-after-update-trigger

>> https://www.sfdc99.com/2014/01/25/use-vs-triggers/

>> https://www.brcline.com/blog/whats-the-difference-between-before-and-after-triggers#:~:text=Before%20triggers%20execute%20before%20the%20data%20has%20been%20committed%20into%20the%20database.&text=After%20triggers%20execute%20after%20the,the%20case%20of%20an%20insert.

Please do note that this works for one lead and in case if you want to concatinate the status for multiple leads then you will have to update the soql and logic as per the scenario.
 
trigger CopyleadvaluetoOpp on Opportunity (before insert) {

if(trigger.isbefore && trigger.isinsert)
{
    list<id> Id_list = new list<id>();
    List<Lead> Lead_list = new List<Lead>();
    map<id,String> acountid_leadstatus = new map<id,String>();
    

    for(Opportunity opp : trigger.new)
    {
    if(Opp.Lead_Exception__c == null)
    {
    Id_list.add(opp.AccountId);
    }
    }
    Lead_list = [select AccountId, Status from Lead where AccountId in:Id_list LIMIT 1];

    for(Lead l: Lead_list)
    {
    acountid_leadstatus.put(l.AccountId,l.Status);
    }

    for(Opportunity o : trigger.new)
    {
    o.Lead_Exception__c = acountid_leadstatus.get(o.AccountId);
    }
    }
} 
 
I would suggest you try checking the https://trailhead.salesforce.com/content/learn/modules/apex_triggers trailhead link that could help you get started with the trigger.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Supriyo Ghosh 9Supriyo Ghosh 9
It is not working.I have done some small modification.

trigger CopyleadvaluetoOpp on Opportunity (before insert) {
   
   Set <String> OppotunityCustId = new Set <String> ();
   
   for(Opportunity Opp : trigger.New)
   {
    OppotunityCustId.add(Opp.Customer_Id__c);
   }

   //Map <String, Lead> matchingLeadMap = new Map <String, Lead> ();
   
   List <Lead> newLead = new List<Lead> ([Select Id,Status,Customer_Code__c,Lead_No__c From Lead Where Customer_Code__c IN :OppotunityCustId AND Status != 'Converted']);
   /*{
    matchingLeadMap.put(Ld.Status, Ld);
   }*/
   
   List <Opportunity> opportunityToUpdate = new List <Opportunity> ();
   
   for(Opportunity Opp : trigger.New)
{
    if (newLead.size()>0)
    {
      
        // we found a mathing one
        Opp.Lead_Exception__c = true;
        
        
        //Opp.adderror('Lead already exists for this account. Please convert lead to create account.');

        // add it to a separate list and update it
        opportunityToUpdate.add(Opp);
    }

//update OpportunityToUpdate;
}

But if want to update lead no into opportunity filed then what I need to do.Please help.