Quantcast
Channel: How do I use REST API on a Trigger to populate account fields - Salesforce Stack Exchange
Viewing all articles
Browse latest Browse all 2

How do I use REST API on a Trigger to populate account fields

$
0
0

I'm new to working with API's and code in general, I have been challenged to create an API to collect data to input into Salesforce records, this will be invoked by a trigger.

Currently, I'm running the tests on the Open Weather Map API.

I'm preforming the callout as a 'GET' Method and returning the response which I deserialize into variables that I would like to put into the Account record.

Code Example

trigger RedTrigger on Account (before Update) {
    Public String ShowThisResult = null;
    @future(callout=true)
   public static void GetAPIAsyn ()
    {

        Http http=new Http();
        HttpRequest request=new HttpRequest();
        request.setEndPoint('https://api.openweathermap.org/data/2.5/weather?q=Ulverston&APPID=6a4199d8698bb67cc604e3fb8082599d');
        request.setMethod('GET');
        HttpResponse response=http.send(request);

        if (response.getStatusCode() == 200) {
            ShowThisResult = response.getBody();
            Map<String,Object> results=(Map<String,Object>)JSON.deserializeUntyped(response.getBody());
            String.valueOf(results.get('weather'));
            System.debug(ShowThisResult);
            System.debug(response);
            System.debug(response.getStatusCode());
        }

        else {
            System.debug('The status code returned was not expected: ' +
                response.getStatusCode() + ' ' + response.getStatus());

            }

}
    for (Account a : Trigger.new) {
        a.value1__c = ShowThisResult;
        }
}

I have tried doing it as separate classes using @future(callout=true) but I can't get it working for me.

Here are the fields on Account that I'm trying to populate with the collected values-

Populate Thes Fields On Account

It seems 'ShowThisResult' isn't populating because when I run this on account it pulls through as a blank value. Any pointers/guidance will be greatly appreciated thank you.


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>