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
Kunal Purohit 4Kunal Purohit 4 

How to write trigger for REST API?

Hello All,
I am having two Fields ZipCode__c and TimeZone__c, Now whenever user enters ZipCode__c, TimeZone__c should be updated accordingly. Here I have to use REST API for ZipCode. I am new to salesforce. Please help to write the trigger for same.
Thanks.

Shri RajShri Raj

Try the below

 

trigger UpdateTimezone on Account (after insert) {
    // Get the zipcode from the inserted records
    Set<String> zipcodes = new Set<String>();
    for (Account acc : Trigger.new) {
        zipcodes.add(acc.ZipCode__c);
    }

    // Call the REST API to get the timezone information for the zipcodes
    Map<String, String> zipcodeToTimezone = getTimezoneFromAPI(zipcodes);

    // Update the timezone field on the records
    for (Account acc : Trigger.new) {
        acc.Timezone__c = zipcodeToTimezone.get(acc.ZipCode__c);
    }

    update Trigger.new;
}

private static Map<String, String> getTimezoneFromAPI(Set<String> zipcodes) {
    // Code to call the REST API and parse the response to get the timezone information
}