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
nmnbawanmnbawa 

Trigger Help

I have a trigger question.

 

I have a custom object named employee with a lookup field to the user record called Salesforce_user__c

 

I have another custom object called DD master list in which there is a field called Account_Manager__c that is a lookup to employee object.

 

 

Mt requirement is to populate the Account manager Salesforce User id in the new AM__c field. Below is the trigger i wrote(that does not work at all), if someone can help me with the test class and refinining the trigger I would be very grateful to you.

Please help

trigger SetDDmasterFields on DD_master_list__c (before insert, before update) {
    Map<Id,User> userMap = new Map<Id,User>();
    
    for (dd_master_list__c dd : Trigger.new) {
        userMap.put(dd.account_manager__r.Salesforce_User__c, null);
    }
    userMap.remove(null);
    
    if (!userMap.keyset().isEmpty()) {
        for (User u : [SELECT Id FROM User WHERE Id IN :userMap.keyset()]) {
            userMap.put(u.Id,u);
        }
        
        for (dd_master_list__c dd : Trigger.new) {
            if (userMap.get(dd.account_manager__r.Salesforce_User__c) != null) {
              dd.AM__c = userMap.get(dd.account_manager__r.Salesforce_User__c).Id;
          }
        }
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

You can't use references directly in a trigger. You also don't really need to query the user table directly just the employee table based off the Account Managers.

 

Something like this should get you on the right track (I don't know the API name of the Employee table so you probably need to change that).

 

trigger SetDDmasterFields on DD_master_list__c (before insert, before update) 
{
    Map<Id,Id> userMap = new Map<Id,Id>();
    
	for (DD_master_list__c item : Trigger.new)
	{
		if (item.Account_Manager__c != null)
		{
			userMap.put(item.Account_Manager__c, null);
		}
	}
	
	for (Employee__c item : [SELECT Id, Salesforce_User__c FROM Employee__c WHERE Id IN :userMap.keySet()])
	{
		userMap.put(item.Id, item.Salesforce_User__c);
	}
			
	for (DD_master_list__c item : Trigger.new)
	{
		item.AM__c = userMap.get(dd.Account_Manager__c);
	}	
}

 

All Answers

Sean TanSean Tan

You can't use references directly in a trigger. You also don't really need to query the user table directly just the employee table based off the Account Managers.

 

Something like this should get you on the right track (I don't know the API name of the Employee table so you probably need to change that).

 

trigger SetDDmasterFields on DD_master_list__c (before insert, before update) 
{
    Map<Id,Id> userMap = new Map<Id,Id>();
    
	for (DD_master_list__c item : Trigger.new)
	{
		if (item.Account_Manager__c != null)
		{
			userMap.put(item.Account_Manager__c, null);
		}
	}
	
	for (Employee__c item : [SELECT Id, Salesforce_User__c FROM Employee__c WHERE Id IN :userMap.keySet()])
	{
		userMap.put(item.Id, item.Salesforce_User__c);
	}
			
	for (DD_master_list__c item : Trigger.new)
	{
		item.AM__c = userMap.get(dd.Account_Manager__c);
	}	
}

 

This was selected as the best answer
nmnbawanmnbawa

Thanks a Ton Sean,

 

Can you please advise the test class as well. Many thanks again.

Sean TanSean Tan

Try something like this (It may not compile / work on first go but should give you a good template)

 

@isTest(SeeAllData=false)
private class TestSetDDMasterFields
{
	private static testMethod void test_Trigger()
	{
		//Need to add any required fields here as needed
		Employee__c employee = new Employee__c(Salesforce_User__c=UserInfo.getUserId());
		insert employee;
		
		//Also need to add any required fields here as needed
		DD_master_list__c masterList = new DD_master_list__c(Account_Manager__c=employee.Id);
		insert masterList;
		
		masterList = [ select Id, AM__c from DD_master_list__c where Id = :masterList.Id ];
		System.assertEquals(employee.Salesforce_User__c, masterList.AM__c);
	}
}