You need to sign in to do that
Don't have an account?

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; } }
Put a null checking
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;
}
}
[image: Error]Error: Compile Error: Method does not exist or incorrect
signature: [MAP].value() at line 15 column 16
Hi nmnbawa,
I am not sure why you need to a trigger to accomplish this. You can simply add a formula field referencing the corresponding names right.
For Lead_Account_Manager__c, you can create a formula LeadAMID__r.Name and for Client_Account_Manager__c you can create another formula ClientAMID__r.Name
If you need a writable field and the trigger then you can use the following optimized code. You can simply use a similar logic for contact trigger.
If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.
ownermap1.values()// soory for my mistake ...