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
manjunath vivekmanjunath 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;

 }
ManojjenaManojjena
Hi Manjunath ,

Try with below code it will help .
trigger Yearlyinvestments  on Budget__c (before update ) {
	  List<Budget__c> budg=new list<budget__C> ();
	  for(Budget__c bud:trigger.new){
		If(bud.Monthlyinvestments__C!=null){
			 bud.Yearlyinvestments__C=bud.Monthlyinvestments__C;
		}
	}
}
Patcs_1Patcs_1
Hi

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.
manjunath vivekmanjunath vivek
Both of the above answers did'nt work, error message is 
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". 
 
Patcs_1Patcs_1
Hi

can you please post your code again

Thanks
manjunath vivekmanjunath vivek
I just want to let you know that 
Monthlyinvestments__C is a formula field and 
Yearlyinvestments__C is a currency field.


 
trigger Yearlyinvestments  on Budget__c (before insert,before update) {


List<Budget__c> budg=new list<budget__C> ();

Public decimal X=0.0;
Public decimal Y=0.0;


for(Budget__c bud:trigger.new){

If(bud.Monthlyinvestments__C!=null){

 
     x=bud.Monthlyinvestments__C;
  
     Y+=X;
     
     bud.Yearlyinvestments__C=y;
  
     budg.add(bud);


    }
    
  }

update budg;

 }
ManojjenaManojjena
Hi Manjunath ,

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 
 
Patcs_1Patcs_1
Hi Manjunath

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