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
VollyVolly 

Map user custom field to custom object via trigger

I have a trigger that pulls information from the owner of the account and places it in the custom object. There are 7 fields in the user object that I need to pull data from. 3 are standard objects, 4 are custom objects. My question is how do I pull the custom field information from the account owner's user record to the custom object fields?

 

My code:

 

trigger Escalation_Trigger on test1__c (before insert, before update) 
{

    list<id> accountIds = new list<id>();
    for (test1__c xxx : trigger.new)
        accountIds.add(xxx.account__c);
    //Map works fine on standard objects in User object.
	map<id,account> approvers = new map<id,account>([select id, owner.id, owner.manager.id, owner.UserRole.id from account where id in :accountIds]);

	//create map of Escalation Manager, Escalation Manager email, DDSM email, LOB email
	//Receiving error on line 12. I do not understand why. 
	map<id,account> managersescalation = new map<id,account>([select owner.Escalation_Manager_Email__c,  DDSM_email__c, LOB_Manager_s_email__c, RP_email__c from account where id in :account]);	

    for (test1 xxx : trigger.new)
    {
        Account approversAccount = approvers.get(xxx.account__c);
        // The first 3 work fine. THey are standard fields on the user object       
        if (xxx.Approving_M__c == null)
            xxx.Approving_M__c = approversAccount.owner.id; 
            
        if (xxx.Approving_Manager__c == null)
            xxx.Approving_Manager__c = approversAccount.owner.manager.id;
 
        if (xxx.Owner_Role__c == null)
            xxx.Owner_Role__c = approversAccount.owner.UserRole.id;
        //----------------
        // These 4 do not work. I do not know why. 
        // Do I reference the field name or API name?	

        if (xxx.Escalation_Email__c == null)
            xxx.Escalation_Email__c = managersescalationAccount.owner.Escalation_Manager_Email__c.id;	        
		
		if (xxx.DDSM_Email__c == null)
            xxx.DDSM_Email__c = managersescalationAccount.owner.DDSM_Email__c;
						
        if (xxx.LOB_Manager_s_email__c == null)
            xxx.LOB_Manager_s_email__c = managersescalationAccount.owner.LOB_Manager_s_email__c.id;	
			
        if (xxx.RP_email__c == null)
            xxx.RP_email__c = managersescalationAccount.owner.RP_email__c.id;	
    }
}   		
		

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Anand@SAASAnand@SAAS

Change the SOQL to:

 

select id, owner.id, owner.manager.id, owner.UserRole.id,owner.Escalation_Email__c,owner.DDSM_Email__c ,owner.LOB_Manager_s_email__c,Owner.RP_email__c  from account where id in :accountIds

 

That should do the trick

 

All Answers

Anand@SAASAnand@SAAS

Change the SOQL to:

 

select id, owner.id, owner.manager.id, owner.UserRole.id,owner.Escalation_Email__c,owner.DDSM_Email__c ,owner.LOB_Manager_s_email__c,Owner.RP_email__c  from account where id in :accountIds

 

That should do the trick

 

This was selected as the best answer
VollyVolly

Tried what you suggested, I received this error:

 

	Error: Compile Error: No such column 'Escalation_Manager_Email' on entity 'User'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 38 column 53

 

 

VollyVolly

I also tried owner.Escalation_Email__c and owner.Escalation_Email__r.  Both attempts made no difference.

VollyVolly

Opps! I had it labled wrong. What you gave me works great. Thanks!