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
KidNikiKidNiki 

Can not get Relationship Query to return correct info on User Object ManagerId...

Hello,

 

Im trying to write a trigger that updates a field with either the manager or the DelegatedApprover's name.  I used a list to pull all of the id's in to query the database and then a map to gather the info from the User Object.  The problem that I am running into is that when I run my query it returns everything but the referenced stuff.

 

here is my query/code:

 

for (SFDC_Expense_Header__c Expnse : Trigger.new){
		
		exRoleId.add(Expnse.OwnerId);
		
		
	}
	
	Map<id, User> usr = new map<id,User>([Select UserRoleId, Manager.Username, DelegatedApproverId.Username From User where id in  :exRoleId]);

 

ThanX for the help!

 

Best Answer chosen by Admin (Salesforce Developers) 
KidNikiKidNiki

Figured it out! :P  Was using the Object type instead of the reference when setting the value in the last for loop! :P

 

 

 

All Answers

KidNikiKidNiki

Here is the full trigger with what I am trying to do now.  It still will not save to my sandbox

 

trigger SFDC_Expense_Trigger on SFDC_Expense_Header__c (before insert, before update) {
	
	//create a list to store all of the OwnerId's in for each new or updated Expense Report
	set<id> exRoleId = new set<id>();
	
	//For every Expense report being created or modified add the Ownerid to the Set so that there are no duplicates
	for (SFDC_Expense_Header__c Expnse : Trigger.new){
		
		exRoleId.add(Expnse.OwnerId);
		
		
	}
	
	Map<id, User> usr = new map<id,User>([Select UserRoleId, ManagerId, DelegatedApproverId From User where id in  :exRoleId]);
	
	//Select u.Name, u.Manager.Username, u.ManagerId, u.Id, u.DelegatedApproverId From User u

	//create a list that holds the approver Id, this could either be the managerID or the DelegatedApproverId
	//list<id> apprverId = new list<id>();
	
	//loop through the exRoleId list and add the manager or Approver ID to the apprvrId list, determined by UserRoleId neing (trainer)
	for (SFDC_Expense_Header__c Expnse : Trigger.new){
		
		if(usr.get(Expnse.id).UserRoleId == '00E70000000wHfNEAU'){
				
		SFDC_Expense_Header__c.Approver__c = usr.get(Expnse.id).DelegatedApproverId;	
			
		} else {
			
		SFDC_Expense_Header__c.Approver__c = usr.get(Expnse.id).ManagerId;
		
		}
		
	}

}

 Im getting the error, "Description    Path    Resource    Location    Type
Save error: Expression cannot be assigned    /VinSandBox/src/triggers    SFDC_Expense_Trigger.trigger    line 0    Force.com save problem"

 

Helllp!

KidNikiKidNiki

Figured it out! :P  Was using the Object type instead of the reference when setting the value in the last for loop! :P

 

 

 

This was selected as the best answer