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
nkbk0108nkbk0108 

Apex Trigger or Workflow?

Hello

I want to create a workflow on the Case Object.

What we would like to have happen is:

The Case Owner should change to who ever is the current Support Owner, when Resolution Type is changed to "Escalate Client Services", Unless Support Owner is blank. (I have another workflow in place to take care of this part).

Current Object: Case
Fields:

Case Owner (Lookup)

Support Owner (Custom Field, Formula, Lookup) populated originally on the Account Object

Resolution Type (Picklist).

 

I'm fairly new to Apex Triggers so if that's the path I need to take, I would appreciate any assistance. :)

 

I hope this makes sense.

Thanks
Nancy

Best Answer chosen by Admin (Salesforce Developers) 
Erite11Erite11

To change Owner, workflow is only good when changing to a specific owner (such as a queue or an admin).  Otherwise, you have to have workflows for each support rep.

 

So, its a trigger you need.  An an example, here is a trigger that updates a field in the same way when we escalate a case, so that we know the original owner.

 

trigger updateOriginalOwnerontoEscalatedCase on Case (before update) {

  List<Case> Caseupdates = new List<Case>();
  Caseupdates = Trigger.new;
    
  for (Case Caseupdate : Caseupdates) {
  
    if (Caseupdate.IsEscalated == True && Caseupdate.Original_Owner__c == NULL && Caseupdate.Origin != 'Email Installation' && Caseupdate.Origin != 'Form SecResponse' && String.valueOf(CaseUpdate.ownerId).startsWith('005'))  {
      try {
        Caseupdate.Original_Owner__c = Caseupdate.OwnerId;
      }
      catch (Exception e) {
        //
      }
    }
  }
  
}


 

The "Startswith 005" is important is you use queues.  Case Owner is polymorphic in that is can be User or Queue ID.  Updating a custom field tied to User would break if a queue owned the case, so we use the "Start with" to make sure the object is a user object who owns the case.  We also eliminate cases of certain origins as they auto-escalate and wouldnt have an owner we needed to track.

 


 


All Answers

Shashikant SharmaShashikant Sharma

When workflow and trigger both can perform an action workflow is prefered as it provides more configurability. To compare your case See Trigger vs WorkFlow : http://forceschool.blogspot.com/2011/05/hi-all-triggers-are-very-essential-in.html

nkbk0108nkbk0108

Do you have any suggestions on how I would create a workflow to make this happen?

 

Thanks

Nancy

Erite11Erite11

To change Owner, workflow is only good when changing to a specific owner (such as a queue or an admin).  Otherwise, you have to have workflows for each support rep.

 

So, its a trigger you need.  An an example, here is a trigger that updates a field in the same way when we escalate a case, so that we know the original owner.

 

trigger updateOriginalOwnerontoEscalatedCase on Case (before update) {

  List<Case> Caseupdates = new List<Case>();
  Caseupdates = Trigger.new;
    
  for (Case Caseupdate : Caseupdates) {
  
    if (Caseupdate.IsEscalated == True && Caseupdate.Original_Owner__c == NULL && Caseupdate.Origin != 'Email Installation' && Caseupdate.Origin != 'Form SecResponse' && String.valueOf(CaseUpdate.ownerId).startsWith('005'))  {
      try {
        Caseupdate.Original_Owner__c = Caseupdate.OwnerId;
      }
      catch (Exception e) {
        //
      }
    }
  }
  
}


 

The "Startswith 005" is important is you use queues.  Case Owner is polymorphic in that is can be User or Queue ID.  Updating a custom field tied to User would break if a queue owned the case, so we use the "Start with" to make sure the object is a user object who owns the case.  We also eliminate cases of certain origins as they auto-escalate and wouldnt have an owner we needed to track.

 


 


This was selected as the best answer