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
Collaborative eCollaborative e 

Workflow Rule - Rule Criteria

Need to build a workflow rule to trigger when opportunities owned by certain users (all a part of the same role) meet a certain criteria (close date is within 3 days of today).

I have all figured out execpt for how to tell the system to look for these users.

Any/all help appreciated.
arnt72arnt72
the OwnerID seems to be somewhat limited when it comes to related information. Strangely enough it works better with a custom field that link to a user. So I used the following workaround:
  1. a simple Apex Trigger copies the OwnerID to a custom lookup field to the user object. This field can be excluded from layouts.
  2. in the workflow rule, use formula instead of criteria and access the role through "Insert field" - Opportunity> [YourCustomField]> RoleID or even Role> Name
Collaborative eCollaborative e
Great idea Arnt. Thank you for sharing. I'm new to Apex...do you have any sample code that would help with that piece?
arnt72arnt72
sure. The Custom field  in this Trigger is called OwnerRollup, instead of CustomObj you want to user Opportunity.
Code:
trigger OwnerTransfer03 on CustomObj__c (before insert,before update) {
CustomObj__c[] sos = Trigger.New;

String id;

for ( s:sos){
id=s.OwnerID;


//check if Owner is user and not a queue
if (id.startsWith('0057') ) {
s.OwnerRollup__c = s.OwnerID;
} else {
s.OwnerRollup__c = null;
}
}

}

 you will also need a test class to deploy it from sandbox or Developer account to production. Replace User1 and User2 with usernames in your Org. All this does is create test objects to see if the trigger works. Changes are rolled back so the test does not affect your database.

Code:
public class testOwnerTransfer03 {

  static testMethod void testOwnerTransfer (){
    Sales_Order__c obj = new Sales_Order__c(
        Name = '1234',
        Booked_Date__c = date.newinstance(2008,1,1),
        Account__c = [select ID from Account limit 1].ID,
        OwnerID = [Select ID from user where Name = 'User1' limit 1].ID
        );
    insert obj;
    
    Sales_Order__c obj2 = [select id,OwnerID,OwnerRollup__c from Sales_Order__c where id = :obj.ID];
    System.assertEquals(obj2.OwnerRollup__c, [Select ID from user where Name = 'User1' limit 1].ID);
    
    
    obj.OwnerID = [Select ID from user where Name = 'User2' limit 1].ID;
    update obj;
    obj2 = [select id,OwnerID,OwnerRollup__c from Sales_Order__c where id = :obj.ID];
    System.assertEquals(obj2.OwnerRollup__c, [Select ID from user where Name = 'User2' limit 1].ID);
    

  } 

}