You need to sign in to do that
Don't have an account?
himanshu huske 7
Please correct my CallOut Code
Hi developers,
I m a beginner, please correct my code.
i have Country__c Object with all Text fields Name__c,Capital_City__c,Region__c,Sub_Region__c,Regional_Block__c,Country_ISO_Code_2__c,Country_ISO_Code_3__c from Country__c.
this is the Url for integration: https://restcountries.eu/
I want to create records for each country and populate all fiels in records for JSON
also provide testing code if possible
I m a beginner, please correct my code.
i have Country__c Object with all Text fields Name__c,Capital_City__c,Region__c,Sub_Region__c,Regional_Block__c,Country_ISO_Code_2__c,Country_ISO_Code_3__c from Country__c.
this is the Url for integration: https://restcountries.eu/
I want to create records for each country and populate all fiels in records for JSON
also provide testing code if possible
public class CountryNameWrapper{ public string name{get;set;} public string alpha2Code{get;set;} public string alpha3Code{get;set;} public string capital{get;set;} public string region{get;set;} public string subregion{get;set;} public List<CountryNameWrapper.RegionalBlocsWrapper> regionalBlocs{get;set;} public class RegionalBlocsWrapper{ public String acronym{get;set;} public String name{get;set;} public List<String> otherAcronyms{get;set;} public List<String> otherNames{get;set;} } } global class Load_Countries_RestApi implements Database.Batchable<sObject>,Database.AllowsCallouts { global String query ; global Database.QueryLocator start(Database.BatchableContext BC) { query = 'Select Name__c,Capital_City__c,Region__c,Sub_Region__c,Country_ISO_Code_2__c,Country_ISO_Code_3__c from Country__c'; //Query all countries from Country object return DataBase.getQueryLocator(query); } global void execute(Database.BatchableContext BC , List <Country__c> contlist) { Boolean hasChanged = false; Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint('https://restcountries.eu/rest/v2/all'); request.setMethod('GET'); HttpResponse response = http.send(request); if (response.getStatusCode() == 299) { System.debug('response--->'+(List<CountryNameWrapper>)JSON.deserialize(response.getBody(),List<CountryNameWrapper>.class)); List<CountryNameWrapper> abc = (List<CountryNameWrapper>)JSON.deserialize(response.getBody(),List<CountryNameWrapper>.class); List<Country__c> cntList = new List<Country__c>(); for(CountryNameWrapper cw : abc){ Country__c cnt = new Country__c(); cnt.Name__c = cw.name; cnt.Capital_City__c = cw.name; cnt.Region__c = cw.region; cnt.Sub_Region__c = cw.subregion; // cnt.Regional_Block__c = cw.regionalBlocs; cnt.Country_ISO_Code_2__c = cw.alpha2Code; cnt.Country_ISO_Code_3__c = cw.alpha3Code; cntList.add(cnt); } insert cntList; //Give call to https://restcountries.eu/rest/v2/name/AF //AF is country name. It will return you the iterate over the list //(List<CountryNameWrapper>) and see if the information in country is same //as that of object returned from API. If it is not same then set boolean //hasChanged to true. //If has changed is true, then update country object } } global void finish(Database.BatchableContext BC){ } }