+ Start a Discussion
raja subbu 1raja subbu 1 

dml exception which working for before trigger event but its not working for after trigger event plz explain here .i have two custom fields position1__c and candidate1__c with lookup.when ever i try to update email getting exception

trigger actrig on position1__c (after insert) {
    list<candidate1__c> candlist=new list<candidate1__c>();
    map<id,position1__c> mymap=new map<id,position1__c>();
list<candidate1__c> canlist=[select id,email__c,position1__c from candidate1__c where position1__c=:mymap.keyset()];
    for(candidate1__c ca:canlist){
      ca.email__c=mymap.get(ca.position1__c).email__c; 
        candlist.add(ca);
        
    }
    
    update candlist;
    
    
}
Hemant_SoniHemant_Soni
Hi Raja,
I thought i gave you proper working solution previously. But no problem to tell you again that your "mymap" is blank and without putting any check of null you are trying to get values thats why you are getting error.
trigger actrig on position1__c (after insert) {
	list<candidate1__c> candlist=new list<candidate1__c>();
	map<id,position1__c> mymap=new map<id,position1__c>(); // THis Map Is Blank
	
	list<candidate1__c> canlist=[select id,email__c,position1__c from candidate1__c where position1__c=:mymap.keyset()];
	
	for(candidate1__c ca:canlist){
	if(!mymap.isEmpty() && mymap.containsKey(ca.position1__c)){// ANd Without This check you are getting attempt to de reference on DML
			ca.email__c=mymap.get(ca.position1__c).email__c;// Because you get getting null value here; 
		}
		candlist.add(ca);
	}
	update candlist;
}
Thanks
Hemant
 
Raj VakatiRaj Vakati
try this
 
trigger actrig on position1__c (after insert) {
    list<candidate1__c> candlist=new list<candidate1__c>();
    Map<id,position1__c> mymap=Trigger.newMap;
	
	
    list<candidate1__c> canlist=[select id,email__c,position1__c from candidate1__c where position1__c=:mymap.keyset()];
    for(candidate1__c ca:canlist){
      ca.email__c=mymap.get(ca.position1__c).email__c; 
        candlist.add(ca);
        
    }
    
    update candlist;
    
    
}

 
raja subbu 1raja subbu 1
i am agan getting dml exception  even after taking ur code hemant bro .here is the exception:Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger trigb23 caused an unexpected exception, contact your administrator: trigb23: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.trigb23: line 15, column 1
raja subbu 1raja subbu 1
and one more thing it  is after update event not after inser event
Hemant_SoniHemant_Soni
Use below code 
trigger actrig on position1__c (after Update) {
	list<candidate1__c> candlist=new list<candidate1__c>();
	map<id,position1__c> mymap=new map<id,position1__c>(); // THis Map Is Blank
	
	list<candidate1__c> canlist=[select id,email__c,position1__c from candidate1__c where position1__c=:mymap.keyset()];
	
	for(candidate1__c ca:canlist){
	if(!mymap.isEmpty() && mymap.containsKey(ca.position1__c)){// ANd Without This check you are getting attempt to de reference on DML
			ca.email__c=mymap.get(ca.position1__c).email__c;// Because you get getting null value here; 
		}
		candlist.add(ca);
	}
	if(candlist.size() > 0){
		update candlist;
	}
}
If still you are getting error then we can connect on skype.
raja subbu 1raja subbu 1
still getting error bro (id not specified in update call)  is the error message
raja subbu 1raja subbu 1
sry hemant bro getting error trigger because other triggers on position was in active.now it was saving sucessfully.but the email that i was trying to update on position1__c was not reflecting back  to candidate1__c.plz give solution .thanks a lott for helping me