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 

code improvement

Hi Guys,

 

i wrote a trigger which  is doing the job and i want  to know if there is a room to improve the code.

 

 

trigger aprUpdate on Annual_Progress_Report__c (after insert) {

Integer SC = 0;
    
//get previous month/year value
list<Annual_Progress_Report__c> lstname=new list<Annual_Progress_Report__c>();
for(Annual_Progress_Report__c a:[select id, Health_assessment_progress_score__c from Annual_Progress_Report__c order by id desc Limit 2])
 {
 
  // Assign value to temp variable
 SC = Integer.valueOf(a.Health_assessment_progress_score__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.Health_assessment_progress_score2__c= SC;
    //update the new value
    update updRP;
    

}

 

 

thanks,

Best Answer chosen by Admin (Salesforce Developers) 
VaasuVaasu
I just modified your trigger assuming only one record will create at a time.
Trigger you have written is fine but as you requested for improvement I just removed some lines.

trigger aprUpdate on Annual_Progress_Report__c (after insert) {

Annual_Progress_Report__c updRP=[select id from Annual_Progress_Report__c where id = :trigger.new[0].id];
updRP.Health_assessment_progress_score2__c= [select id, Health_assessment_progress_score__c from Annual_Progress_Report__c order by id desc Limit 1].Health_assessment_progress_score__c;
//update the new value
update updRP;
}

All Answers

VaasuVaasu
Is there any specific reason why you have used After Insert event here, as you are updating the same record why dont you go with before update/ insert?
ArrgHunterArrgHunter

I  want to achieve the below, but i was suggested to use after insert

 

 

I have a custom object A that stores user progress details every month. When a new record is added next month I want to copy user’s previous month’s scorer to newly added record. I nut shall I want to have users previous month’s score into the current month record in custom field.

 

Am I right to think that I need before insert trigger to achieve it. But don’t know how I can save previous month’s score in the trigger temporarily and then added to new record. Any help?

 

 

http://boards.developerforce.com/t5/Apex-Code-Development/on-insert-wants-to-copy-values-from-previous-record/m-p/667460#M124040

 

VaasuVaasu
Can you clarify me on below questions.
1) How the record is getting created every month in this object?
2) Are there any chances that records can be inserted in bulk like multiple records at a time?
VaasuVaasu
I just modified your trigger assuming only one record will create at a time.
Trigger you have written is fine but as you requested for improvement I just removed some lines.

trigger aprUpdate on Annual_Progress_Report__c (after insert) {

Annual_Progress_Report__c updRP=[select id from Annual_Progress_Report__c where id = :trigger.new[0].id];
updRP.Health_assessment_progress_score2__c= [select id, Health_assessment_progress_score__c from Annual_Progress_Report__c order by id desc Limit 1].Health_assessment_progress_score__c;
//update the new value
update updRP;
}
This was selected as the best answer
ArrgHunterArrgHunter

thanks sales force, the code looks much better now.

 

However i can see you have changed record limit to 1 where as was using 2 (order by id desc Limit 2).

the reason i was using 2 was because i am using after insert and i needed to get last rcord's value  hence i was using limit 2 oder by descning to achieve it.

 

the new record will be created when user clicks on a button and fill in the custom required fields and they can create only one record at a time.

 

if i can achieve it with before insert that will be ideal but have no idea how i will insert the new value when record is not created yet.

 

 

thanks,