public class AccountIntegrationHelper {
@future(callout=true)
public static void populateShippingAddress(List<Id> accountIds) {
List<Account> accountsToUpdate = [SELECT Id, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry
FROM Account
WHERE Id IN :accountIds];
for (Account acc : accountsToUpdate) {
// Example payload with Billing Address data
String payload = JSON.serialize(new Map<String, Object>{
'accountId' => acc.Id,
'billingStreet' => acc.BillingStreet,
'billingCity' => acc.BillingCity,
'billingState' => acc.BillingState,
'billingPostalCode' => acc.BillingPostalCode,
'billingCountry' => acc.BillingCountry
});
// Set up HTTP request
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://external-api.example.com/populateShippingAddress');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setBody(payload);
try {
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
// Update the Shipping Address in Salesforce based on the response
Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
// Assuming the response contains the shipping address fields
acc.ShippingStreet = (String) responseMap.get('shippingStreet');
acc.ShippingCity = (String) responseMap.get('shippingCity');
acc.ShippingState = (String) responseMap.get('shippingState');
acc.ShippingPostalCode = (String) responseMap.get('shippingPostalCode');
acc.ShippingCountry = (String) responseMap.get('shippingCountry');
}
} catch (Exception e) {
System.debug('Error in HTTP Callout: ' + e.getMessage());
}
}
if (!accountsToUpdate.isEmpty()) {
update accountsToUpdate;
}
}
}
Comments