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
sapsap 

Problem while updating CurrencyIsoCode field in Triggers

Hi,
 
My organisation have enabled multi currencies.Im trying to update CurrencyIsoCode field of a custom object based on some condition.Trigger is getting saved but while testing the code it is throwing an Exception
 
This is the code im using
 
trigger PRCurrencyUpdate on Product_Request__c (before insert,before update)
{
 
Inventory__c inv =[Select currencyIsoCode from Inventory__c where Id= :Trigger.new[0].inventory_Name__c];
double currencycode=inv.currencyIsoCode;
Product_Request__c pr=[Select Id,currencyIsoCode from Product_Request__c where Id=:Trigger.new[0].Id];
pr.currencyIsoCode=currencycode;
update pr;
}
 
 
 
Can any one please help me resolving this issue.
Any pointers will be of great help.
Thanks in advance.
micwamicwa
currencyIsoCode is of type string, shouldn't it be:

String currencycode=inv.currencyIsoCode;

It might work now, if not you should post the exception message.
sapsap
Hi,
 
Thanks for your reply.
 
I'm getting the following error.
 
Apex trigger PRCurrencyUpdate caused an unexpected exception, contact your administrator: PRCurrencyUpdate: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0US00000008OYwMAM; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a0US00000008OYw) is currently being updated in trigger PRCurrencyUpdate: [Id]: Trigger.PRCurrencyUpdate: line 7, column 1
 
 
micwamicwa
I don't really know what it is but I found that, it might help:

SELF_REFERENCE_FROM_TRIGGER
You cannot recursively update or delete the same object from an Apex trigger. This error often occurs when:
  • You try to update or delete an object from within its before trigger.
  • You try to delete an object from within its after trigger.

This error occurs with both direct and indirect operations. The following is an example of an indirect operation:

  1. A request is submitted to update Object A.
  2. A before update trigger on object A creates an object B.
  3. Object A is updated.
  4. An after insert trigger on object B queries object A and updates it. This is an indirect update of object A because of the before trigger of object A, so an error is generated.

thought_currythought_curry

I think you are trying to update the record (statement update pr) in the before update trigger which is causing you the exception.

Try removing that statement and check.