function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mb_mb_ 

APEX Bulk Lead Update

Hey guys,

I am kinda new to APEX triggers and I try to solve the following challange:

I built a trigger which updates 2 fields (integer field and picklist field) depending on 2 other picklist fields. Behind those updated fields are some values like:
if(ScoreAbs <= 21){
                a.ScoreTotal__c = 'Bronze';
            }else if(ScoreAbs > 21 && ScoreAbs <= 28){
                a.ScoreTotal__c = 'Silver';
            }else if(ScoreAbs > 28 && ScoreAbs <= 33){
                a.ScoreTotal__c = 'Gold';
            }else if(ScoreAbs > 33){
                a.ScoreTotal__c = 'Platinum';
            }
"ScoreAbs" is calculated via another method. That trigger works fine and there are no errors in it. Now I need to change values within the above code, e.g.  values like 21, 33, 28 etc...
After I changed those values in the first trigger I want to update all leads (something about 2000) with another trigger. Best way would be to press a button within the lead record view.

Do i need to save all leads to a list via a query first to update them after via "update leadList"? Or what would be the best way to get this problem solved?

Thanks!
Best
 
prabhakaran Chockalingam 5prabhakaran Chockalingam 5
Please implement the logic using a single trigger. This is possible by creating two Apex classes to handle the trigger logic separatley by calling the class methods from the trigger.
Stroing them in a list and doing a bulk update also works fine. 
But implementing the logic to update in a Apex class, gives us flexibility to control the order of logic execution. First the scoreAbs logic and then another batch apex class which will update the Lead.

Also, if there's a possibility that the trigger update will call this trigger again, it will result in a loop. So you can use static variable to control the trigger re-entry.

Let's wait to hear it from the experts too..

Thanks


 
mb_mb_
Those two triggers sit in seperate files already. Within the first trigger there are just some methods and calculations, values come from an apex class (values are static). As said that trigger works fine.

What trigger does not work is that one which just updates all leads once... manual process would be to just press edit and save again, so that the trigger fires, but for over 2500 leads it would take a time so do that...
prabhakaran Chockalingam 5prabhakaran Chockalingam 5
Could you post the trigger code using which you are updating the lead. 
mb_mb_
trigger UpdateLeadTrigger on Lead (before update, before insert) {
    
    List<Lead> leadList = [SELECT Id, updated__c FROM Lead IN Trigger.New];
    
    for(Lead l :leadList){
        l.updated__c = true;
    }
    update leadList;
}
sure, the first error is "IN Trigger.New", but i do not know where to place it otherwise... The second error occurs when the trigger is fired:

"first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 00Q3E000002IpDX) is currently in trigger UpdateLeadTrigger, therefore it cannot recursively update itself: []: Trigger.UpdateLeadTrigger: line 8, column 1"
 
prabhakaran Chockalingam 5prabhakaran Chockalingam 5
As this is a before trigger, a explicit DML statement is not required. I guess it is because of the update statement. Please remove the update statement.
mb_mb_
trigger UpdateLeadTrigger on Lead (before update, before insert) {
    
    List<Lead> leadList = [SELECT Id, updated__c FROM Lead];
    
    for(Lead l :leadList){
    }
    
}
Well, i tried this now remove the first error... now it works without errors, but it doesnt update every lead, just the one I updated myself to fire the trigger...
 
prabhakaran Chockalingam 5prabhakaran Chockalingam 5
trigger UpdateLeadTrigger on Lead (before update, before insert) {
    
    List<Lead> leadList = [SELECT Id, updated__c FROM Lead IN Trigger.New];
    
    for(Lead l :leadList){
        l.updated__c = true;
    }
}

Sorry If I am not clear. I meant to remove the last update statement. The above code should defnitley work.
mb_mb_
Mhh... It says: expecting right square bracket, found 'IN'. line 3
prabhakaran Chockalingam 5prabhakaran Chockalingam 5
change this query from SELECT Id, updated__c FROM Lead IN Trigger.New to
SELECT Id, updated__c FROM Lead where Id IN Trigger.New
mb_mb_
trigger UpdateLeadTrigger on Lead (before update, before insert) {
    
    List<Lead> leadList = [SELECT Id, updated__c FROM Lead WHERE Id IN :Trigger.New];
    
    for(Lead l :leadList){
        l.updated__c = true;
    }
    
}
without any errors now, but not updating updated__c field
 
mb_mb_
Well, found a workaround. Just export all leads (id field) via dataloader and update all leads with the same file without changing anything...