How to Replace a Trigger code with Validation Rule - Answers - Salesforce Trailblazer Community
Ask Search:
Mathew AbrahamMathew Abraham 

How to Replace a Trigger code with Validation Rule

I have the following code in Account Object, and would like to replace it with a validation rule to make it simple and easy to manage.  How do I do that?

  if (UserInfo.getProfileId() != '00ef4000000Ner0AAC') {
            if (thisAccount.Scheduler__c == null) {
                if (thisAccount.Status_Detail__c == 'Scheduled') {
                    List<User> userList = [SELECT Id FROM User WHERE Id =: UserInfo.getUserId()];
                    if (userList.size() == 1) {
                        thisAccount.Scheduler__c = userList[0].Id;
                    }
                }
            } else {
                if (thisAccount.Screening_Scheduled_Canceled_Date__c != null || thisAccount.Screening_Scheduled_No_Show_Date__c != null) {
                    if (thisAccount.Status_Detail__c == 'Scheduled' || thisAccount.Status_Detail__c == 'Rescheduled') {
                        thisAccount.Status_Detail__c = 'Rescheduled';
                        List<User> userList = [SELECT Id FROM User WHERE Id =: UserInfo.getUserId()];
                        if (userList.size() == 1) {
                            thisAccount.Scheduler__c = userList[0].Id;
                        }

Appreciate your expert advise.

Thanks,
Mathew
Best Answer chosen by Mathew Abraham
Tony TrinhTony Trinh
Ah, I see what you're saying, you want the error message to appear and tell them they need to fill it out rather than update the field as your trigger was doing.

If that is the case, you can give this a try:
OR(
  AND(
    $Profile.Name != "Name of Profile",
    ISPICKVAL(Status_Detail__c = "Scheduled"),
    ISBLANK(Scheduler__c)
  ),
  AND(
    OR(
      NOT(ISBLANK(Screening_Scheduled_No_Show_Date__c)),
      NOT(ISBLANK(Screening_Scheduled_Canceled_Date__c))
    ),
    OR(
      ISPICKVAL(Status_Detail__c = "Scheduled"),
      ISPICKVAL(Status_Detail__c = "Rescheduled")
    )
  )
)

All Answers

Manish SajwanManish Sajwan
Hi Mathew,

Validatiion rules are to throw error when a certain criteia is met. The trigger code you have mentioned here doesn't seem to throw any errors.
 
Tony TrinhTony Trinh
Hello Matthew,

This will not be a Validation Rule. This will be a Process Builder automation.

Give this a try for your criteria for the first if of your trigger:
AND(
$Profile.Name != "Name of Profile", /* or you can use $Profile.Id != "00ef4000000Ner0", */
ISPICKVAL([Account].Status_Detail__c = "Scheduled"),
ISBLANK([Account].Scheduler__c)
)
Set the immediate action as: Scheduler equals Formula:
$User.Id

For your second if, give this a try:
AND(
  OR(
    NOT(ISBLANK([Account].Screening_Scheduled_No_Show_Date__c)),
    NOT(ISBLANK([Account].Screening_Scheduled_Canceled_Date__c))
  ),
  OR(
    ISPICKVAL([Account].Status_Detail__c = "Scheduled"),
    ISPICKVAL([Account].Status_Detail__c = "Rescheduled")
  )
)
Set the immediate action as: Scheduler equals Formula:
$User.Id
Set Status Detail equals picklist Rescheduled.
Mathew AbrahamMathew Abraham
Thank you Tony amd Manish, 

Mnish - Yes, I do want to throw an error, because right now users do not understand why they cannot perform the task, for example change the Scheduler!  I need to show them a clear error message so that they know why.

Tony, This is exactly what I was expecting.  Now, can't we not combine these to formulae into one and create validation rule?  This is a great start, I think. 

Thank you both, appreciate it.
Regards,
Mathew 
Tony TrinhTony Trinh
Ah, I see what you're saying, you want the error message to appear and tell them they need to fill it out rather than update the field as your trigger was doing.

If that is the case, you can give this a try:
OR(
  AND(
    $Profile.Name != "Name of Profile",
    ISPICKVAL(Status_Detail__c = "Scheduled"),
    ISBLANK(Scheduler__c)
  ),
  AND(
    OR(
      NOT(ISBLANK(Screening_Scheduled_No_Show_Date__c)),
      NOT(ISBLANK(Screening_Scheduled_Canceled_Date__c))
    ),
    OR(
      ISPICKVAL(Status_Detail__c = "Scheduled"),
      ISPICKVAL(Status_Detail__c = "Rescheduled")
    )
  )
)
This was selected as the best answer
Mathew AbrahamMathew Abraham
Tony, this is awesome!  This is exactly what I was thinking too, but had some confusion with the AND/OR usage!
Below was what created, and thought would work: 

OR( 
          AND(
              $Profile.Name != "Standard User - HCM Supervisor" || $Profile.Name != "System Administrator", 
              ISPICKVAL(Status_Detail__c,"Scheduled"),
              ISBLANK(Scheduler__c),

    NOT(ISBLANK(Screening_Scheduled_No_Show_Date__c)),
    NOT(ISBLANK(Screening_Scheduled_Canceled_Date__c)),
    ISPICKVAL(Status_Detail__c, "Scheduled"),
    ISPICKVAL(Status_Detail__c, "Rescheduled")
  ))

But I think your steps are correct, except that the equal sign need to be replaced with a coma, right?
Thank you so much
Mathew
Mathew AbrahamMathew Abraham
Tony,
The formula you suggested works great!  Thank you so much. 

Now one additional requirement we have is, if the Scheduler field is empty (meaning the first time Scheduling), put the user who change the Status to Schedule as the Scheduler.  How can we add that to the formulae?

Appreciate your help.
Best,
Mathew
Tony TrinhTony Trinh
Hello Matthew,

That is why I had you go with the Workflow.
You cannot use a validation rule to insert/update/delete/undelete data into your Salesforce org. Validations are used to only block invalid data from entering the org.
Mathew AbrahamMathew Abraham
Okay, thanks for clarifying that point.  If so, the field update portion we will do with a Process Builder.  The validation rule works perfect for the rest of our issue!
Thank you..