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
elmaschingondeguateelmaschingondeguate 

Evaluate prior value of owner field.

Hello, I am new to APEX and in programming in general, so apologies for asking these basic questions.

 

I am trying to create a trigger that evaluates the prior value of the case owner field and if it changed from a queue to a user, update a lookup field (to users) with the new owner, so far I have been unabled to do it and I workarounded this creating a workflow rule that does this evaluation and changes a checkbox to true, I then created a very simple trigger that updates the lookup field if the checkbox is selected(true) , obviously this is not ideal as I would like to create all the functionality in the trigger.

 

Can someone please provide some guidance in how can all this  be achieved in the trigger . How can I say if the priorowner was a queue (or a list of queues) and the new owner is a user, update the lookup field with the new owner.

 

Many thanks in advance for any help on this.

Best Answer chosen by Admin (Salesforce Developers) 
ShamilShamil

You can rely on the ID Prefixes of User object - the owner, with prefix '005' and Queue, which technically is a Group object with ID prefix '00G'. The trigger would be:

 

trigger CaseTrigger on Case (before update) {
	for(Case c: Trigger.new){
		String oldOwnerId = Trigger.oldMap.get(c.Id).OwnerId;
		String newOwnerId = c.OwnerId;
		if(newOwnerId.substring(0,3).equals('005') && oldOwnerId.substring(0,3).equals('00G')){
			c.New_Owner__c = c.OwnerId; 
		}
	}
}

 

All Answers

ShamilShamil

You can rely on the ID Prefixes of User object - the owner, with prefix '005' and Queue, which technically is a Group object with ID prefix '00G'. The trigger would be:

 

trigger CaseTrigger on Case (before update) {
	for(Case c: Trigger.new){
		String oldOwnerId = Trigger.oldMap.get(c.Id).OwnerId;
		String newOwnerId = c.OwnerId;
		if(newOwnerId.substring(0,3).equals('005') && oldOwnerId.substring(0,3).equals('00G')){
			c.New_Owner__c = c.OwnerId; 
		}
	}
}

 

This was selected as the best answer
InternalServerErrorInternalServerError

 

Hi Shamil, thank you so much!. I was relying on the prefix as well but on the WF rule, I didn't know how to do this on a trigger. Thanks again :)