Hi All,
Today's topic , Use of Future method in web services callout.
Consider one scenario, Whenever ,We create one Account record or Edit a Account record using UI, Populate Billing city based on Billing Zipcode . Like Below image before save,
After Press the save button,Billing city should auto populate.
We want to populate Billing city based on Billing zipcode using below API endpoint.
Postal PIN Code API
1. Get Post Office(s) details search by Postal PIN Code
http://postalpincode.in/api/pincode/{PINCODE}
Today's topic , Use of Future method in web services callout.
Consider one scenario, Whenever ,We create one Account record or Edit a Account record using UI, Populate Billing city based on Billing Zipcode . Like Below image before save,
We want to populate Billing city based on Billing zipcode using below API endpoint.
Postal PIN Code API
Postal PIN Code API allows developers to get details of Post Office by searching Postal PIN Code or Post Office Branch Name of India.
It has following format:
1. Get Post Office(s) details search by Postal PIN Code
http://postalpincode.in/api/pincode/{PINCODE}
2.Get Post Office(s) details search by Post Office branch name
http://postalpincode.in/api/postoffice/{POSTOFFICEBRANCHNAME}
http://postalpincode.in/api/postoffice/{POSTOFFICEBRANCHNAME}
Postal PIN Code API returns the response in JSON format. "Status" field in response is set to SUCCESS or ERROR, "Message" field will return message against the request and "PostOffice" field will return data.
Example 1 (Search by Postal PIN Code):
To get details of a PIN Code (110001), use following query
Response For Valid PIN Codes:
{"Message":"Number of Post office(s) found: 21","Status":"Success","PostOffice":[{"Name":"Baroda House","Description":"","BranchType":"Sub Post Office","DeliveryStatus":"Non-Delivery","Taluk":"New Delhi","Circle":"New Delhi","District":"Central Delhi","Division":"New Delhi Central","Region":"Delhi","State":"Delhi","Country":"India"}]}
{"Message":"Number of Post office(s) found: 21","Status":"Success","PostOffice":[{"Name":"Baroda House","Description":"","BranchType":"Sub Post Office","DeliveryStatus":"Non-Delivery","Taluk":"New Delhi","Circle":"New Delhi","District":"Central Delhi","Division":"New Delhi Central","Region":"Delhi","State":"Delhi","Country":"India"}]}
Response For Invalid PIN Codes or Error messages:
{"Message":"No records found","Status":"Error","PostOffice":null}
{"Message":"No records found","Status":"Error","PostOffice":null}
-------------
Link:- http://postalpincode.in/Api-Details
When making a callout from a method, the method waits for the external service to send back the callout response before executing subsequent lines of code. Alternatively, you can place the callout code in an asynchronous method that’s annotated with @future(callout=true) or use Queueable Apex. This way, the callout runs on a separate thread, and the execution of the calling method isn’t blocked.
When making a callout from a trigger, the callout must not block the trigger process while waiting for the response. For the trigger to be able to make a callout, the method containing the callout code must be annotated with @future(callout=true) to run in a separate thread
Now let us check the coding section,
1. Trigger on Account object.
//Why to check System.isFutute and System.isBatch
//Futute method cannot be call from another future method. After update trigger will call future method from future method , result will error "CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountTrigger: execution of AfterInsert caused by: System.AsyncException: Future method cannot be called from a future or batch method".
// Update the trigger logic to leverage the System.isFuture() and System.isBatch() calls so that the future method invocation is not made if the current execution context is future or batch. Record created through such cases should be handled separately using a scheduled batch job or Replace the future method with Queueable Apex.
trigger Pincode_Address on Account (after insert,after update) {
if(!System.isFuture() && !System.isBatch()){
for(Account acc:trigger.new){
Apex_callout.callcount(acc.BillingPostalCode,acc.Id);
}
}
}
2. Now call the End point URL using @future callout by passing the zipcode.
public class Apex_callout {
@future(callout=true)
Public static void callcount(string zipcode,id ids){
Http http=new Http();
HttpRequest req=new HttpRequest();
req.setEndpoint('http://postalpincode.in/api/pincode/'+zipcode);
req.setMethod('GET');
HttpResponse res=http.send(req);
system.debug('--->>'+res);
if(res.getStatus()=='OK' && res.getStatusCode()==200){
//To deserialize the JSON body , created a class pincode
pincode pc=(pincode)JSON.deserialize(res.getBody(), pincode.class);
Account accd=[select id,BillingCity from Account where id=:ids limit 1];
system.debug('---->>>++'+accd.BillingCity);
if(accd.BillingCity==''||accd.BillingCity==Null){
accd.BillingCity=pc.PostOffice[0].Circle;
update accd;
}
}
}
}
3. To deserialize the JSON ,created the below class.Which you can create using https://json2apex.herokuapp.com/ . Just you need to paste the Json string and it will convert to apex class.
public class pincode {
public String Message;
public String Status;
public List<PostOffice> PostOffice;
public class PostOffice {
public String Name;
public String Description;
public String BranchType;
public String DeliveryStatus;
public String Taluk;
public String Circle;
public String District;
public String Division;
public String Region;
public String State;
public String Country;
}
Find a pincode is a finest Indian pin code search engine provides information about Delhi pin code, Pin code Bangalore pin code, Pune pin code and all India post offices, pin codes, localities.
ReplyDelete