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
faceroy123faceroy123 

Case owner details via apex

Hi,

 

I'm trying to pull through some details relating to a case owner.

I have workflows that fire if a case is owned by a queue that work fine.

I now just want to fire back a text field from the related user (a custom relationship field) but it doesn't seem to populate:

 

There are no errors on the code it just doesn't populate even though the field is populated on the user record.

 

thanks

 

trigger region on case (before Insert, before Update) {

for(case x : Trigger.New){

if(x.User__c == null)
{
x.case_owner_region__c = x.RCCM_region__c ;}
else if(x.User__c!= null)
{
x.case_owner_region__c = x.user__r.region__c ;
}
}

}

 

Rahul SharmaRahul Sharma

hi faceroy123,

 

With trigger.new look-up fields can't be accessed. simply returns null value even if we try to acces the fields.

So, you would need to query  the fields and Iterate through it.

for(case x : [Select Required/Accessed Field Names from Case where Id in: Trigger.new])


faceroy123faceroy123

Thanks very much.

 

This is all new to me.

 

So do I put the name of the field in that section? What about the final section?

 

for(case x : [Select case.user__r.region__c from Case where Id in: Trigger.new]){

 

Rahul SharmaRahul Sharma

Hi,

try using following code, Instead of using Trigger.new list directly, use query.

trigger region on case (before Insert, before Update) {
	for(case x : [Select Id, User__c, case_owner_region__c, RCCM_region__c, user__r.region__c from case where Id in: Trigger.New]){
		if(x.User__c == null)
		{
			x.case_owner_region__c = x.RCCM_region__c ;
		}
		else if(x.User__c!= null){
			x.case_owner_region__c = x.user__r.region__c ;
		}
	}

}

 

faceroy123faceroy123

No errors this time but the fields do not populate as I would expect.

 

If the case is owned by a user a custom lookup field (User__c)will populate based on another trigger I have that works fine. This field is a lookup to the user. If this is populated I want to lookup on that user to find a region field (user__r.region__c) and populate it in (case_owner_region__c).

If the case is owned by a queue, the custom lookup to user is left blank and a workflow rule fires to populate a case field with a region.(RCCM_Region__c). In this instance I want this populated in the field (Case_owner__region__c).

 

I hope this is clear. Apologies but I'm still learning :-)