0

I am 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.

Trigger

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;
    }
}
1

There's a problem in your last for loop:

    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;
    }

You are browsing an empty list. But for your problem, here's what I suggest:

trigger LTS_Update1 on Account (after update, after insert) {
    List<String> acctSellerIds = new List<String>();
    for(Account acct : trigger.new) {
        acctSellerIds.add(acct.Account_id__c);
    }

    String value = 'Yes';
    List<Lead> listLead = [SELECT Id, Live_to_site__c FROM Lead WHERE seller_id__c IN :acctSellerIds AND Live_to_site__c != :value];
    for(Lead lead : listLead){
        lead.Live_to_site__c = value;
    }
    update listLead;
}

With that solution, you only request the leads with a Seller Id existing in an Account and with the value Live_to_site__c that must be changed.

  • You should also create a trigger for the deletion of an account. To set the value to 'No' on the matching lead. – Martin Lezer Jan 11 '17 at 13:07
  • Looks like something is not fine but I am getting the following error after saving the record and also live to site field is not updating " System.StringException: Invalid id: 2543: External entry point" a – Laxman Jan 11 '17 at 13:26
  • What's the type of your custom field "Account_Id__c"? – Martin Lezer Jan 11 '17 at 13:30
  • It's Text data type – Laxman Jan 11 '17 at 14:07
  • Ok I updated my answer. Also you should consider to change the "Account_Id__c" field type if it's supposed to be a number. – Martin Lezer Jan 11 '17 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.