You need to sign in to do that
Don't have an account?
phantom1982
Trigger Problem
Hi,
I wrote a trigger that serves to update a custom field on an object. The field actually gets updated with the change in price of the item. Now am kind of stuck with it if I use the before update trigger, it shows the second last change in the price. I cannot use the After update as record gets locked and I cannot update the field value. Below is the trigger code:
trigger TrackPRICEupdate on pb__InventoryItem__c(before update) { pb__InventoryItem__History invh = new pb__InventoryItem__History(); for(pb__InventoryItem__c inv: trigger.new){ invh = [Select NEWVALUE, FIELD, OLDVALUE, CREATEDDATE, PARENTID from pb__InventoryItem__History where PARENTID = :inv.id and FIELD = 'pb__PurchaseListPrice__c' order by CREATEDDATE DESC LIMIT 1]; if(invh.oldvalue <> null && invh.newvalue <> null) { string a = string.valueof(invh.oldvalue); string b = string.valueof(invh.newvalue); decimal old = decimal.valueof(a); decimal newv = decimal.valueof(b); inv.price_change__c = newv - old; } //update inv; } }
Anyone can help?
Why are you fetching history object for old and new? Trigger has standard fields Trigger.new and Trigger.old for it.
try this, it might help you.
All Answers
Why are you fetching history object for old and new? Trigger has standard fields Trigger.new and Trigger.old for it.
try this, it might help you.
Thanks for the reply.
I tested with your code and it gives the error: Variable doesn't exist: trigger.new
I have not compiled my code, it may be syntactically wrong, but logically its correct.
at which line its giving error?
Resolved. Here is the final code.
Thanks Sonali