You need to sign in to do that
Don't have an account?
Problem in updating an old record by a value in the new record using trigger.
HI. Am trying to update immediately previous record from a value of the current record. The field is End__c which is of type date / time. Given below is the code. Its not working. Can anyone help pls.
trigger EndTimeUpdate on Sales_Order_Status_History__c(after insert){
/* List <Sales_Order_Status_History__c> soshList = [SELECT update__c FROM Sales_Order_Status_History__c WHERE Id=: ApexPages.currentPage().GetParameters().get('Id')]; */
Sales_Order_Status_History__c sosh3 = [SELECT update__c FROM Sales_Order_Status_History__c WHERE Id=: ApexPages.currentPage().GetParameters().get('Id')];
Sales_Order_Status_History__c sosh;
Sales_Order_Status_History__c sosh2;
if(trigger.isInsert){
for(Sales_Order_Status_History__c sosh1: trigger.old){
if(trigger.oldMap.get(sosh2.id).End__c == Null){
// trigger.oldMap.get(sosh2.id).End__c = soshList.update__c;
trigger.oldMap.get(sosh2.id).End__c = sosh3.update__c;
}
}
update sosh2;
}
}
Your requirement is not clear...
Since you are using 'after insert', I guess you are trying to make update to previously created record.
Record 1
Record 2
Record 3 - Previous Record
Record 4 - New Record
If thats the case, you can select Id and Orderby time DESC and get the previous one.(Make sure its always same in Production)
Sort DESC(Always N-1);
Record 4 - New Record
Record 3 - Previous Record
Record 2
Record 1
//Sample Code
List<Sales_Order_Status_History__c > sosh = [SELECT update__c FROM Sales_Order_Status_History__c ORDER BY CreatedDateTime DESC];
// previous Update_c vaue
system.debug('Previous Value' +sosh[sosh.size() - 1)].update__c);
First of all, trigger.old is not availabe in insert.
Please see: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm
Second, I really didn't see any single line of your code make sense.
- if you are trying to get the record which you are inserting, please use trigger.new[0] (I assume you are apply this trigger only for UI)
- you are using sosh1 in For loop as single occurance, but didn't see you using it within ur For loop!!! .....