0

I see this is a pretty common question, and I'm sorry if this exact scenario has been asked before.

I have a case validation trigger that I am using in lieu of standard validation rules. I need to add the following scenario to it, but am having trouble with the logic.

In human terms first: If running user profile is not System Admin or Trading and the status = "trades placed", throw error of "cannot choose this status. if trades are needed, please update status to "Trades Needed*". To add another layer to this, if trading changes the status to "Trades Placed" and changes the owner back to the case submitter, that person should now be able to make any updates on the case.

An easy fix is to create a checkbox of "trades placed" and change that to true once trades are placed, but I'm at all 500 fields for cases with too much effort trying to find one to delete. Just seeing if there is something else I could do. Here is my code thus far:

 if((UserInfo.getProfileId() != '00ei0000000KWzj' || UserInfo.getProfileId() != '00ei0000001Mglo') && c.status == 'Trades Placed'))
   {
       c.addError('You cannot change status to "Trades Placed".  If Trades are needed, please update the status to "Trades Needed*"');
   }

But there's the obvious issue of the submitter not being able to make any updates to the case once trading is done and assigns the case back to them with the status of "Trades Placed"

Is there any kind of logic possible for this to work - or will I need to find a case field to delete to add "&& c.Trades_Placed__c =! True"?

2
  • well, if it is really hard to find a field to track state in, you could, if you have field history enabled on Status and Owner, query Lead Field History records to see what previous values of those fields were.
    – cropredy
    Commented Apr 23, 2019 at 17:39
  • Do you track the trades in Salesforce? If so, could you check whether there are placed trades for the record instead of using a field? Commented Apr 23, 2019 at 17:59

1 Answer 1

2
  1. You should notice that you have a small mistake in your decision logic.

When you write: (UserInfo.getProfileId() != '00ei0000000KWzj' || UserInfo.getProfileId() != '00ei0000001Mglo')

this statement will always be true. You need to replace the || with &&.

  1. Is there a specific reason you are using Apex to accomplish this instead of a good old fashioned validation rule?

Here is an example: (I wrote it on the fly so a bit of testing / tweaking may be required)

NOT(ISPICKVAL(PRIORVALUE(Status), 'Trades Placed'))
&&
ISPICKVAL(Status, 'Trades Placed')
&&
( $Profile.Id != '00ei0000000KWzj' && $Profile.Id != '00ei0000001Mglo' )

Essentially this VR will be flagged if the Status is Changed To 'Trades Placed' and the profile is not ...

You can add another condition or create a seperate validation rule to accommodate the rest of your validation.

  1. If you would prefer to use a trigger (or have to because you exceeded relationship field limits) you can use the same type of logic. For you're situation, I believe this would be a before update trigger and you can compare the old and new values in the status field.

Heres a little snippet that should accomplish the same result as the validation rule above:

for (Case c : trigger.new){
  Case oldCase = trigger.oldMap.get(c.id);
  if(oldCase.Status != 'Trades Placed' && c.status = 'Trades Placed' &&
     UserInfo.getProfileId() != '00ei0000000KWzj' && UserInfo.getProfileId() != '00ei0000001Mglo')

        c.status.addError('....');
} 

Take this further and you can also compare c.OwnerId == [caseSubmitter] to add additional logic to your validation so that it suits your use case.

2
  • I was originally using the standard validation rules, but ran into an issue with going over the limit on custom relationship fields from the 30+ validation rules needed in the org. To clear up some of those relationships, made use of this validation trigger.
    – tDactyl
    Commented Apr 23, 2019 at 17:59
  • I edited my answer. I hope that answers your question in more detail.
    – Z33dawg
    Commented Apr 23, 2019 at 19:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .