You need to sign in to do that
Don't have an account?
ArrgHunter
Capturing last saved records fields before adding new one
I have a requirement where, when a user create a new record in custom object i want to copy 3 fields (integer values) from last saved record and insert to newly created one.
Any suggestions on how i can capture last saved records fields before new record gets saved or if there any better way to achieve this.
thanks in advance...
Any suggestions on how i can capture last saved records fields before new record gets saved or if there any better way to achieve this.
thanks in advance...
Great community.
All Answers
This will give you the last most recent saved record. Use above query before performing DML otherwise you will get the latest inserted record.
i am getting erro on
see my code below
===============================
trigger trgAnnualProgressReportIns on Annual_Progress_Report__c (before insert, after insert, after update ) {
public decimal EducationSc = 1;
if (trigger.isBefore){
if (trigger.isInsert){
Set<Id> sobjectSetOfIds = new Set<Id>();
//Iterate through your SObjects
for(Annual_Progress_Report__c s : trigger.new){
if(s.Orphan__c != null){
sobjectSetOfIds.add(s.Orphan__c);
}
}
Map<Id,Orphan__c> sobjectMap = new Map<Id,Orphan__c>([SELECT Id, IRP_Country__r.Primary_PO_User__c
FROM Orphan__c WHERE Id IN : sobjectSetOfIds]);
for(Annual_Progress_Report__c s : trigger.new){
if(sobjectMap.containsKey(s.Orphan__c)){
s.Primary_PO_User__c = sobjectMap.get(s.Orphan__c).IRP_Country__r.Primary_PO_User__c;
}
Annual_Progress_report__c APRcomp = [SELECT Id,APR_Education_Current__c FROM Annual_Progress_Report__c WHERE Orphan__c IN:sobjectSetOfIds
ORDER BY CreatedDate DESC NULLS FIRST LIMIT 1];
EducationSc = APRcomp.APR_Education_Current__c;
}// end if isinsert
}// end if isbefore
// Insert previous years values into newly created report.
if (trigger.isInsert)
{
Annual_Progress_Report__c APR=trigger.new[0];
Annual_Progress_Report__c updRP=[select id from Annual_Progress_Report__c where id = :APR.id]; getting error here List has no rows.
updRP.APR_Education_Previous__c = EducationSc;
//update the APR with new values
update updRP;
} // isinsert trg
} // end of end if after
}
Great community.