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
KirstyFranceKirstyFrance 

Trigger functions on standard but not on custom object

I am not a programmer but have been learning to write some basic triggers to improve our Salesforce implementation. I have been obliged to replace the standard Asset object with a custom object that is a child object to accounts. A trigger written for the standard object has been recreated for the custom object. It worked perfectly on the standard object but does not on the custom.

 

The trigger is to set the Currency Iso Code of the object based on that of the related account. Does anyone know if there would be slightly different syntax for a custom object trigger? In the below I replaced Asset with the name of the custom object. Everything else is the same.

 

 

trigger SetPricePlanCurrency on Price_Plan__c (before update) { for (Price_Plan__c a : Trigger.new) { if(a.Set_Billing_Currency__c== 'EUR'){a.CurrencyIsoCode = 'EUR' ;} if(a.Set_Billing_Currency__c== 'CAD'){a.CurrencyIsoCode = 'CAD' ;} if(a.Set_Billing_Currency__c== 'CHF'){a.CurrencyIsoCode = 'CHF' ;} if(a.Set_Billing_Currency__c== 'DKK'){a.CurrencyIsoCode = 'DKK' ;} if(a.Set_Billing_Currency__c== 'GBR'){a.CurrencyIsoCode = 'GBP' ;} if(a.Set_Billing_Currency__c== 'NOK'){a.CurrencyIsoCode = 'NOK' ;} if(a.Set_Billing_Currency__c== 'SEK'){a.CurrencyIsoCode = 'SEK' ;} if(a.Set_Billing_Currency__c== 'USD'){a.CurrencyIsoCode = 'USD' ;} } }

 

 

 

dmchengdmcheng

No, there are no differences in syntax.  I don't have an answer for you, but I'm wondering if you can't simply your code.  It seems like you want the CurrencyISOCode to be the exact same value as the Billing Currency, so you could do this:

 

 

trigger SetPricePlanCurrency on Price_Plan__c (before update) { for (Price_Plan__c a : Trigger.new) { a.CurrencyIsoCode = a.Set_Billing_Currency__c; } }

 

 

unless you only want this to happen for specific currencies, in which case:

 

 

trigger SetPricePlanCurrency on Price_Plan__c (before update) { String ourCurrencies = 'EUR CAD CHF DKK GBP NOK SEK USD'; for (Price_Plan__c a : Trigger.new) { if(ourCurrencies.contains(a.Set_Billing_Currency__c) a.CurrencyIsoCode = a.Set_Billing_Currency__c;

} }

 

And you can append new currencies to the ourCurrencies string as needed in the future.

 

KirstyFranceKirstyFrance

Thanks, that is much simpler! :smileyhappy: I have changed before update to before insert and it is now working. Still confused as to why it would work on my standard object as it was before and not on my custom one.

 

Thank you! 

dmchengdmcheng
Oh.  Update triggers fire only for existing records and when you make changes to those records.  Insert triggers fire for new records only.  Is that why you weren't seeing any results, because you're inserting records?
KirstyFranceKirstyFrance
In both cases I was creating new records linked to existing accounts. I guess to be sure I can have both update and insert?
dmchengdmcheng

Yes, your trigger line should read

 

 

trigger SetPricePlanCurrency on Price_Plan__c (before insert, before update)

 

 

 

KirstyFranceKirstyFrance
Thanks for your help!
BobBob

I am working on a standard object (Case), but i need to change the currency on the case if a customer service rep enters an account with a shipping country of IRL.  

 

I tweaked your code and tried to make it work but nothing happens. Please see code below, and offer any advice.

 

 

 

 

trigger CurrencyIsoCode on Case (before insert, before update)
{
   for (Case c:Trigger.new)
    {
        if (c.Account.Shipping_Country__c =='IRL') c.CurrencyIsoCode = 'EUR';
    }
}

 

trigger CurrencyIsoCode on Case (before insert, before update)

{ for (Case c:Trigger.new)

 {  if (c.Account.Shipping_Country__c =='IRL') c.CurrencyIsoCode = 'EUR';    }

}