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
edward scott 10edward scott 10 

calling a trigger from an apex class

Hi,
I am trying to convert a trigger I wrote into an apex class and call the class from the trigger. I am receiving two errors when I copy the code into the class. I am hoping that someone on here can help me or give me a few pointers on what I can do to fix the problem. This is the trigger that fires correctly as a trigger.
trigger RunAssignmentRule on Lead (after update) {
    List<Lead> ls = new List<Lead>();

    for (Lead l : Trigger.new) {
    
    String oldVal = trigger.oldMap.get(l.id).Status;
    
        if (l.Status == 'Open/Requalified' && oldVal <> 'Open/Requalified') {
            ls.add(new Lead(id = l.id));
        }
    }
    
    if (ls.size() > 0) {
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule = true;
    Database.update(ls, dmo);
}

}

The trigger makes a lead run back through the lead assignment when its status his open requalified. When I add it to an apen class I get two errors. An unexpected token error on the list and an error on for because it says it expected a } instead of the for. 

Thanks for your help,
Edward

 
Venkata Shiva Koushik RachapudiVenkata Shiva Koushik Rachapudi
Please make the change to your lead trigger as following : 
trigger RunAssignmentRule on Lead (after update) {
    map<Id,Lead> LeadMapping = new map<Id,Lead>();
    for(lead l : trigger.old){
    LeadMapping.put(l.id,l);    
    }
   RunRule.RunAssignmentRule(trigger.new,LeadMapping);

}

and Trigger class to be : 
public class RunRule {
    public static void RunAssignmentRule(List<Lead>trigger1,Map<Id,Lead>trigger2){
        List<Lead> ls = new List<Lead>();

    for (Lead l : Trigger1) {
    
    String oldVal = trigger2.get(l.id).Status;
    
        if (l.Status == 'Open/Requalified' && oldVal <> 'Open/Requalified') {
            ls.add(new Lead(id = l.id));
        }
    }
    
    if (ls.size() > 0) {
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule = true;
    Database.update(ls, dmo);
}
    }
}

this should work as per your logic, and change naming convention as required.

let me know if this works :) i am also learing to develop wanted to see if i can help.
pankul guptapankul gupta
Hi Edward,

You can't use a trigger.oldMap in a after update trigger. Please include before update event also in your trigger and it should resolve the error. Please let me know if the same helps.
 
trigger RunAssignmentRule on Lead (before update, after update) {
    List<Lead> ls = new List<Lead>();

    for (Lead l : Trigger.new) {
    
    String oldVal = trigger.oldMap.get(l.id).Status;
    
        if (l.Status == 'Open/Requalified' && oldVal <> 'Open/Requalified') {
            ls.add(new Lead(id = l.id));
        }
    }
    
    if (ls.size() > 0) {
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule = true;
    Database.update(ls, dmo);
}

}
Regards,
Pankul
 
peter paul 1peter paul 1
Thanks for this example. You will get more example from Hotmail customer service (https://emailsupports.net/hotmail-support/) Trigger is related to database, it is used to delete the database.