0

I have a custom field on Case object named "Random_Field__c". Now my requirement is that whenever the case owner is changed from one queue to another queue, I want to store this new queue value in this custom field. The value should be captured in this field only when the case owner changes from a queue to another queue and not from any user to any queue or any queue to any user.

I successfully achieved this requirement using the PB in which i put the entry criteria as follows and updating the field subsequently oncew the criteria is met and it is working fine :-

AND
(
    ISCHANGED([Case].OwnerId),
    NOT(ISNULL([Case].OwnerId)), 
    BEGINS(PRIORVALUE([Case].OwnerId) , "00G"),
    BEGINS([Case].OwnerId , "00G")
)

I want to know how to achieve the same with the help of Apex Trigger. How to fetch the prior value of the case owner as queue or not in Apex and check for the latest value and every other thing.

Please help me out!!! I really wanna know out of my curiosity.

Thanks in Advance, Ali

0

Here is code example you can insert in your before update trigger.

I have matched each code row to your formula to clarify it.

for (Case case : Trigger.new) {
    Case oldCase = Trigger.oldMap.get(case.Id);
    if (oldCase.OwnerId != case.OwnerId //ISCHANGED([Case].OwnerId)
            && case.OwnerId != null //NOT(ISNULL([Case].OwnerId))
            && oldCase.OwnerId.getSobjectType().getDescribe().getKeyPrefix() == '00G' //BEGINS(PRIORVALUE([Case].OwnerId) , "00G")
            && case.OwnerId.getSobjectType().getDescribe().getKeyPrefix() == '00G' //BEGINS([Case].OwnerId , "00G")
            ) {
        //do something
    }
}

Here is documentation from SF:

| improve this answer | |
  • Thanks a lot Mariia... this was really helpful... If you don't mind can you please share wd me the document that would contain the methods that you have used, from where I can refer to these methods like getSobjectType(), getDescribe() and getKeyPrefix(). Thank you again!!! – SFDC Alex Jul 28 at 10:43
  • @SFDCAlex Have updated the answer with relevant links to documentation. – Mariia Illarionova Jul 28 at 10:58
  • got it...thanks a lot!!! appreciate all your help on this... – SFDC Alex Jul 28 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.