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

Please help.

 

I have a custom object on which I have two record types A and B. I have two triggers to populate the Contact Owner or the Lead owner automatically in the two text fields Contact Account Manager and Lead Account Manager respectively. The problem is that I get the null reference error when I only try  to select a lead or a contact as on the record type page assignment I am either showing lead or contact. 

 

Here are two triggers. IF someone can provide test class as well it would be great. Thanks a ton in advance.

rigger LeadOwner on DD_Master_List__c (before insert, before update) {
    Map<Id, User> ownerMap = new Map<Id, User>{};
    
    for(DD_Master_List__c tt: Trigger.new)
    {   
        ownerMap.put(tt.LeadAMID__c, null);       
    }
    
    ownerMap.putAll([SELECT Id, Name FROM User WHERE Id IN :ownerMap.keySet()]);
    
    for(DD_Master_List__c tt: Trigger.new)
    {
       
        User u = ownerMap.get(tt.LeadAMID__c);
        tt.Lead_Account_Manager__c= u.name;
    }
    }

 

trigger ContactOwner on DD_Master_List__c (before insert, before update) {
    Map<Id, User> ownerMap = new Map<Id, User>{};
    for(DD_Master_List__c tt: Trigger.new)
    {        
        ownerMap.put(tt.ClientAMID__c, null);        
    }
    
    ownerMap.putAll([SELECT Id, Name FROM User WHERE Id IN :ownerMap.keySet()]);
    
    for(DD_Master_List__c tt: Trigger.new)
    {
       
        User u = ownerMap.get(tt.clientAMID__c);
        tt.Client_Account_Manager__c= u.name;
    }
    }

 

Avidev9Avidev9

Put a null checking

 

 User u = ownerMap.get(tt.LeadAMID__c);
if(u!=null){
// do other stuff here
}

 

sushant sussushant sus

according to my understanding u have two lookup og contact and lead on custom object according to that u wanto update fiel

my code

 

trigger ContactOwner on DD_Master_List__c (before insert, before update)
{
    Map<Id, contact> ownerMap1 = new Map<Id, contact>{};
    Map<Id, lead> ownerMap = new Map<Id, lead>{};
    set<id>lid = new set<id>();
    set<id>cid=new set<id>();
    for(DD_Master_List__c tt: Trigger.new)
    {        
        ownerMap.put(tt.LeadAMID__c, null);  
        ownerMap1.put(tt.ClientAMID__c, null);          
    }
    
    ownerMap.putAll([SELECT Id, Name,ownerid FROM lead WHERE Id IN :ownerMap.keySet()]);
    ownerMap1.putAll([SELECT Id, Name,ownerid FROM contact WHERE Id IN :ownerMap1.keySet()]);
    for(lead l:ownermap.value())
    {
    lid.add(l.ownerid);
    }
    for(contact co:ownermap1.value())
    {
    cid.add(co.ownerid);
    }
    map<id,user> mpuser=new map<id,user>([select id,name from user where id in :lid]);
    map<id,user> mpusercontact=new map<id,user>([select id,name from user where id in :cid]);
    for(DD_Master_List__c tt: Trigger.new)
    {
       tt.Lead_Account_Manager__c= mpuser.get(ownerMap.get(tt.LeadAMID__c).ownerid).name;
        tt.Client_Account_Manager__c= mpusercontact.get(ownerMap1.get(tt.clientAMID__c).ownerid).name;
    }
}

Bhawani SharmaBhawani Sharma
Add a null check before adding tt.LeadAMID__c or tt.ClientAMID__c in map. Don't add null
nmnbawanmnbawa
I am getting this.

[image: Error]Error: Compile Error: Method does not exist or incorrect
signature: [MAP].value() at line 15 column 16
Bhawani SharmaBhawani Sharma
It's map.values(). You are missing "s".
Kiran  KurellaKiran Kurella

Hi nmnbawa

trigger LeadOwner on DD_Master_List__c (before insert, before update) {
    Map<Id, User> ownerMap = new Map<Id, User>{};
    
    // gather various lead owners
    for (DD_Master_List__c tt: Trigger.new) {   
        if (tt.LeadAMID__c != null && (trigger.isInsert || tt.LeadAMID__c != trigger.oldMap.get(tt.Id).LeadAMID__c)) ownerMap.put(tt.LeadAMID__c, null);
    }
    
    if (!ownerMap.isEmpty()) {
	    ownerMap.putAll([SELECT Id, Name FROM User WHERE Id IN :ownerMap.keySet()]);
	    
	    for(DD_Master_List__c tt: Trigger.new) {
	    	if (tt.LeadAMID__c != null && (trigger.isInsert || tt.LeadAMID__c != trigger.oldMap.get(tt.Id).LeadAMID__c)) {
	        	tt.Lead_Account_Manager__c= ownerMap.get(tt.LeadAMID__c).name;
	    	}
	    }
    }
}

 

sushant sussushant sus
ya it should be

ownermap1.values()// soory for my mistake ...