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
psoheilpsoheil 

Trigger fails to update related records

Hello All,

 

I am fairly new to Apex and Force.com in general. I have been going through the Force.com Workbook (the one that comes with Eclipse Force IDE Help File) which has an example Adding Apex Trigger, with the following code:

 

trigger HandleProductPriceChange on Merchandise__c (after undelete) 
{
	// update invoice line items associated with open invoices
	List openLineItems = 
		[SELECT j.Unit_Price__c, j.Merchandise__r.Price__c
		 FROM Line_Item__c j 
		 WHERE j.Invoice_Statement__r.Status__c = 'Negotiating'  
		 AND j.Merchandise__r.id IN :Trigger.new 
	 	 FOR UPDATE]; 
	 	 
	for (Line_Item__c li: openLineItems)
	{
		if (li.Merchandise__r.Price__c < li.Unit_Price__c)
		{
			li.Unit_Price__c = li.Merchandise__r.Price__c;
		}
	}
	
	update openLineItems;
}

The trigger saves and even shows up on the Web Interface under Triggers for the Merchandise Object. But when I change the price of the item, the line item never updates the amount. 

 

Can anyone tell me what I am missing? Everything else seems to be working like a charm.

 

Thanks,

Pedram

Best Answer chosen by Admin (Salesforce Developers) 
MJ09MJ09

Your trigger is defined with "after undelete," so it will fire only when a record is being undeleted. If you want it to fire when a record is changed, you'll need to add either "before update" or "after update."

All Answers

MJ09MJ09

Your trigger is defined with "after undelete," so it will fire only when a record is being undeleted. If you want it to fire when a record is changed, you'll need to add either "before update" or "after update."

This was selected as the best answer
psoheilpsoheil

Yep, that was the problem. I changed it to after update and it works fine now.

 

Something else that I have noticed is that when I was trying to look at the execution log to see what the problem was, none of the logs contain any entries. Is there a way to see actual trigger execution log somewhere regardless of if it was successful or generated error?

 

Thanks,

Pedram

JPClark3JPClark3

Click on the "System Log" link at the top left of you browser, when logged into your ORG. You can paste some code into the window to query and update your object. The debug window will display your results.

 

Use "System.Debug(any);" throughout your code to display variable values.

psoheilpsoheil
Hello JP, I am using the web interface to view the System Log. The Apex function call you posted does not show any log entries (it just generates an error when I execute it). Thanks, Pedram
horoshhorosh

Hallo, Im have a problem. My code

 

trigger HandleProductPriceChange on Merchandise__c (after update) {

 

List<Line_Item__c> openLineItems =
   [SELECT j.Unit_Price__c, j.Merchandise__r.Price__c
   FROM Line_Item__c j
   WHERE j.Invoice_Statement__r.Status__c = 'Negotiating'
   AND j.Merchandise__r.id IN :Trigger.new
   FOR UPDATE];

}

I use eclipse and Force.com IDE.

 

The reference  j.Invoice_Statement__r.Status__c don't work. The code completer show me j.invoice_Statement__r and I can't see Status__c attribut. Error: "Save error: Didn't understand relationship 'Unit_Price__c' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.    Force.com save problem"

 

Can you help me please? Thank