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
ArrgHunterArrgHunter 

Need help with Trigger Rewrite

Hi Guys,

i wrote code some time ago in a trigger but now trying to move it to a class but hitting some errors. any help wll be greatly appreciated.
Aim is to copy the last report's field values to newly created one with in same object.

Code i wrote for trigger and it works fine.

trigger trgUpdateAPR on Annual_Progress_Report__c (after insert) {

Integer HealthSc = 0;
Integer EducationSc = 0;
Integer LivingSc = 0;
Integer Flag = 0;

   
//get previous year value
list<Annual_Progress_Report__c> lstname=new list<Annual_Progress_Report__c>();
for(Annual_Progress_Report__c a:[select id,APR_Education_progress_current_year__c,APR_Health_progress_current_year__c,APR_Living_progress_current_year__c from Annual_Progress_Report__c where Status__c = 'Approved' order by name desc Limit 1])
{
flag = flag +1;
system.debug ('Health Score::-' + a.Health_assessment_progress_score__c);
  // Assign value to temp variable
EducationSc = Integer.valueOf(a.APR_Education_progress_current_year__c);   
LivingSc = Integer.valueOf(a.APR_Living_progress_current_year__c);   
HealthSc = Integer.valueOf(a.APR_Health_progress_current_year__c);   


}   
Annual_Progress_Report__c APR=trigger.new[0];
Annual_Progress_Report__c updRP=[select id from Annual_Progress_Report__c where id = :APR.id];

updRP.APR_Education_progress_last_year__c= EducationSc;
updRP.APR_Living_progress_last_year__c= LivingSc;
updRP.APR_Health_progress_last_year__c= HealthSc;

    //update the new value
    update updRP;
 }


i have created a method that is being called by a trigger.

public void copyPreviousAPR(list<Annual_Progress_Report__c> triggernew){

Integer HealthSc = 0;
Integer EducationSc = 0;
Integer LivingSc = 0;
Integer Flag = 0;

  
//get previous year value
//list<Annual_Progress_Report__c> lstname=new list<Annual_Progress_Report__c>();
for(Annual_Progress_Report__c a:[select id,APR_Education_Current__c from Annual_Progress_Report__c where Status__c = 'Approved' order by name desc Limit 1])
{

  system.debug ('DBG- Education Score:: ' + a.APR_Education_Current__c);

   // Assign value to temp variable
EducationSc = Integer.valueOf(a.APR_Education_Current__c);   
//LivingSc = Integer.valueOf(a.APR_Living_progress_current_year__c);   
//HealthSc = Integer.valueOf(a.APR_Health_progress_current_year__c);   

}
   
Annual_Progress_Report__c APR = trigger.new[0];
Annual_Progress_Report__c updRP=[select id from Annual_Progress_Report__c where id = :APR.id];


updRP.APR_Education_Previous__c= EducationSc;

//updRP.APR_Living_progress_last_year__c= LivingSc;
//updRP.APR_Health_progress_last_year__c= HealthSc;

    //update the new value
   update updRP;
   
}


i am having problem with these 2 lines

Annual_Progress_Report__c APR = trigger.new[0];
Annual_Progress_Report__c updRP=[select id from Annual_Progress_Report__c where id = :APR.id];


can someone help me to rewrite these 2 lines.

thanks,


Best Answer chosen by ArrgHunter
srlawr uksrlawr uk
do you have any other triggers on Annual_Progress_Report__c records? This is a common error when you have an after update trigger on a record type that then updates another record of the same type, which causes the trigger to fire again.

You need to construct some logic to stop updates and insert happening if, for example, the values already exist due to previous trigger activity. You can see someone discussing this here
http://gtr.net/apex-error-cannot_insert_update_activate_entity-update_opportunity_calculation_fields-maximum-trigger-depth-exceeded-s-object-error/

All Answers

srlawr uksrlawr uk
It's tricky to be sure, but the syntax problem I can see in those two lines is that in the method you are passing the list in as a parameter called "triggernew" but then in your first line of code you are trying to access the list in the trigger format of trigger DOT new..

So I'd try something like

Annual_Progress_Report__c APR = triggernew.get(0);

instead of

Annual_Progress_Report__c APR = trigger.new[0];

see?
Anoop yadavAnoop yadav

Replce: 
Annual_Progress_Report__c APR = trigger.new[0];

with the below one
Annual_Progress_Report__c APR = triggernew[0];

ArrgHunterArrgHunter
thanks srlawr and anoop. that fixed the complier error but now i am getting the Trigger depth Exceeded erro on update DML statement, see as below


Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger trgAnnualProgressReportIns caused an unexpected exception, contact your administrator: trgAnnualProgressReportIns: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0H11000009DDb4EAG; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, trgAnnualProgressReportIns: maximum trigger depth exceeded Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4] Annual_Progress_Report trigger event AfterUpdate for [a0H11000009DDb4]: []: Class.AnnualReportUpdBiodata.copyPreviousAPR: line 30, column 1
ArrgHunterArrgHunter
i have noticed that my debug values are being shown 16 times in the debug log.
srlawr uksrlawr uk
do you have any other triggers on Annual_Progress_Report__c records? This is a common error when you have an after update trigger on a record type that then updates another record of the same type, which causes the trigger to fire again.

You need to construct some logic to stop updates and insert happening if, for example, the values already exist due to previous trigger activity. You can see someone discussing this here
http://gtr.net/apex-error-cannot_insert_update_activate_entity-update_opportunity_calculation_fields-maximum-trigger-depth-exceeded-s-object-error/
This was selected as the best answer
ArrgHunterArrgHunter
Thanks   @Srlawr UK, the issue was with after update. Once I removed it, the recursion issue disappeared.
The article is very informative and I am glad I learned new thing today.

thanks again.