You need to sign in to do that
Don't have an account?
ArrgHunter
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,
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,
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
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?
Replce:
Annual_Progress_Report__c APR = trigger.new[0];
with the below one
Annual_Progress_Report__c APR = triggernew[0];
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
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/
The article is very informative and I am glad I learned new thing today.
thanks again.