0

Can anyone help on avoiding nested for loop in the below code:

 List<Account> leadAccountIds=[Select Id, OwnerId, Name FROM Account WHERE Name LIKE :Liktr];
    System.debug('leadAccountIds=' + leadAccountIds);

    Map<String, Id> acctNameId=new Map<String, Id>();
    Map<String, Id> acctNameOwner=new Map<String, Id>();
    if(leadAccountIds.size()==1){
          For (Account a :leadAccountIds){

                  acctNameId.put(a.name,a.Id);
                  acctNameOwner.put(a.name,a.ownerId);
                  system.debug(acctNameOwner);
                  system.debug(acctNameId);

                For (Lead l2:trigger.new){
                         l2.ownerId=a.ownerId;
                         system.debug(a.ownerId);
                 }
           }

Thanks in advance!...

0

Assuming that you want to set owners of the lead to the account you have queried. This can be done as follows.

List<Account> leadAccountIds=new List<Account>([Select Id, OwnerId, Name FROM Account WHERE Name LIKE :Liktr]);

for(Account a :leadAccountIds) {
    //do any additional your if required
}
if(leadAccountIds.size() > 0) {
    for(Lead l2 : trigger.new) {
        l2.OwnerId = leadAccountIds[0].OwnerId;
    }
}
3
  • Thanks for your reply /* l2.ownerId=leadAccountIds.get(l2.PartnerAccountId).ownerId; */ If i Use that i can't assisgn account owner to lead right – VINOD Jul 13 '18 at 10:20
  • Yes there was an issue. I've updated the answer – Ranga Jul 13 '18 at 10:38
  • Hi @Ranga, I don't Understand your answer, can you please – VINOD Jul 16 '18 at 12:07

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.