+ Start a Discussion
Nejib EssouriNejib Essouri 

Trigger that affects an old value from one record to another created

Hi guys, I would asign the old value of "stock_actuel__c" to the new field when a record is created. I develop this trigger. The problem is that when I insert a record I have an exception nullpointer:
MyTigger:
trigger InvoiceStock on Tissus__c (after insert, after update, after delete) {
   for(Tissus__c tnew : Trigger.New)
       for(Tissus__c told : Trigger.Old){
           if(tnew.Name == told.Name)
               tnew.stock_actuel__c = told.stock_actuel__c ;
       }
}
the Exception:
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger InvoiceStock caused an unexpected exception, contact your administrator: InvoiceStock: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.InvoiceStock: line 3, column 1
 
Best Answer chosen by Nejib Essouri
Abhishek BansalAbhishek Bansal
Hi Nejib,

Please try with the below code :
trigger InvoiceStock on Tissus__c (before insert) {
	Set<String> tissusNameSet = new Set<String>();
	for(Tissus__c tiss : trigger.new){
		tissusNameSet.add(tiss.Name);
	}
	
	Map<String,Tissus__c> mapOfTissus = new Map<String,Tissus__c>();
	for(Tissus__c tiss : [Select Name,stock_actuel__c from Tissus__c where name In : tissusNameSet]){
		mapOfTissus.put(tiss.Name,tiss);
	}
	for(Tissus__c newTissus : trigger.New){
		if(mapOfTissus.containsKey(newTissus.Name)){
			newTissus.stock_actuel__c = mapOfTissus.get(newTissus.Name).stock_actuel__c ;
		}
	}
}

Let me know if you have any issue.

Thanks,
Abhishek

All Answers

Vijay NagarathinamVijay Nagarathinam
Hi Nejib,

Try the following code it will be work.
 
trigger InvoiceStock on Tissus__c (after insert, after update, after delete) {
   for(Tissus__c tnew : Trigger.New){
       if(tnew.Name == trigger.oldMap.get(tnew.Id).Name){
               tnew.stock_actuel__c = told.stock_actuel__c ;
       }
   }
}

Let me know if you have any question regarding this.

Thanks,
Vijay
Akhil AnilAkhil Anil
Trigger.Old will not work with After Insert. You need to remove the "after insert" from the trigger events. A newly entered record will anyway not have an old value. So you need not consider the "after insert" event.
 
Nejib EssouriNejib Essouri
@Vijay
I try the code below and the same exception is there..
trigger InvoiceStock on Tissus__c (after insert, after update, after delete) {
   for(Tissus__c tnew : Trigger.New){
       if(tnew.Name == trigger.oldMap.get(tnew.Id).Name){
               tnew.stock_actuel__c = trigger.oldMap.get(tnew.Id).stock_actuel__c ;
       }
   }
}

 
Akhil AnilAkhil Anil
Hi Nejib,

This is how your code ideally should be
 
trigger InvoiceStock on Tissus__c (after update) {

   for(Tissus__c tnew : Trigger.New){

       if(tnew.Name == trigger.oldMap.get(tnew.Id).Name){

               tnew.stock_actuel__c = trigger.oldMap.get(tnew.Id).stock_actuel__c ;

       }

   }

}

Kindly mark it as an answer if that works. 
Vijay NagarathinamVijay Nagarathinam
@Nejib Essouri, Remove the after insert event in the trigger, because trigger.oldMap.get() will returns the values in update event only. 

Let me know if you face any issue.

Thanks,
Vijay
Nejib EssouriNejib Essouri
@Guys, even when i remove the after insert event the same exception is there..
User-added image
Jithin Krishnan 2Jithin Krishnan 2
Hi,
Please give this a try:
trigger InvoiceStock on Tissus__c (before update) {
   for(Tissus__c tnew : Trigger.New){
           if(tnew.Name == Trigger.OldMap.get(tNew.id).Name){
               tnew.stock_actuel__c = Trigger.OldMap.get(tNew.id).stock_actuel__c ;
       }
  }
}
Thanks!
Abhishek BansalAbhishek Bansal
Hi Nejib,

You are getting the exception because you are trying to updatthe same record in after update context.
In after update context the record is considered to be Read-Only so you cannot update any value of the same record
Please try with the below code:
trigger InvoiceStock on Tissus__c (before update) {
   for(Tissus__c newTissus : trigger.New){
	   //Store the reference of Old Tissus
	   Tissus__c oldTissus = trigger.OldMap.get(newTissus.id);
	   
       //Compare the old and new Name    
	   if(newTissus.Name == oldTissus.Name){
		   newTissus.stock_actuel__c = oldTissus.stock_actuel__c ;
       }
   }
}
Let me know if you have any issue.

Thanks,
Abhishek
Nejib EssouriNejib Essouri
Nothing chanded the exception is tther'...
Abhishek BansalAbhishek Bansal
Have you tried it with the code given by me
Nejib EssouriNejib Essouri
@Abhishek
i try your code, the exception is not appeared but the new firld is not uodated. 
Abhishek BansalAbhishek Bansal
I dont see any error in the code.
May be you are missing something in your testing here.
If it is possible for you than please contact me on :
Gmail : abhibansal2790@gmail.com
Skype : abhishek.bansal2790
Abhishek BansalAbhishek Bansal
Hi Nejib,

Please try with the below code :
trigger InvoiceStock on Tissus__c (before insert) {
	Set<String> tissusNameSet = new Set<String>();
	for(Tissus__c tiss : trigger.new){
		tissusNameSet.add(tiss.Name);
	}
	
	Map<String,Tissus__c> mapOfTissus = new Map<String,Tissus__c>();
	for(Tissus__c tiss : [Select Name,stock_actuel__c from Tissus__c where name In : tissusNameSet]){
		mapOfTissus.put(tiss.Name,tiss);
	}
	for(Tissus__c newTissus : trigger.New){
		if(mapOfTissus.containsKey(newTissus.Name)){
			newTissus.stock_actuel__c = mapOfTissus.get(newTissus.Name).stock_actuel__c ;
		}
	}
}

Let me know if you have any issue.

Thanks,
Abhishek
This was selected as the best answer