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
BreandanAnseoBreandanAnseo 

Triggers & CurrencyType

Hi,

I am trying to update the CurrencyType object using a Trigger but recently found out that DML can not be used to update it. I've tested updating CurrencyType using the API & Flex so am able to do this from a Page for example. Is there any way a Trigger can update the CurrencyType object without using an externally hosted webservice? Can it load a Page in the background or something?

Waiting in fear of the answer ;o)

Thanks
Breandán

PS is there an Idea to allow DML to update all objects?
aalbertaalbert

Here is a list of objects that can't perform DML operations in Apex. 

soofsoof

BreandanAnseo wrote:
Is there any way a Trigger can update the CurrencyType object without using an externally hosted webservice? Can it load a Page in the background or something?

 

 

The only way that I can think of for a trigger to load a page is via calling a method with future annotation.  You will need something like the following code

public class FutureTest {

 

@future

public static void callPage() {

PageReference pr = new PageReference('<URL_OF_INTERNAL PAGE>');

pr.getContent();

}

}

 

The problem with this approach is that you can issue "No more than 200 method calls per Salesforce license per 24 hours" (i.e. methods with future annotation).  I'm hoping that you won't need an update/insert of CurrencyType very frequently, so this should work.

 

Hope this helps!

 

-soof 

 

BreandanAnseoBreandanAnseo

Hi soof,

 

Thanks for the suggestion but the apex pdf (v16) says: "The getContent method for PageReference objects is not allowed in triggers." so doesn't look like that would work. What I would need to do is be able to run a flex resource on the page being called from the trigger so this would probably need the page to actually open in the browser? Sounds like this is not possible from a (server side) trigger, right? Any other ideas about how a trigger might update CurrencyType?

 

Thanks for link aalbert, that will be useful. Seems to be a lot of objects that can't be updated from Apex - wonder why that is when at least some of them can be updated from the API.

 

Thanks

Breandán

soofsoof

BreandanAnseo wrote:

"The getContent method for PageReference objects is not allowed in triggers." so doesn't look like that would work.




That's correct, and that is exactly the reason why I used the 'future' annotation.  If the getContent() method is being called in a method that has 'future' annotation, it works!  Try it; it worked for me!

 

Again, the only issue that I see with this approach is that how often you would want to call this 'future' method.  Salesforce places a limit of 200 future method calls per 24 hours.

 

As for opening the page in the browser, it is not possible via trigger.  That said, I think you can get the desired result by calling the getContent() method.

 

One more thing, can you tell me where you want this trigger to be invoked from?  Do you want it to fire when you do a DML operation via Native UI or other (Apex/API/DataLoader)?  Because if you want currency type to be updated after an operation on Salesforce Native UI, then you can accomplish that by overriding the saveURL query string parameter.

 

-soof 

BreandanAnseoBreandanAnseo

Hi soof,

 

The frequency should be ok. I'm using a time-dependent workflow to update a field that fires the trigger. Then the trigger should update the CurrencyType object and re-create another record for the workflow to work off again on another date. The idea is to allow currencies to be updated on a recurring basis - eg daily, weekly (but no more frequent than that). Would the saveURL work in this case?

 

Will try the getContent. Thanks for the creative ideas!

 

Breandán

soofsoof

The saveURL query string parameter won't work in this case - it works only via standard button overriding and in the native UI environment (e.g. the standard Account edit screen in the browser).

 

 -soof 

BreandanAnseoBreandanAnseo

Hi soof,

 

I've tested using the getContent method as suggested but it doesn't appear to be working. I have a page with a flex control which makes a simple update to a row in the CurrencyType object. When I open this in the typical way as a tab it works fine and updates the object. However if I open this page using @Future with getContent it doesn't make any update. Maybe the flex control doesn't get loaded in getContent? I noticed as well though that the system.debug I have on the custom page controller also doesn't fire. When you had this working yourself were you using flex? The output of the getContent looks a bit unusual as well:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"> <script> if (window.location.replace){ window.location.replace('https://login.salesforce.com/?ec=302&startURL=%2Fvisualforce%2Fsession%3Furl%3Dhttps%253A%252F%252Fc.na5.visual.force.com%252Fapex%252FtrueRates%253Finline%253D1%2526core.apexpages.devmode.url%253D1'); } else {; window.location.href ='https://login.salesforce.com/?ec=302&startURL=%2Fvisualforce%2Fsession%3Furl%3Dhttps%253A%252F%252Fc.na5.visual.force.com%252Fapex%252FtrueRates%253Finline%253D1%2526core.apexpages.devmode.url%253D1'; } </script> </head> </html> <!-- ................................................................................................... ................................................................................................... ................................................................................................... ................................................................................................... -->

 

Breandán