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
RstrunkRstrunk 

triggers and assignment rules

Good morning, 

 

      So my question may be very basic in nature for a seasoned developer but I am not quite there yet.

 

I have a Case trigger that sends an sms page to the new owner of the case.  Looking at the order of execution, assignment rules will fire after a trigger so, to me, that would mean that the sms page would get sent to the wrong person in most cases.  Below is a shorthand flow of events as I see them:

 

 

New case is created

 

Triggers fire and send an sms page to the owner of the case(which will be the person who just created it)

assignemnt rules fire and change the owner to the intended owner.  

 

Case is commited to the DB

 

 

the new owner has not recieved the SMS

 

 

Any insight would be greatly appreciated.  

 

 

Thanks, 

 

 

sfdcfoxsfdcfox

What you can do in this case is have your trigger call a @future method to send the SMS. This means that the assignment will have already occurred by the time the change in ownership has occurred.

 

trigger smsNotificationChange on case (after insert, after update) {
    set<id> changedOwnerIds = new set<id>();
    for(case record:trigger.new) {
        if(trigger.isnew || trigger.oldmap.get(record.id).ownerid != record.ownerid) {
            changedOwnerIds.add(record.id);
        }
    }
    if(!changedOwnerIds.isEmpty()) {
        smsNotify.forCases(changedOwnerIds);
    }
}

 

public class smsNotify {
    @future public static void forCases(set<id> caseIds) {
        // code to notify new owners here
    }
}