0

I'm writing a Queueable Apex, that will run and compare search the Lead last name to the Account name if Lead lastname == Account name && Account phone field is null || blank then update the phone number field on the Account obj off of the lead phone field value.

This code is not copying the phone number value. Also I'm not if I'm passing the phone number from map collection to account list correctly?

Here is my code:

public class compAcctnLeadPhone implements Queueable{
public void execute(QueueableContext context){
    List<Account> lisAccts = [select Id, Name, phone from Account where phone =''];
    set<String> acctNameSet = new set<String>();
    for(Account acc : lisAccts){
        acctNameSet.add(acc.Name);  // adding the filtred accounts by phone = null into a SET type String
    }

    Map<String, String> ledMap = new Map<String, String>();
    List <Lead> lisLeads = [select Id, lastname, phone from Lead where lastname in:acctNameSet];
    for(Lead led : lisLeads){
        //ledMap.put(led.lastname + '' + led.phone, led.Id);   // this map has lastnames match the last name in Acct obj
        ledMap.put(led.lastname, led.Phone);
    }

    //compare the Account Name to lead lastname looping through the accounts
    for(Account act : lisAccts){
        if(ledMap.containsKey(act.Name)){
            system.debug('lead Id ===> ' + ledMap.get(act.Name));
            act.phone = ledMap.get(act.Name).phone;
            lisAccts.add(act);
        }
        update lisAccts;
        /*else{
            act.addError('there is no match !!!');
        }*/

    }
}

}

4
  • 3
    There is no question here. What do you expect us to do with this code? Is there anything not working, or are you simply looking for a critique? Please edit your question to include more information so we can help you. – sfdcfox Dec 7 '19 at 3:51
  • The phone number not being copied from lead to account – Templar Dec 7 '19 at 7:01
  • 2
    a queueable without a constructor w/ args is very unusual (one would imagine a collection of leadIds). Otherwise, your logic above better belongs in a batch class. – cropredy Dec 7 '19 at 7:24
  • shouldnt this be batch? – Pranay Jaiswal Dec 7 '19 at 13:03
0

You really should not be doing this update in the same list:

for(Account act : lisAccts){
    if(ledMap.containsKey(act.Name)){
        system.debug('lead Id ===> ' + ledMap.get(act.Name));
        act.phone = ledMap.get(act.Name).phone;
        lisAccts.add(act);
    }
    update lisAccts;

Those last two lines there are the a problem, if not the problem. You generally do not want to mutate a list that you're iterating over, one, and two, you're just adding changed Accounts back into a list that already contains the original version you queried. Then you're update-ing the entire list, every time through the loop!

I don't know if this is your core problem or not - I suspect your queries just aren't returning anything - but it's certainly going to cause rather inscrutable behavior later on.

Instead, accumulate a separate List<Account> accountsToUpdate, and execute update DML exactly once, outside the loop.

As cropredy noted in a comment, this should almost certainly be a batch process, not a Queueable. Indeed, it's possible that you observe no effect because the Queueable is throwing an exception, due to limits or to the logical errors noted above.

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.