You need to sign in to do that
Don't have an account?
manjunath vivek
Hel me with trigger in which I need to update a custom field in custom object.
Hi,
I am trying to modify the values of monthly investments and update it to the yearly investments of the same object but Iam getting an error message.
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger Yearlyinvestments caused an unexpected exception, contact your administrator: Yearlyinvestments: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Yearlyinvestments: line 15, column 1".
Below is my code
trigger Yearlyinvestments on Budget__c (after insert, after update) {
List<Budget__c> budg=new list<budget__C> ();
for(Budget__c bud:trigger.new){
If(bud.Monthlyinvestments__C!=null){
decimal X,Y;
x=bud.Monthlyinvestments__C;
Y+=X;
bud.Yearlyinvestments__C=y;
budg.add(bud);
}
}
update budg;
}
I am trying to modify the values of monthly investments and update it to the yearly investments of the same object but Iam getting an error message.
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger Yearlyinvestments caused an unexpected exception, contact your administrator: Yearlyinvestments: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Yearlyinvestments: line 15, column 1".
Below is my code
trigger Yearlyinvestments on Budget__c (after insert, after update) {
List<Budget__c> budg=new list<budget__C> ();
for(Budget__c bud:trigger.new){
If(bud.Monthlyinvestments__C!=null){
decimal X,Y;
x=bud.Monthlyinvestments__C;
Y+=X;
bud.Yearlyinvestments__C=y;
budg.add(bud);
}
}
update budg;
}
Try with below code it will help .
You should intialize the Y, and you should be using a before trigger. While there are some exceptions you never want to be updating a record that is being processed by the trigger using DML statements, as this can lead to trigger recursion. If you use a before trigger you can update the record directly without a DML statement.
Try this below code,
trigger Yearlyinvestments on Budget__c (before insert, before update) {
for(Budget__c bud:trigger.new){
If(bud.Monthlyinvestments__C!=null){
decimal X,Y=0.0;
x=bud.Monthlyinvestments__C;
Y+=X;
bud.Yearlyinvestments__C=y;
}
}
}
Hope this Helps!
Thanks
P.S - If this solves your problem, Please mark this as solution by selecting it as best answer.
Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger Yearlyinvestments caused an unexpected exception, contact your administrator: Yearlyinvestments: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Yearlyinvestments: line 17, column 1".
can you please post your code again
Thanks
Monthlyinvestments__C is a formula field and
Yearlyinvestments__C is a currency field.
First you need to check the Yearlyinvestments__C accessbility .Click on that field and check the accessibility .
If accessbility is there then please post the formula of the formula field and what is teh return type of the field .
If all the values you are geeting before event then better you should use before event in trigger then after .
Thnaks
Manoj
you can also use "System.debug" to check the values in the debug logs. So that you can easily understand where you are geting this error.
Thanks