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
phantom1982phantom1982 

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?

Best Answer chosen by Admin (Salesforce Developers) 
Sonali BhardwajSonali Bhardwaj

 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.

 

trigger TrackPRICEupdate on pb__InventoryItem__c(before update) {
for(integer i=0; i<trigger.new.size(); i++){

if(trigger.new[i].pb__PurchaseListPrice__c <> null && trigger.old[i].pb__PurchaseListPrice__c <> null)
{
string a = string.valueof(trigger.old[i].pb__PurchaseListPrice__c);
string b = string.valueof(trigger.new[i].pb__PurchaseListPrice__c);
decimal old = decimal.valueof(a);
decimal newv = decimal.valueof(b);
inv.price_change__c = newv - old;
}
//update inv;
}
}



All Answers

Sonali BhardwajSonali Bhardwaj

 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.

 

trigger TrackPRICEupdate on pb__InventoryItem__c(before update) {
for(integer i=0; i<trigger.new.size(); i++){

if(trigger.new[i].pb__PurchaseListPrice__c <> null && trigger.old[i].pb__PurchaseListPrice__c <> null)
{
string a = string.valueof(trigger.old[i].pb__PurchaseListPrice__c);
string b = string.valueof(trigger.new[i].pb__PurchaseListPrice__c);
decimal old = decimal.valueof(a);
decimal newv = decimal.valueof(b);
inv.price_change__c = newv - old;
}
//update inv;
}
}



This was selected as the best answer
phantom1982phantom1982

Thanks for the reply.

 

I tested with your code and it gives the error: Variable doesn't exist: trigger.new

Sonali BhardwajSonali Bhardwaj

I have not compiled my code, it may be syntactically wrong, but logically its correct.

at which line its giving error?

phantom1982phantom1982

Resolved. Here is the final code.

trigger TrackPRICEupdate on pb__InventoryItem__c(before update) {

	for(pb__InventoryItem__c invnew: trigger.new){		
		if(trigger.new <> null && trigger.old <> null){
			pb__InventoryItem__c invold = trigger.oldMap.get(invnew.id);
		 	invnew.price_change__c = invnew.pb__PurchaseListPrice__c - invold.pb__PurchaseListPrice__c;
		}
	}
}

 Thanks Sonali