+ Start a Discussion
himanshu huske 7himanshu huske 7 

Callout is Not Working, Please correct

Hi mighty 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
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();
     HttpResponse response = http.send(request);
     if (response.getStatusCode() == 299) {
     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;
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){

Danish HodaDanish Hoda
Hi Himanshu,
May I ask why are you using batch class for this?
Please  check the debug log/apex job logs when this batch is run, you will get the error of what is restricting your logic.
himanshu huske 7himanshu huske 7
here is the scenerio
Danish HodaDanish Hoda
Hi Himanshu,
I got your requirement to use Batch apex, but you need to send some data to fetch the corresponding values and update the same in salesforce, here you are trying to insert every record.

I will suggest you to first test your code and take the response in a simple class, once done then implement it in batch
yash muley 18yash muley 18
Hi Himanshu , have you got the answer to this ? I am also stuck on the same question.