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
Laxmaya ChnLaxmaya Chn 

Trigger conditional field update in lead based on parent account

Hi I am new to salesforce and trying to write a trigger to update field(live to site) in Lead based on some condition.

we have a custom field in Account Account_id__c(ex:25679) which is a number and also similar field(seller_id__c-which is also a number ex:25679) in lead which is also a number. When there is a new account with some account id matches with lead seller id then a field live to site(picklist with values Yes, No) in lead should update with Yes.

Here's is my trigger, please correct me.


trigger LTS_Update1 on Account (after update) {
    set<Id> acctIds = new set<Id>();
    map<Id, Account> mapAccount = new map<Id, Account>();
      List<Lead> listLead = new List<Lead>();
    
    for(Account acct : trigger.new) {
        acctIds.add(acct.Id);
        mapAccount.put(acct.Id, acct);
    }
    
    listLead = [SELECT Id, Live_to_site__c, Account__c FROM Lead WHERE Account__c IN : acctIds];    
    
    List<Account> acc=new List<Account>();
    for(Account acct: acc)
    {
    if(acc[0].Account_ID__c == listLead[0].seller_id__c) {
        for(Lead ld : listLead) {
            ld.Live_to_site__c = 'Yes';
          }  
        }
        update listLead;
    }
}
Vaishali NagarVaishali Nagar
Hi Laxmaya,

You can try below code

trigger LTS_Update1 on Account (after update) {
  Map<id,Lead> MapLead =new Map<id,Lead>([SELECT id,seller_id__c,Live_to_site__c from lead WHERE  Account__c IN : Trigger.NewMap.KeySet()]);

    for(Account acct: Trigger.New)
    {
        for(Lead ld : MapLead .Values()) {
                    if(acc.Account_ID__c == ld.seller_id__c) 
                      ld.Live_to_site__c ='Yes';
        }
    }  
               update MapLead.values();
 }


Thanks
Vaishali Nagar
Deepak Pandey 13Deepak Pandey 13
trigger LTS_Update1 on Account (after update) {
    set<Id> acctIds = new set<Id>();
      List<Lead> listLead = new List<Lead>();
    
    for(Account acct : trigger.new) {
        if(acct.Account_ID__c != null)
            {
            acctIds.add(acct.Id);
            }
    }
    
    listLead = [SELECT Id, Live_to_site__c, Account__c FROM Lead WHERE Account__c IN : acctIds ];    
    list<lead> lstld = new list<lead>();
    List<Account> acc=new List<Account>();
  for(Account acc: trigger.new)
  {
    for(Lead acct: listLead)
    {
    if(acc.Account_ID__c == acct.seller_id__c) 
            acct.Live_to_site__c = 'Yes';
            lstld.add(acct);
        }
    }
       if(lstld.size()>0)
       {
        update lstld;
       } 
    }
}
ajay rawat 14ajay rawat 14
Hi Laxmaya,

Vaishali is right but if you want to save inner for loop then we can use below code

trigger LTS_Update1 on Account (after update) {
  Map<id,Lead> MapLead =new Map<id,Lead>([SELECT id,seller_id__c,Live_to_site__c,Account__c from lead WHERE  Account__c IN : Trigger.NewMap.KeySet()]);
  Map<id,Account> AccountMap =Trigger.NewMap;
       for(Lead ld : MapLead.Values()) {
           Account AccountObj = AccountMap.get(ld.Account__c);
                    if(AccountObj.Account_ID__c == ld.seller_id__c) 
                      ld.Live_to_site__c ='Yes';
        }       
         update MapLead.values();
 }


Thanks
Ajay Rawat