Updating multi currency rates from Apex

I just looked in the Salesforce.com documentation for a way to update currency rates using Apex, but I have a vague feeling that this is not possible using standard features. I noticed that a couple of packages on AppExchange are doing this for free, but I was looking to integrate our own module since we use a different rate provider. I guess that it is related to DatedConversionRate (detailed here)

What is the best way to achieve this? I guess it’s doable using a workaround such as calling the Metadata API, but not sure how to implement it. Any sample/example?

Thanks!

Answer

The Metadata API does not give access to this information, this is used for org configuration such as custom objects and fields. CurrencyType and DatedConversionRate contain record level information. As per the documentation you linked to, you can use the SOAP API to insert and udpate records in these.

All standard and custom objects can also be accessed through the SOAP API.

Which I also assume would mean the REST API aswell and indeed it does…

Here is a working REST API insert via Apex for DatedConversionRate

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v28.0/sobjects/DatedConversionRate/');
    req.setBody('{ "IsoCode" : "ADP", "ConversionRate" : 1.5, "StartDate" : "2013-06-26" }');
    req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
    req.setHeader('Content-Type', 'application/json');
    req.setMethod('POST');
    HttpResponse res = h.send(req);

Here is a working REST API update via Apex for DatedConversionRate

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v28.0/sobjects/DatedConversionRate/04wb0000000KzHlAAK?_HttpMethod=PATCH');
    req.setBody('{ "ConversionRate" : 2.5 }');
    req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
    req.setHeader('Content-Type', 'application/json');
    req.setMethod('POST');
    HttpResponse res = h.send(req);

Note: Using setMethod(‘PATCH’) gives an Apex error. The documentation for HttpRequest.setMethod does not list ‘PATCH’ as a method, though it says ‘for example’, so its unclear if its saying its not supported or the documentation author just missed it out.

Fortunately you can override the Http method as a URL parameter as described here.

If you use an HTTP library that doesn’t allow overriding or setting an arbitrary HTTP method name, you can send a POST request and provide an override to the HTTP method via the query string parameter _HttpMethod. In the PATCH example, you can replace the PostMethod line with one that doesn’t use override: PostMethod m = new PostMethod(url + “?_HttpMethod=PATCH”);

Attribution
Source : Link , Question Author : jpmonette , Answer Author : Community

Leave a Comment