0

my goal is to update custom field seller_id__c(if Account_id__c is 1122 then seller_id__c should update with 1122) in Lead object with custom field Account_id__c in Account whenever there is a new record/update in Account happens. My trigger behavior is little bit awkward can you please suggest changes where I am doing mistakes.

Note:Account__c is a lookup relationship from lead to account

Trigger Lead_Update on Account (after insert, after update) {

    set<Id> acctIds = new set<Id>();
    map<string, Account> mapAccount = new map<string, Account>();
    list<Lead> listLead = new list<Lead>();

    for(Account acct : trigger.new) {
        acctIds.add(acct.Id);
        mapAccount.put(acct.Id, acct);
    }

    listLead = [SELECT Name, Seller_id__c, Status, Account__c FROM Lead WHERE Account__c IN : acctIds];

    if(listLead.size() > 0) {
        for(Lead ldu : listLead) {
            ldu.Seller_id__c = mapAccount.get(ldu.Account__c).Account_ID__c;

        }
        update listLead;
        system.debug(listLead);
    }
}
3
  • can you explain what mean little bit awkward?
    – kurunve
    Commented Jan 18, 2017 at 8:23
  • why cant you use a formula field here if your only goal is to update the seller__c field with a value from the field in account?(If you want the user not to edit the field and the seller__c field always has the value from the field Account_ID__c in account)
    – Preya
    Commented Jan 18, 2017 at 8:29
  • It's updating seller_id__c only when we link both ways account to lead and lead to account. But my requirement is when we insert/update account then only it should update seller_id__c in lead.
    – Laxman
    Commented Jan 18, 2017 at 8:31

2 Answers 2

1

Try this code. Remove unwanted code in your trigger. Keep it simple.

trigger Lead_Update on Account (after insert, after update) {
    list<Lead> listLead = new list<Lead>();

    for(Lead ldu : [SELECT Id, Name, Seller_id__c, Status, Account__c FROM Lead WHERE Account__c IN : Trigger.newMap.keySet()]) {
        ldu.Seller_id__c = Trigger.newMap.get(ldu.Account__c).Account_ID__c;
        listLead.add(ldu);
    }
    if(!listLead.isEmpty()){
        update listLead;
    }
}
2
  • It's working Reshma we forget to add the lead records got the mistake.
    – Laxman
    Commented Jan 18, 2017 at 10:11
  • @Laxman glad to help you. Please mark the answer so that it would be helpful to many others.
    – Reshma
    Commented Jan 18, 2017 at 10:14
0

Try Below piece of code

Trigger.newMap context variable is available in after insert and after update so you don't need to have any for loop for creating the map.

trigger Lead_Update on Account (after insert, after update) {
   if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) {
    list<Lead> updateLeadList = new list<Lead>();

    for(Lead ldu : [SELECT Name, Seller_id__c, Status, Account__c FROM Lead WHERE Account__c IN : Trigger.newMap.keySet()]) {
        ldu.Seller_id__c = Trigger.newMap.get(ldu.Account__c).Account_ID__c;
        updateLeadList.add(ldu);
    }
    if(!updateLeadList.isEmpty())
        update updateLeadList;
  }
}
8
  • 1
    Try with RCS provided piece of code
    – KS Kumaar
    Commented Jan 18, 2017 at 9:33
  • Not working RCS code
    – Laxman
    Commented Jan 18, 2017 at 9:41
  • could you let us know what exactly is not working?
    – RCS
    Commented Jan 18, 2017 at 9:42
  • it's not updating seller_id__c field in leads
    – Laxman
    Commented Jan 18, 2017 at 9:53
  • Have you checked the debug log why its not updating the field? Is there any specific error message that you encountered? Please check FLS as well.
    – RCS
    Commented Jan 18, 2017 at 9:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .