0

I am having a problem to update Lead status field.

I've implemented my own method which is supposed to set the Lead status field to particular value but after the method is invoked in the before insert and the before update trigger the Lead status value is not updated in the Lead table.

Here is how the method is implemented:

private void setLeadStatus(Account accountObj) {
       String companyName = accountObj.name;
       String accountObjWebs = accountObj.Website;
       List<Lead> leadList = [SELECT company, website, status__c, IsConverted FROM Lead where company =:companyName AND website =:accountObjWebs];
        for(Lead ld :leadList) {
           boolean isLeadConverted = ld.IsConverted;
           System.debug('isLeadConverted ' + isLeadConverted);
           if (!isLeadConverted) {
               ld.Status__c = 'Existing Deal';
               update ld;
               System.debug('ld ' + ld);               
           }    
       }
}

In debug log I see that the status is set:

DEBUG|ld Lead:{Company=emerchantpay, Website=www.emp.com, IsConverted=false, Id=00Q560000019glrEAA, Status__c=Existing Deal}

The problem is that I do not see the Status__c to be updated in Lead table. Could you please advise how to update the Status__c in the Lead table?

Regards,

Dilyan

1
  • You should really move your update outside the for loop. It won't fix the issue but the code will be much better for it.
    – Adrian Larson
    Nov 4, 2015 at 16:38

1 Answer 1

0

You need to consider two things here:
1. setLeadStatus is a private method. Hence it wont be visible while being invoked from the trigger.
2. As @adrian mentioned, its better to put the UPDATE outside the for loop. Its not a good practice to a perform DML operation during each iteration. You should put it into a list finally update the whole List.

public with sharing class updateLeadStatus{
    public void setLeadStatus(Account accountObj) {
           String companyName = accountObj.name;
           String accountObjWebs = accountObj.Website;
           List (Lead) leadList = [SELECT company, website, status__c, IsConverted FROM Lead where company =:companyName AND website =:accountObjWebs];
             List (Lead) updateLeadList = new List(); 
            for(Lead ld :leadList) {
               boolean isLeadConverted = ld.IsConverted;
               System.debug('isLeadConverted ' + isLeadConverted);
               if (!isLeadConverted) {
                   ld.Status__c = 'Existing Deal';
                   //update ld;
                   updateLeadList.add(ld);
                   System.debug('ld ' + ld);               
               }    
           }

           UPDATE updateLeadList;
    }
}

NOTE: in the above code replace (Lead) with Thanks,
Aditya

1
  • Hi, Thank you for your comment. I followed your suggestions as well as made an update outside the for loop but still it didn't help me to update the Status__c in the Lead table? I even tried to invoke the setLeadStatus() method in the after insert trigger but that didn't work either. Could you please advise what else I could try to make an update in the Lead table?
    – Dylan D
    Nov 5, 2015 at 8:24

You must log in to answer this question.

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