+ Start a Discussion
Iqra TechIqra Tech 

how can i convert my HTTP Callout class into the Batch class

I am  having Http callout class to post the data in third party class is working perfctly according to my requirement , but the thing as i am new on Devlopment so i need to convert this class into the batch class how can i convert this can any please help me i am just stuck on this.........
Best Answer chosen by Iqra Tech
Rajesh_KumarRajesh_Kumar
public class Batch_API_New implements Database.Batchable<sObject>, Database.AllowsCallouts{
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'select Id,Edition__c,Object__c,Opportunity__c,Opportunity_Id__c,Stage__c,Banner_Name__c,Stand_no__c,Edition_Event_Name__c,Additional_field_3__c,' +
            'Additional_field_2__c,Type_of_Stand__c,Customer_Product__c,Additional_field_4__c,Additional_field_1__c,Contractual_Sq_M__c,Hall__c,Height__c,Width__c,' +
            'Open_Sides__c,Additional_field_6__c,Additional_field_5__c,Phone__c,Website__c,Fax__c,PO_Box__c,Floor_Plan_Stage__c from ' + 
            'Floor_Plan_Mapping__c order by createdDate DESC'
        );
    }
    public void execute(Database.BatchableContext bc, List<sObject> records){
        // process each batch of records
		Insert_Records(records);
    }    
    public void finish(Database.BatchableContext bc){
        
    }    
	Public void Insert_Records(List<Floor_Plan_Mapping__c> LstFlrmapping){  
		String query= 'select ID,' ;
       if(LstFlrmapping[0].Edition__c!=null){
           query += LstFlrmapping[0].Edition__c+',' ; 
       }
       if(LstFlrmapping[0].Opportunity__c!=null){
           query += LstFlrmapping[0].Opportunity__c+',' ; 
       }
       if(LstFlrmapping[0].Opportunity_Id__c!=null){
           query += LstFlrmapping[0].Opportunity_Id__c+',' ; 
       }
       if(LstFlrmapping[0].Stage__c!=null){
           query += LstFlrmapping[0].Stage__c+',' ; 
       }
       if(LstFlrmapping[0].Banner_Name__c!=null){
           query += LstFlrmapping[0].Banner_Name__c+',' ; 
       }
       if(LstFlrmapping[0].Stand_no__c!=null){
           query += LstFlrmapping[0].Stand_no__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_3__c!=null){
           query += LstFlrmapping[0].Additional_field_3__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_2__c!=null){
           query += LstFlrmapping[0].Additional_field_2__c+',' ; 
       }
        if(LstFlrmapping[0].Type_of_Stand__c!=null){
           query += LstFlrmapping[0].Type_of_Stand__c+',' ; 
       }
        if(LstFlrmapping[0].Customer_Product__c!=null){
           query += LstFlrmapping[0].Customer_Product__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_4__c!=null){
           query += LstFlrmapping[0].Additional_field_4__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_1__c!=null){
           query += LstFlrmapping[0].Additional_field_1__c+',' ; 
       }
        if(LstFlrmapping[0].Contractual_Sq_M__c!=null){
           query += LstFlrmapping[0].Contractual_Sq_M__c+',' ; 
       }
        if(LstFlrmapping[0].Hall__c!=null){
           query += LstFlrmapping[0].Hall__c+',' ; 
       }
        if(LstFlrmapping[0].Height__c!=null){
           query += LstFlrmapping[0].Height__c+',' ; 
       }
        if(LstFlrmapping[0].Width__c!=null){
           query += LstFlrmapping[0].Width__c+',' ; 
       }
        if(LstFlrmapping[0].Open_Sides__c!=null){
           query += LstFlrmapping[0].Open_Sides__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_6__c!=null){
           query += LstFlrmapping[0].Additional_field_6__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_5__c!=null){
           query += LstFlrmapping[0].Additional_field_5__c+',' ; 
       }
        if(LstFlrmapping[0].Phone__c!=null){
           query += LstFlrmapping[0].Phone__c+',' ; 
       }
        if(LstFlrmapping[0].Website__c!=null){
           query += LstFlrmapping[0].Website__c+',' ; 
       }
        if(LstFlrmapping[0].Fax__c!=null){
           query += LstFlrmapping[0].Fax__c+',' ; 
       }
        if(LstFlrmapping[0].PO_Box__c!=null){
           query += LstFlrmapping[0].PO_Box__c+',' ; 
       } 
        if(LstFlrmapping[0].Floor_Plan_Stage__c!=null){
           query += LstFlrmapping[0].Floor_Plan_Stage__c+',' ; 
       }     
       
       query = query.removeEnd(','); 
       query += ' from '+LstFlrmapping[0].Object__c+' where '+LstFlrmapping[0].Edition__c+' = \''+LstFlrmapping[0].Edition_Event_Name__c +'\''; 

      String Edition=LstFlrmapping[0].Edition__c;
      String Opportunity=LstFlrmapping[0].Opportunity__c;
      String OpprtunityId=LstFlrmapping[0].Opportunity_Id__c;
      String Objects=LstFlrmapping[0].Object__c;
      String stage=LstFlrmapping[0].Stage__c;
      String BannerName=LstFlrmapping[0].Banner_Name__c;
      String StandNo=LstFlrmapping[0].Stand_no__c;
      String Edition_ID=LstFlrmapping[0].Additional_field_3__c;
      String Stand_Sub_Allocation_Name=LstFlrmapping[0].Additional_field_2__c;
      String Type_of_Stand=LstFlrmapping[0].Type_of_Stand__c;
      String Customer_Product=LstFlrmapping[0].Customer_Product__c;
      String Customer_Product_category=LstFlrmapping[0].Additional_field_4__c;
      String Product_Sub_Category=LstFlrmapping[0].Additional_field_1__c;
      String Contractual_Sq_M=LstFlrmapping[0].Contractual_Sq_M__c;
      String Hall=LstFlrmapping[0].Hall__c;
      String Height=LstFlrmapping[0].Height__c;
      String Width=LstFlrmapping[0].Width__c;
      String Open_Sides=LstFlrmapping[0].Open_Sides__c;
      String Street=LstFlrmapping[0].Additional_field_6__c;
      String City=LstFlrmapping[0].Additional_field_5__c;
      String Phone=LstFlrmapping[0].Phone__c;
      String Web_Sites=LstFlrmapping[0].Website__c;
      String Fax=LstFlrmapping[0].Fax__c;
      String PO_BOX=LstFlrmapping[0].PO_Box__c;
      String Floor_Plan_Stage = LstFlrmapping[0].Floor_Plan_Stage__c;  
      System.debug('Query formed----> '+query); 

     List<sObject> sc= Database.query(query);
     //System.debug('All Records -->'+sc);
       
     List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
     for(sObject  s : sc) 
     {
       system.debug('Cheking Object ssss ---->'+s);
       //OpportunityLineItem o = (OpportunityLineItem)s ;
        //sObject o =  (sObject)s.get(LstFlrmapping[0].Object__c);
        //sObject o = ((LstFlrmapping[0].Object__c != null) ? (Sobject)s.get(LstFlrmapping[0].Object__c) : null);
       sObject o = (sObject)s;

        system.debug('Cheking Object---->'+o);
         try
         {
         
     //OppWrapper oppwrappervar=new OppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);
     OppWrapper oppwrappervars = new OppWrapper(
    (LstFlrmapping[0].Edition__c != null) ? (string)o.get(LstFlrmapping[0].Edition__c) : null,
    (LstFlrmapping[0].Opportunity_Id__c != null) ? (string)o.get(LstFlrmapping[0].Opportunity_Id__c) : null,
    (LstFlrmapping[0].Opportunity__c != null) ? (string)o.get(LstFlrmapping[0].Opportunity__c) : null,
    (LstFlrmapping[0].Stage__c != null) ? (string)o.get(LstFlrmapping[0].Stage__c) : null,
    (LstFlrmapping[0].Banner_Name__c != null) ? (string)o.get(LstFlrmapping[0].Banner_Name__c) : null,
    (LstFlrmapping[0].Stand_no__c != null) ? (string)o.get(LstFlrmapping[0].Stand_no__c) : null,
    (LstFlrmapping[0].Additional_field_3__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_3__c) : null,
    (LstFlrmapping[0].Additional_field_2__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_2__c) : null,
    (LstFlrmapping[0].Phone__c != null) ? (string)o.get(LstFlrmapping[0].Phone__c) : null,
    (LstFlrmapping[0].Website__c != null) ? (string)o.get(LstFlrmapping[0].Website__c) : null,
    (LstFlrmapping[0].Additional_field_5__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_5__c) : null,
    (LstFlrmapping[0].Additional_field_6__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_6__c) : null,
    (LstFlrmapping[0].PO_Box__c != null) ? (string)o.get(LstFlrmapping[0].PO_Box__c) : null,
    (LstFlrmapping[0].Customer_Product__c != null) ? (string)o.get(LstFlrmapping[0].Customer_Product__c) : null,      
    (LstFlrmapping[0].Additional_field_4__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_4__c) : null,
    (LstFlrmapping[0].Fax__c != null) ? (string)o.get(LstFlrmapping[0].Fax__c) : null,
    (LstFlrmapping[0].Additional_field_1__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_1__c) : null,
    (LstFlrmapping[0].Hall__c != null) ? (string)o.get(LstFlrmapping[0].Hall__c) : null,
    (LstFlrmapping[0].Height__c != null) ? (string)o.get(LstFlrmapping[0].Height__c) : null,
    (LstFlrmapping[0].Width__c != null) ? (string)o.get(LstFlrmapping[0].Width__c) : null,
    (LstFlrmapping[0].Contractual_Sq_M__c != null) ? (Decimal)o.get(LstFlrmapping[0].Contractual_Sq_M__c) : null,
    (LstFlrmapping[0].Open_Sides__c != null) ? (string)o.get(LstFlrmapping[0].Open_Sides__c) : null,
    (LstFlrmapping[0].Type_of_Stand__c != null) ? (string)o.get(LstFlrmapping[0].Type_of_Stand__c) : null);
       oppWrapperVarList.add(oppwrappervars);
         }catch(Exception ex)
         {
             System.debug('================'+ex.getMessage()+'======================'+ex);
         }
        
     }
        string jsonbody=Json.serialize(oppWrapperVarList);
        system.debug('Cheking API---->'+oppWrapperVarList);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setTimeout(25000);
        //request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setEndpoint('http://sfdata.iqratechnology.com/api/data');

        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
     
        request.setBody(jsonbody);
        
       HttpResponse response = http.send(request);
        system.debug('Response body '+ response.getBody());
     if (response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
      } else {
		System.debug(response.getBody());
		}	
    }
	public class OppWrapper
    {
        public String EventName {get;set;}
        public String RecordId {get;set;}
        public String RecordName {get;set;}
        public String RecordStage {get;set;}
        public String StandNo {get;set;}
        public String BannerName {get;set;}
        public String Edition_ID {get;set;}
        public String Stand_Sub_Allocation_Name {get;set;}
        public String Phone {get;set;}
        public String  Web_Site {get;set;}
        public String  City {get;set;}
        public String  Street {get;set;}
        public String  PO_BOX {get;set;}
        public String  Customer_Product {get;set;}
        public String  Customer_Product_category {get;set;}
        public String  Fax {get;set;}
        public String  Product_Sub_Category {get;set;}
        public String  Hall {get;set;}
        public String  Height {get;set;}
        public String  Width {get;set;}
        public Decimal  Contractual_Sq_M {get;set;}
        public String  Open_Sides {get;set;}
        public String  Type_of_Stand {get;set;}
        public OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String BannerName,String StandNo,String Edition_ID,
                          String Stand_Sub_Allocation_Name,String Phone,String Web_Site,String City,String Street,String PO_BOX,String Customer_Product,
                          String Customer_Product_category,String Fax,String Product_Sub_Category,String Hall,String Height,String Width,Decimal Contractual_Sq_M,
                         String Open_Sides,String Type_of_Stand)
                          
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            this.Edition_ID=Edition_ID;
            this.Stand_Sub_Allocation_Name=Stand_Sub_Allocation_Name;
            this.Phone=Phone;
            this.Web_Site=Web_Site;
            this.City=City;
            this.Street=Street;
            this.PO_BOX=PO_BOX;
            this.Customer_Product=Customer_Product;
            this.Customer_Product_category=Customer_Product_category;
            this.Fax=Fax;
            this.Product_Sub_Category=Product_Sub_Category;
            this.Hall=Hall;
            this.Height=Height;
            this.Width=Width;
            this.Contractual_Sq_M=Contractual_Sq_M;
            this.Open_Sides=Open_Sides;
            this.Type_of_Stand=Type_of_Stand;
        } 
    }
}
Don't forget to run the batch with the batch_size 1. Database.executeBatch(new Batch_API_New (),1);

All Answers

Rajesh_KumarRajesh_Kumar
Hi Iqra,
Please make sure you are implementing Database.AllowsCallouts interface in your batch class. 
public class myBatchClass implements Database.Batchable<sObject>, Database.AllowsCallouts{

}

Remember you cannot call the @future method from batch class.
Iqra TechIqra Tech
@Rajesh_Kumar thanks sir if u dont mind can i share with you class 
can you look into that and make batch class it will be helpful for me as in my class insert method are used so i am unable to convert into the batch class can you please ..........


public class NEW_API_CLASSES {
     Public static void Insert_Records()
    {           
      List<Floor_Plan_Mapping__c> LstFlrmapping=new list< Floor_Plan_Mapping__c>();
      LstFlrmapping=[select Id,Edition__c,Object__c,Opportunity__c,Opportunity_Id__c,Stage__c,Banner_Name__c,Stand_no__c,Edition_Event_Name__c,Additional_field_3__c,
                     Additional_field_2__c,Type_of_Stand__c,Customer_Product__c,Additional_field_4__c,Additional_field_1__c,Contractual_Sq_M__c,Hall__c,Height__c,Width__c,
                     Open_Sides__c,Additional_field_6__c,Additional_field_5__c,Phone__c,Website__c,Fax__c,PO_Box__c,Floor_Plan_Stage__c from Floor_Plan_Mapping__c order by createdDate DESC limit 1];
      
       String query= 'select ID,' ;
       if(LstFlrmapping[0].Edition__c!=null){
           query += LstFlrmapping[0].Edition__c+',' ; 
       }
       if(LstFlrmapping[0].Opportunity__c!=null){
           query += LstFlrmapping[0].Opportunity__c+',' ; 
       }
       if(LstFlrmapping[0].Opportunity_Id__c!=null){
           query += LstFlrmapping[0].Opportunity_Id__c+',' ; 
       }
       if(LstFlrmapping[0].Stage__c!=null){
           query += LstFlrmapping[0].Stage__c+',' ; 
       }
       if(LstFlrmapping[0].Banner_Name__c!=null){
           query += LstFlrmapping[0].Banner_Name__c+',' ; 
       }
       if(LstFlrmapping[0].Stand_no__c!=null){
           query += LstFlrmapping[0].Stand_no__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_3__c!=null){
           query += LstFlrmapping[0].Additional_field_3__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_2__c!=null){
           query += LstFlrmapping[0].Additional_field_2__c+',' ; 
       }
        if(LstFlrmapping[0].Type_of_Stand__c!=null){
           query += LstFlrmapping[0].Type_of_Stand__c+',' ; 
       }
        if(LstFlrmapping[0].Customer_Product__c!=null){
           query += LstFlrmapping[0].Customer_Product__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_4__c!=null){
           query += LstFlrmapping[0].Additional_field_4__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_1__c!=null){
           query += LstFlrmapping[0].Additional_field_1__c+',' ; 
       }
        if(LstFlrmapping[0].Contractual_Sq_M__c!=null){
           query += LstFlrmapping[0].Contractual_Sq_M__c+',' ; 
       }
        if(LstFlrmapping[0].Hall__c!=null){
           query += LstFlrmapping[0].Hall__c+',' ; 
       }
        if(LstFlrmapping[0].Height__c!=null){
           query += LstFlrmapping[0].Height__c+',' ; 
       }
        if(LstFlrmapping[0].Width__c!=null){
           query += LstFlrmapping[0].Width__c+',' ; 
       }
        if(LstFlrmapping[0].Open_Sides__c!=null){
           query += LstFlrmapping[0].Open_Sides__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_6__c!=null){
           query += LstFlrmapping[0].Additional_field_6__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_5__c!=null){
           query += LstFlrmapping[0].Additional_field_5__c+',' ; 
       }
        if(LstFlrmapping[0].Phone__c!=null){
           query += LstFlrmapping[0].Phone__c+',' ; 
       }
        if(LstFlrmapping[0].Website__c!=null){
           query += LstFlrmapping[0].Website__c+',' ; 
       }
        if(LstFlrmapping[0].Fax__c!=null){
           query += LstFlrmapping[0].Fax__c+',' ; 
       }
        if(LstFlrmapping[0].PO_Box__c!=null){
           query += LstFlrmapping[0].PO_Box__c+',' ; 
       } 
        if(LstFlrmapping[0].Floor_Plan_Stage__c!=null){
           query += LstFlrmapping[0].Floor_Plan_Stage__c+',' ; 
       }     
       
       query = query.removeEnd(','); 
       query += ' from '+LstFlrmapping[0].Object__c+' where '+LstFlrmapping[0].Edition__c+' = \''+LstFlrmapping[0].Edition_Event_Name__c +'\''; 

      String Edition=LstFlrmapping[0].Edition__c;
      String Opportunity=LstFlrmapping[0].Opportunity__c;
      String OpprtunityId=LstFlrmapping[0].Opportunity_Id__c;
      String Objects=LstFlrmapping[0].Object__c;
      String stage=LstFlrmapping[0].Stage__c;
      String BannerName=LstFlrmapping[0].Banner_Name__c;
      String StandNo=LstFlrmapping[0].Stand_no__c;
      String Edition_ID=LstFlrmapping[0].Additional_field_3__c;
      String Stand_Sub_Allocation_Name=LstFlrmapping[0].Additional_field_2__c;
      String Type_of_Stand=LstFlrmapping[0].Type_of_Stand__c;
      String Customer_Product=LstFlrmapping[0].Customer_Product__c;
      String Customer_Product_category=LstFlrmapping[0].Additional_field_4__c;
      String Product_Sub_Category=LstFlrmapping[0].Additional_field_1__c;
      String Contractual_Sq_M=LstFlrmapping[0].Contractual_Sq_M__c;
      String Hall=LstFlrmapping[0].Hall__c;
      String Height=LstFlrmapping[0].Height__c;
      String Width=LstFlrmapping[0].Width__c;
      String Open_Sides=LstFlrmapping[0].Open_Sides__c;
      String Street=LstFlrmapping[0].Additional_field_6__c;
      String City=LstFlrmapping[0].Additional_field_5__c;
      String Phone=LstFlrmapping[0].Phone__c;
      String Web_Sites=LstFlrmapping[0].Website__c;
      String Fax=LstFlrmapping[0].Fax__c;
      String PO_BOX=LstFlrmapping[0].PO_Box__c;
      String Floor_Plan_Stage = LstFlrmapping[0].Floor_Plan_Stage__c;  
      System.debug('Query formed----> '+query); 

     List<sObject> sc= Database.query(query);
     //System.debug('All Records -->'+sc);
       
     List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
     for(sObject  s : sc) 
     {
       system.debug('Cheking Object ssss ---->'+s);
       //OpportunityLineItem o = (OpportunityLineItem)s ;
        //sObject o =  (sObject)s.get(LstFlrmapping[0].Object__c);
        //sObject o = ((LstFlrmapping[0].Object__c != null) ? (Sobject)s.get(LstFlrmapping[0].Object__c) : null);
       sObject o = (sObject)s;

        system.debug('Cheking Object---->'+o);
         try
         {
         
     //OppWrapper oppwrappervar=new OppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);
     OppWrapper oppwrappervars = new OppWrapper(
    (LstFlrmapping[0].Edition__c != null) ? (string)o.get(LstFlrmapping[0].Edition__c) : null,
    (LstFlrmapping[0].Opportunity_Id__c != null) ? (string)o.get(LstFlrmapping[0].Opportunity_Id__c) : null,
    (LstFlrmapping[0].Opportunity__c != null) ? (string)o.get(LstFlrmapping[0].Opportunity__c) : null,
    (LstFlrmapping[0].Stage__c != null) ? (string)o.get(LstFlrmapping[0].Stage__c) : null,
    (LstFlrmapping[0].Banner_Name__c != null) ? (string)o.get(LstFlrmapping[0].Banner_Name__c) : null,
    (LstFlrmapping[0].Stand_no__c != null) ? (string)o.get(LstFlrmapping[0].Stand_no__c) : null,
    (LstFlrmapping[0].Additional_field_3__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_3__c) : null,
    (LstFlrmapping[0].Additional_field_2__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_2__c) : null,
    (LstFlrmapping[0].Phone__c != null) ? (string)o.get(LstFlrmapping[0].Phone__c) : null,
    (LstFlrmapping[0].Website__c != null) ? (string)o.get(LstFlrmapping[0].Website__c) : null,
    (LstFlrmapping[0].Additional_field_5__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_5__c) : null,
    (LstFlrmapping[0].Additional_field_6__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_6__c) : null,
    (LstFlrmapping[0].PO_Box__c != null) ? (string)o.get(LstFlrmapping[0].PO_Box__c) : null,
    (LstFlrmapping[0].Customer_Product__c != null) ? (string)o.get(LstFlrmapping[0].Customer_Product__c) : null,      
    (LstFlrmapping[0].Additional_field_4__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_4__c) : null,
    (LstFlrmapping[0].Fax__c != null) ? (string)o.get(LstFlrmapping[0].Fax__c) : null,
    (LstFlrmapping[0].Additional_field_1__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_1__c) : null,
    (LstFlrmapping[0].Hall__c != null) ? (string)o.get(LstFlrmapping[0].Hall__c) : null,
    (LstFlrmapping[0].Height__c != null) ? (string)o.get(LstFlrmapping[0].Height__c) : null,
    (LstFlrmapping[0].Width__c != null) ? (string)o.get(LstFlrmapping[0].Width__c) : null,
    (LstFlrmapping[0].Contractual_Sq_M__c != null) ? (Decimal)o.get(LstFlrmapping[0].Contractual_Sq_M__c) : null,
    (LstFlrmapping[0].Open_Sides__c != null) ? (string)o.get(LstFlrmapping[0].Open_Sides__c) : null,
    (LstFlrmapping[0].Type_of_Stand__c != null) ? (string)o.get(LstFlrmapping[0].Type_of_Stand__c) : null);
       oppWrapperVarList.add(oppwrappervars);
         }catch(Exception ex)
         {
             System.debug('================'+ex.getMessage()+'======================'+ex);
         }
        
     }
        string jsonbody=Json.serialize(oppWrapperVarList);
        system.debug('Cheking API---->'+oppWrapperVarList);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setTimeout(25000);
        //request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setEndpoint('http://sfdata.iqratechnology.com/api/data');

        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
     
        request.setBody(jsonbody);
        
       HttpResponse response = http.send(request);
        system.debug('Response body '+ response.getBody());
     if (response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
      } else {
    System.debug(response.getBody());
    }
        
        
    }
    
    public class OppWrapper
    {
        public String EventName {get;set;}
        public String RecordId {get;set;}
        public String RecordName {get;set;}
        public String RecordStage {get;set;}
        public String StandNo {get;set;}
        public String BannerName {get;set;}
        public String Edition_ID {get;set;}
        public String Stand_Sub_Allocation_Name {get;set;}
        public String Phone {get;set;}
        public String  Web_Site {get;set;}
        public String  City {get;set;}
        public String  Street {get;set;}
        public String  PO_BOX {get;set;}
        public String  Customer_Product {get;set;}
        public String  Customer_Product_category {get;set;}
        public String  Fax {get;set;}
        public String  Product_Sub_Category {get;set;}
        public String  Hall {get;set;}
        public String  Height {get;set;}
        public String  Width {get;set;}
        public Decimal  Contractual_Sq_M {get;set;}
        public String  Open_Sides {get;set;}
        public String  Type_of_Stand {get;set;}
        public OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String BannerName,String StandNo,String Edition_ID,
                          String Stand_Sub_Allocation_Name,String Phone,String Web_Site,String City,String Street,String PO_BOX,String Customer_Product,
                          String Customer_Product_category,String Fax,String Product_Sub_Category,String Hall,String Height,String Width,Decimal Contractual_Sq_M,
                         String Open_Sides,String Type_of_Stand)
                          
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            this.Edition_ID=Edition_ID;
            this.Stand_Sub_Allocation_Name=Stand_Sub_Allocation_Name;
            this.Phone=Phone;
            this.Web_Site=Web_Site;
            this.City=City;
            this.Street=Street;
            this.PO_BOX=PO_BOX;
            this.Customer_Product=Customer_Product;
            this.Customer_Product_category=Customer_Product_category;
            this.Fax=Fax;
            this.Product_Sub_Category=Product_Sub_Category;
            this.Hall=Hall;
            this.Height=Height;
            this.Width=Width;
            this.Contractual_Sq_M=Contractual_Sq_M;
            this.Open_Sides=Open_Sides;
            this.Type_of_Stand=Type_of_Stand;
        } 
    }
    
}
Rajesh_KumarRajesh_Kumar
Hi Iqra,
Here is my try
1.MyBatchClass.apxc
public class MyBatchClass implements Database.Batchable<sObject>, Database.AllowsCallouts{
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'select Id,Edition__c,Object__c,Opportunity__c,Opportunity_Id__c,Stage__c,Banner_Name__c,Stand_no__c,Edition_Event_Name__c,Additional_field_3__c,' +
            'Additional_field_2__c,Type_of_Stand__c,Customer_Product__c,Additional_field_4__c,Additional_field_1__c,Contractual_Sq_M__c,Hall__c,Height__c,Width__c,' +
            'Open_Sides__c,Additional_field_6__c,Additional_field_5__c,Phone__c,Website__c,Fax__c,PO_Box__c,Floor_Plan_Stage__c from ' + 
            'Floor_Plan_Mapping__c order by createdDate DESC'
        );
    }
    public void execute(Database.BatchableContext bc, List<sObject> records){
        // process each batch of records
		NEW_API_CLASSES.Insert_Records(records[0], true);
    }    
    public void finish(Database.BatchableContext bc){
        
    }    
}

2.NEW_API_CLASSES .apxc
public class NEW_API_CLASSES {
     Public static void Insert_Records(Floor_Plan_Mapping__c flpRecord, Boolean isBatchExe){           
        List<Floor_Plan_Mapping__c> LstFlrmapping=new list< Floor_Plan_Mapping__c>();
	    if(!isBatchExe){
			LstFlrmapping=[select Id,Edition__c,Object__c,Opportunity__c,Opportunity_Id__c,Stage__c,Banner_Name__c,Stand_no__c,Edition_Event_Name__c,Additional_field_3__c,
						 Additional_field_2__c,Type_of_Stand__c,Customer_Product__c,Additional_field_4__c,Additional_field_1__c,Contractual_Sq_M__c,Hall__c,Height__c,Width__c,
						 Open_Sides__c,Additional_field_6__c,Additional_field_5__c,Phone__c,Website__c,Fax__c,PO_Box__c,Floor_Plan_Stage__c from Floor_Plan_Mapping__c order by createdDate DESC limit 1];
		}else{
			LstFlrmapping.add(flpRecord);
		}
		if(LstFlrmapping != null && LstFlrmapping.size() > 0){
			----- Rest of you code ------
		}
	}
}

3.Run the batch like this
Database.executeBatch(new MyBatchClass(),1);

4.NEW_API_CLASSES.Insert_Records method call for non-batch class
NEW_API_CLASSES.Insert_Records(Null, false);

 
Iqra TechIqra Tech
@Rajesh_Kumar thanks sir but i have one question can directly do in batch class i mean no need to call class from bacth class just a single batch class insted of Http callout class ?
Rajesh_KumarRajesh_Kumar
@Iqra Tech
You can do that too. But it's a best practice to have a different class for callouts. You can reuse it in the future when your requirement changes.
Iqra TechIqra Tech
ok @Rajesh_Kumar thanks sir can you please do that i will try both ways ...
Iqra TechIqra Tech
@Rajesh_Kumar Sir to create Batch class direct but getting error can u please make me correct see my created Batch class on Http callout class

global class Batch_API_New implements Database.Batchable<sObject>, Database.AllowsCallouts{
    public Database.QueryLocator start(Database.BatchableContext bc) {
      List<Floor_Plan_Mapping__c> LstFlrmapping=new list< Floor_Plan_Mapping__c>();
      LstFlrmapping=[select Id,Edition__c,Object__c,Opportunity__c,Opportunity_Id__c,Stage__c,Banner_Name__c,Stand_no__c,Edition_Event_Name__c,Additional_field_3__c,
                     Additional_field_2__c,Type_of_Stand__c,Customer_Product__c,Additional_field_4__c,Additional_field_1__c,Contractual_Sq_M__c,Hall__c,Height__c,Width__c,
                     Open_Sides__c,Additional_field_6__c,Additional_field_5__c,Phone__c,Website__c,Fax__c,PO_Box__c,Floor_Plan_Stage__c from Floor_Plan_Mapping__c order by createdDate DESC limit 1];
      
       String query= 'select ID,' ;
       if(LstFlrmapping[0].Edition__c!=null){
           query += LstFlrmapping[0].Edition__c+',' ; 
       }
       if(LstFlrmapping[0].Opportunity__c!=null){
           query += LstFlrmapping[0].Opportunity__c+',' ; 
       }
       if(LstFlrmapping[0].Opportunity_Id__c!=null){
           query += LstFlrmapping[0].Opportunity_Id__c+',' ; 
       }
       if(LstFlrmapping[0].Stage__c!=null){
           query += LstFlrmapping[0].Stage__c+',' ; 
       }
       if(LstFlrmapping[0].Banner_Name__c!=null){
           query += LstFlrmapping[0].Banner_Name__c+',' ; 
       }
       if(LstFlrmapping[0].Stand_no__c!=null){
           query += LstFlrmapping[0].Stand_no__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_3__c!=null){
           query += LstFlrmapping[0].Additional_field_3__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_2__c!=null){
           query += LstFlrmapping[0].Additional_field_2__c+',' ; 
       }
        if(LstFlrmapping[0].Type_of_Stand__c!=null){
           query += LstFlrmapping[0].Type_of_Stand__c+',' ; 
       }
        if(LstFlrmapping[0].Customer_Product__c!=null){
           query += LstFlrmapping[0].Customer_Product__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_4__c!=null){
           query += LstFlrmapping[0].Additional_field_4__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_1__c!=null){
           query += LstFlrmapping[0].Additional_field_1__c+',' ; 
       }
        if(LstFlrmapping[0].Contractual_Sq_M__c!=null){
           query += LstFlrmapping[0].Contractual_Sq_M__c+',' ; 
       }
        if(LstFlrmapping[0].Hall__c!=null){
           query += LstFlrmapping[0].Hall__c+',' ; 
       }
        if(LstFlrmapping[0].Height__c!=null){
           query += LstFlrmapping[0].Height__c+',' ; 
       }
        if(LstFlrmapping[0].Width__c!=null){
           query += LstFlrmapping[0].Width__c+',' ; 
       }
        if(LstFlrmapping[0].Open_Sides__c!=null){
           query += LstFlrmapping[0].Open_Sides__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_6__c!=null){
           query += LstFlrmapping[0].Additional_field_6__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_5__c!=null){
           query += LstFlrmapping[0].Additional_field_5__c+',' ; 
       }
        if(LstFlrmapping[0].Phone__c!=null){
           query += LstFlrmapping[0].Phone__c+',' ; 
       }
        if(LstFlrmapping[0].Website__c!=null){
           query += LstFlrmapping[0].Website__c+',' ; 
       }
        if(LstFlrmapping[0].Fax__c!=null){
           query += LstFlrmapping[0].Fax__c+',' ; 
       }
        if(LstFlrmapping[0].PO_Box__c!=null){
           query += LstFlrmapping[0].PO_Box__c+',' ; 
       } 
        if(LstFlrmapping[0].Floor_Plan_Stage__c!=null){
           query += LstFlrmapping[0].Floor_Plan_Stage__c+',' ; 
       }     
       
       query = query.removeEnd(','); 
       query += ' from '+LstFlrmapping[0].Object__c+' where '+LstFlrmapping[0].Edition__c+' = \''+LstFlrmapping[0].Edition_Event_Name__c +'\''; 
        return Database.getQueryLocator(query);
          String Edition=LstFlrmapping[0].Edition__c;
      String Opportunity=LstFlrmapping[0].Opportunity__c;
      String OpprtunityId=LstFlrmapping[0].Opportunity_Id__c;
      String Objects=LstFlrmapping[0].Object__c;
      String stage=LstFlrmapping[0].Stage__c;
      String BannerName=LstFlrmapping[0].Banner_Name__c;
      String StandNo=LstFlrmapping[0].Stand_no__c;
      String Edition_ID=LstFlrmapping[0].Additional_field_3__c;
      String Stand_Sub_Allocation_Name=LstFlrmapping[0].Additional_field_2__c;
      String Type_of_Stand=LstFlrmapping[0].Type_of_Stand__c;
      String Customer_Product=LstFlrmapping[0].Customer_Product__c;
      String Customer_Product_category=LstFlrmapping[0].Additional_field_4__c;
      String Product_Sub_Category=LstFlrmapping[0].Additional_field_1__c;
      String Contractual_Sq_M=LstFlrmapping[0].Contractual_Sq_M__c;
      String Hall=LstFlrmapping[0].Hall__c;
      String Height=LstFlrmapping[0].Height__c;
      String Width=LstFlrmapping[0].Width__c;
      String Open_Sides=LstFlrmapping[0].Open_Sides__c;
      String Street=LstFlrmapping[0].Additional_field_6__c;
      String City=LstFlrmapping[0].Additional_field_5__c;
      String Phone=LstFlrmapping[0].Phone__c;
      String Web_Sites=LstFlrmapping[0].Website__c;
      String Fax=LstFlrmapping[0].Fax__c;
      String PO_BOX=LstFlrmapping[0].PO_Box__c;
      String Floor_Plan_Stage = LstFlrmapping[0].Floor_Plan_Stage__c;  
      System.debug('Query formed----> '+query);
    }
    global void execute(Database.BatchableContext bc, List<sObject> sc){
       
     List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
     for(sObject  s : sc) 
     {
       system.debug('Cheking Object ssss ---->'+s);
       //OpportunityLineItem o = (OpportunityLineItem)s ;
        //sObject o =  (sObject)s.get(LstFlrmapping[0].Object__c);
        //sObject o = ((LstFlrmapping[0].Object__c != null) ? (Sobject)s.get(LstFlrmapping[0].Object__c) : null);
       sObject o = (sObject)s;

        system.debug('Cheking Object---->'+o);
         try
         {
         
     //OppWrapper oppwrappervar=new OppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);
     OppWrapper oppwrappervars = new OppWrapper(
    (LstFlrmapping[0].Edition__c != null) ? (string)o.get(LstFlrmapping[0].Edition__c) : null,
    (LstFlrmapping[0].Opportunity_Id__c != null) ? (string)o.get(LstFlrmapping[0].Opportunity_Id__c) : null,
    (LstFlrmapping[0].Opportunity__c != null) ? (string)o.get(LstFlrmapping[0].Opportunity__c) : null,
    (LstFlrmapping[0].Stage__c != null) ? (string)o.get(LstFlrmapping[0].Stage__c) : null,
    (LstFlrmapping[0].Banner_Name__c != null) ? (string)o.get(LstFlrmapping[0].Banner_Name__c) : null,
    (LstFlrmapping[0].Stand_no__c != null) ? (string)o.get(LstFlrmapping[0].Stand_no__c) : null,
    (LstFlrmapping[0].Additional_field_3__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_3__c) : null,
    (LstFlrmapping[0].Additional_field_2__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_2__c) : null,
    (LstFlrmapping[0].Phone__c != null) ? (string)o.get(LstFlrmapping[0].Phone__c) : null,
    (LstFlrmapping[0].Website__c != null) ? (string)o.get(LstFlrmapping[0].Website__c) : null,
    (LstFlrmapping[0].Additional_field_5__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_5__c) : null,
    (LstFlrmapping[0].Additional_field_6__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_6__c) : null,
    (LstFlrmapping[0].PO_Box__c != null) ? (string)o.get(LstFlrmapping[0].PO_Box__c) : null,
    (LstFlrmapping[0].Customer_Product__c != null) ? (string)o.get(LstFlrmapping[0].Customer_Product__c) : null,      
    (LstFlrmapping[0].Additional_field_4__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_4__c) : null,
    (LstFlrmapping[0].Fax__c != null) ? (string)o.get(LstFlrmapping[0].Fax__c) : null,
    (LstFlrmapping[0].Additional_field_1__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_1__c) : null,
    (LstFlrmapping[0].Hall__c != null) ? (string)o.get(LstFlrmapping[0].Hall__c) : null,
    (LstFlrmapping[0].Height__c != null) ? (string)o.get(LstFlrmapping[0].Height__c) : null,
    (LstFlrmapping[0].Width__c != null) ? (string)o.get(LstFlrmapping[0].Width__c) : null,
    (LstFlrmapping[0].Contractual_Sq_M__c != null) ? (Decimal)o.get(LstFlrmapping[0].Contractual_Sq_M__c) : null,
    (LstFlrmapping[0].Open_Sides__c != null) ? (string)o.get(LstFlrmapping[0].Open_Sides__c) : null,
    (LstFlrmapping[0].Type_of_Stand__c != null) ? (string)o.get(LstFlrmapping[0].Type_of_Stand__c) : null);
       oppWrapperVarList.add(oppwrappervars);
         }catch(Exception ex)
         {
             System.debug('================'+ex.getMessage()+'======================'+ex);
         }
        
     }
        string jsonbody=Json.serialize(oppWrapperVarList);
        system.debug('Cheking API---->'+oppWrapperVarList);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setTimeout(25000);
        //request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setEndpoint('http://sfdata.iqratechnology.com/api/data');

        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
     
        request.setBody(jsonbody);
        
       HttpResponse response = http.send(request);
        system.debug('Response body '+ response.getBody());
     if (response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
      } else {
    System.debug(response.getBody());
    }
        
        
    }
    
    public class OppWrapper
    {
        public String EventName {get;set;}
        public String RecordId {get;set;}
        public String RecordName {get;set;}
        public String RecordStage {get;set;}
        public String StandNo {get;set;}
        public String BannerName {get;set;}
        public String Edition_ID {get;set;}
        public String Stand_Sub_Allocation_Name {get;set;}
        public String Phone {get;set;}
        public String  Web_Site {get;set;}
        public String  City {get;set;}
        public String  Street {get;set;}
        public String  PO_BOX {get;set;}
        public String  Customer_Product {get;set;}
        public String  Customer_Product_category {get;set;}
        public String  Fax {get;set;}
        public String  Product_Sub_Category {get;set;}
        public String  Hall {get;set;}
        public String  Height {get;set;}
        public String  Width {get;set;}
        public Decimal  Contractual_Sq_M {get;set;}
        public String  Open_Sides {get;set;}
        public String  Type_of_Stand {get;set;}
        public OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String BannerName,String StandNo,String Edition_ID,
                          String Stand_Sub_Allocation_Name,String Phone,String Web_Site,String City,String Street,String PO_BOX,String Customer_Product,
                          String Customer_Product_category,String Fax,String Product_Sub_Category,String Hall,String Height,String Width,Decimal Contractual_Sq_M,
                         String Open_Sides,String Type_of_Stand)
                          
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            this.Edition_ID=Edition_ID;
            this.Stand_Sub_Allocation_Name=Stand_Sub_Allocation_Name;
            this.Phone=Phone;
            this.Web_Site=Web_Site;
            this.City=City;
            this.Street=Street;
            this.PO_BOX=PO_BOX;
            this.Customer_Product=Customer_Product;
            this.Customer_Product_category=Customer_Product_category;
            this.Fax=Fax;
            this.Product_Sub_Category=Product_Sub_Category;
            this.Hall=Hall;
            this.Height=Height;
            this.Width=Width;
            this.Contractual_Sq_M=Contractual_Sq_M;
            this.Open_Sides=Open_Sides;
            this.Type_of_Stand=Type_of_Stand;
        } 
    }
    }    
    global void finish(Database.BatchableContext bc){
        Id job= bc.getJobId();
        System.debug(job);
    }
}
Rajesh_KumarRajesh_Kumar
public class Batch_API_New implements Database.Batchable<sObject>, Database.AllowsCallouts{
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'select Id,Edition__c,Object__c,Opportunity__c,Opportunity_Id__c,Stage__c,Banner_Name__c,Stand_no__c,Edition_Event_Name__c,Additional_field_3__c,' +
            'Additional_field_2__c,Type_of_Stand__c,Customer_Product__c,Additional_field_4__c,Additional_field_1__c,Contractual_Sq_M__c,Hall__c,Height__c,Width__c,' +
            'Open_Sides__c,Additional_field_6__c,Additional_field_5__c,Phone__c,Website__c,Fax__c,PO_Box__c,Floor_Plan_Stage__c from ' + 
            'Floor_Plan_Mapping__c order by createdDate DESC'
        );
    }
    public void execute(Database.BatchableContext bc, List<sObject> records){
        // process each batch of records
		Insert_Records(records);
    }    
    public void finish(Database.BatchableContext bc){
        
    }    
	Public void Insert_Records(List<Floor_Plan_Mapping__c> LstFlrmapping){  
		String query= 'select ID,' ;
       if(LstFlrmapping[0].Edition__c!=null){
           query += LstFlrmapping[0].Edition__c+',' ; 
       }
       if(LstFlrmapping[0].Opportunity__c!=null){
           query += LstFlrmapping[0].Opportunity__c+',' ; 
       }
       if(LstFlrmapping[0].Opportunity_Id__c!=null){
           query += LstFlrmapping[0].Opportunity_Id__c+',' ; 
       }
       if(LstFlrmapping[0].Stage__c!=null){
           query += LstFlrmapping[0].Stage__c+',' ; 
       }
       if(LstFlrmapping[0].Banner_Name__c!=null){
           query += LstFlrmapping[0].Banner_Name__c+',' ; 
       }
       if(LstFlrmapping[0].Stand_no__c!=null){
           query += LstFlrmapping[0].Stand_no__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_3__c!=null){
           query += LstFlrmapping[0].Additional_field_3__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_2__c!=null){
           query += LstFlrmapping[0].Additional_field_2__c+',' ; 
       }
        if(LstFlrmapping[0].Type_of_Stand__c!=null){
           query += LstFlrmapping[0].Type_of_Stand__c+',' ; 
       }
        if(LstFlrmapping[0].Customer_Product__c!=null){
           query += LstFlrmapping[0].Customer_Product__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_4__c!=null){
           query += LstFlrmapping[0].Additional_field_4__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_1__c!=null){
           query += LstFlrmapping[0].Additional_field_1__c+',' ; 
       }
        if(LstFlrmapping[0].Contractual_Sq_M__c!=null){
           query += LstFlrmapping[0].Contractual_Sq_M__c+',' ; 
       }
        if(LstFlrmapping[0].Hall__c!=null){
           query += LstFlrmapping[0].Hall__c+',' ; 
       }
        if(LstFlrmapping[0].Height__c!=null){
           query += LstFlrmapping[0].Height__c+',' ; 
       }
        if(LstFlrmapping[0].Width__c!=null){
           query += LstFlrmapping[0].Width__c+',' ; 
       }
        if(LstFlrmapping[0].Open_Sides__c!=null){
           query += LstFlrmapping[0].Open_Sides__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_6__c!=null){
           query += LstFlrmapping[0].Additional_field_6__c+',' ; 
       }
        if(LstFlrmapping[0].Additional_field_5__c!=null){
           query += LstFlrmapping[0].Additional_field_5__c+',' ; 
       }
        if(LstFlrmapping[0].Phone__c!=null){
           query += LstFlrmapping[0].Phone__c+',' ; 
       }
        if(LstFlrmapping[0].Website__c!=null){
           query += LstFlrmapping[0].Website__c+',' ; 
       }
        if(LstFlrmapping[0].Fax__c!=null){
           query += LstFlrmapping[0].Fax__c+',' ; 
       }
        if(LstFlrmapping[0].PO_Box__c!=null){
           query += LstFlrmapping[0].PO_Box__c+',' ; 
       } 
        if(LstFlrmapping[0].Floor_Plan_Stage__c!=null){
           query += LstFlrmapping[0].Floor_Plan_Stage__c+',' ; 
       }     
       
       query = query.removeEnd(','); 
       query += ' from '+LstFlrmapping[0].Object__c+' where '+LstFlrmapping[0].Edition__c+' = \''+LstFlrmapping[0].Edition_Event_Name__c +'\''; 

      String Edition=LstFlrmapping[0].Edition__c;
      String Opportunity=LstFlrmapping[0].Opportunity__c;
      String OpprtunityId=LstFlrmapping[0].Opportunity_Id__c;
      String Objects=LstFlrmapping[0].Object__c;
      String stage=LstFlrmapping[0].Stage__c;
      String BannerName=LstFlrmapping[0].Banner_Name__c;
      String StandNo=LstFlrmapping[0].Stand_no__c;
      String Edition_ID=LstFlrmapping[0].Additional_field_3__c;
      String Stand_Sub_Allocation_Name=LstFlrmapping[0].Additional_field_2__c;
      String Type_of_Stand=LstFlrmapping[0].Type_of_Stand__c;
      String Customer_Product=LstFlrmapping[0].Customer_Product__c;
      String Customer_Product_category=LstFlrmapping[0].Additional_field_4__c;
      String Product_Sub_Category=LstFlrmapping[0].Additional_field_1__c;
      String Contractual_Sq_M=LstFlrmapping[0].Contractual_Sq_M__c;
      String Hall=LstFlrmapping[0].Hall__c;
      String Height=LstFlrmapping[0].Height__c;
      String Width=LstFlrmapping[0].Width__c;
      String Open_Sides=LstFlrmapping[0].Open_Sides__c;
      String Street=LstFlrmapping[0].Additional_field_6__c;
      String City=LstFlrmapping[0].Additional_field_5__c;
      String Phone=LstFlrmapping[0].Phone__c;
      String Web_Sites=LstFlrmapping[0].Website__c;
      String Fax=LstFlrmapping[0].Fax__c;
      String PO_BOX=LstFlrmapping[0].PO_Box__c;
      String Floor_Plan_Stage = LstFlrmapping[0].Floor_Plan_Stage__c;  
      System.debug('Query formed----> '+query); 

     List<sObject> sc= Database.query(query);
     //System.debug('All Records -->'+sc);
       
     List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
     for(sObject  s : sc) 
     {
       system.debug('Cheking Object ssss ---->'+s);
       //OpportunityLineItem o = (OpportunityLineItem)s ;
        //sObject o =  (sObject)s.get(LstFlrmapping[0].Object__c);
        //sObject o = ((LstFlrmapping[0].Object__c != null) ? (Sobject)s.get(LstFlrmapping[0].Object__c) : null);
       sObject o = (sObject)s;

        system.debug('Cheking Object---->'+o);
         try
         {
         
     //OppWrapper oppwrappervar=new OppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);
     OppWrapper oppwrappervars = new OppWrapper(
    (LstFlrmapping[0].Edition__c != null) ? (string)o.get(LstFlrmapping[0].Edition__c) : null,
    (LstFlrmapping[0].Opportunity_Id__c != null) ? (string)o.get(LstFlrmapping[0].Opportunity_Id__c) : null,
    (LstFlrmapping[0].Opportunity__c != null) ? (string)o.get(LstFlrmapping[0].Opportunity__c) : null,
    (LstFlrmapping[0].Stage__c != null) ? (string)o.get(LstFlrmapping[0].Stage__c) : null,
    (LstFlrmapping[0].Banner_Name__c != null) ? (string)o.get(LstFlrmapping[0].Banner_Name__c) : null,
    (LstFlrmapping[0].Stand_no__c != null) ? (string)o.get(LstFlrmapping[0].Stand_no__c) : null,
    (LstFlrmapping[0].Additional_field_3__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_3__c) : null,
    (LstFlrmapping[0].Additional_field_2__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_2__c) : null,
    (LstFlrmapping[0].Phone__c != null) ? (string)o.get(LstFlrmapping[0].Phone__c) : null,
    (LstFlrmapping[0].Website__c != null) ? (string)o.get(LstFlrmapping[0].Website__c) : null,
    (LstFlrmapping[0].Additional_field_5__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_5__c) : null,
    (LstFlrmapping[0].Additional_field_6__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_6__c) : null,
    (LstFlrmapping[0].PO_Box__c != null) ? (string)o.get(LstFlrmapping[0].PO_Box__c) : null,
    (LstFlrmapping[0].Customer_Product__c != null) ? (string)o.get(LstFlrmapping[0].Customer_Product__c) : null,      
    (LstFlrmapping[0].Additional_field_4__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_4__c) : null,
    (LstFlrmapping[0].Fax__c != null) ? (string)o.get(LstFlrmapping[0].Fax__c) : null,
    (LstFlrmapping[0].Additional_field_1__c != null) ? (string)o.get(LstFlrmapping[0].Additional_field_1__c) : null,
    (LstFlrmapping[0].Hall__c != null) ? (string)o.get(LstFlrmapping[0].Hall__c) : null,
    (LstFlrmapping[0].Height__c != null) ? (string)o.get(LstFlrmapping[0].Height__c) : null,
    (LstFlrmapping[0].Width__c != null) ? (string)o.get(LstFlrmapping[0].Width__c) : null,
    (LstFlrmapping[0].Contractual_Sq_M__c != null) ? (Decimal)o.get(LstFlrmapping[0].Contractual_Sq_M__c) : null,
    (LstFlrmapping[0].Open_Sides__c != null) ? (string)o.get(LstFlrmapping[0].Open_Sides__c) : null,
    (LstFlrmapping[0].Type_of_Stand__c != null) ? (string)o.get(LstFlrmapping[0].Type_of_Stand__c) : null);
       oppWrapperVarList.add(oppwrappervars);
         }catch(Exception ex)
         {
             System.debug('================'+ex.getMessage()+'======================'+ex);
         }
        
     }
        string jsonbody=Json.serialize(oppWrapperVarList);
        system.debug('Cheking API---->'+oppWrapperVarList);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setTimeout(25000);
        //request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setEndpoint('http://sfdata.iqratechnology.com/api/data');

        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
     
        request.setBody(jsonbody);
        
       HttpResponse response = http.send(request);
        system.debug('Response body '+ response.getBody());
     if (response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
      } else {
		System.debug(response.getBody());
		}	
    }
	public class OppWrapper
    {
        public String EventName {get;set;}
        public String RecordId {get;set;}
        public String RecordName {get;set;}
        public String RecordStage {get;set;}
        public String StandNo {get;set;}
        public String BannerName {get;set;}
        public String Edition_ID {get;set;}
        public String Stand_Sub_Allocation_Name {get;set;}
        public String Phone {get;set;}
        public String  Web_Site {get;set;}
        public String  City {get;set;}
        public String  Street {get;set;}
        public String  PO_BOX {get;set;}
        public String  Customer_Product {get;set;}
        public String  Customer_Product_category {get;set;}
        public String  Fax {get;set;}
        public String  Product_Sub_Category {get;set;}
        public String  Hall {get;set;}
        public String  Height {get;set;}
        public String  Width {get;set;}
        public Decimal  Contractual_Sq_M {get;set;}
        public String  Open_Sides {get;set;}
        public String  Type_of_Stand {get;set;}
        public OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String BannerName,String StandNo,String Edition_ID,
                          String Stand_Sub_Allocation_Name,String Phone,String Web_Site,String City,String Street,String PO_BOX,String Customer_Product,
                          String Customer_Product_category,String Fax,String Product_Sub_Category,String Hall,String Height,String Width,Decimal Contractual_Sq_M,
                         String Open_Sides,String Type_of_Stand)
                          
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            this.Edition_ID=Edition_ID;
            this.Stand_Sub_Allocation_Name=Stand_Sub_Allocation_Name;
            this.Phone=Phone;
            this.Web_Site=Web_Site;
            this.City=City;
            this.Street=Street;
            this.PO_BOX=PO_BOX;
            this.Customer_Product=Customer_Product;
            this.Customer_Product_category=Customer_Product_category;
            this.Fax=Fax;
            this.Product_Sub_Category=Product_Sub_Category;
            this.Hall=Hall;
            this.Height=Height;
            this.Width=Width;
            this.Contractual_Sq_M=Contractual_Sq_M;
            this.Open_Sides=Open_Sides;
            this.Type_of_Stand=Type_of_Stand;
        } 
    }
}
Don't forget to run the batch with the batch_size 1. Database.executeBatch(new Batch_API_New (),1);
This was selected as the best answer
Iqra TechIqra Tech
@Rajesh_Kumar Sir thnk you so much
it is working perfctly just need to do more testting on that then after need to add authentication and need to do shedlue on futurer your help needed can i get you email id if i needed your help to achive this requiremnt...
Iqra TechIqra Tech
 return Database.getQueryLocator(
            'select Id,Edition__c,Object__c,Opportunity__c,Opportunity_Id__c,Stage__c,Banner_Name__c,Stand_no__c,Edition_Event_Name__c,Additional_field_3__c,' +
            'Additional_field_2__c,Type_of_Stand__c,Customer_Product__c,Additional_field_4__c,Additional_field_1__c,Contractual_Sq_M__c,Hall__c,Height__c,Width__c,' +
            'Open_Sides__c,Additional_field_6__c,Additional_field_5__c,Phone__c,Website__c,Fax__c,PO_Box__c,Floor_Plan_Stage__c from ' + 
            //'Floor_Plan_Mapping__c order by createdDate DESC limit 1'
            'Floor_Plan_Mapping__c where Edition_Event_Name__c =:'FM Expo 2017'');

i am getting errors when i am adding where condition in that please correct ........
Rajesh_KumarRajesh_Kumar
 Replace your last line with this  'Floor_Plan_Mapping__c where Edition_Event_Name__c =\'FM Expo 2017\'');
Iqra TechIqra Tech
@Rajesh_Kumar Thanks sir ii is  working one last thing need to write test class for this batch class...
Iqra TechIqra Tech

 Replace your last line with this  'Floor_Plan_Mapping__c where Edition_Event_Name__c =\'FM Expo 2017\'');

sir if i have more than one value like FM Expo 2017 and FM Expo 2018 how to write for calling those in where condtion?

 'Floor_Plan_Mapping__c where Edition_Event_Name__c =\'FM Expo 2017\' ,'FM Expo 2018''); i tried this but not working  can u please chehk this ...

Iqra TechIqra Tech
@Rajesh_Kumar sir U der??
Rajesh_KumarRajesh_Kumar
Hi Iqra,
Add your where conditions in a list before return.
List<String> e_eventNameList = new List<String>();
e_eventNameList.add('FM Expo 2017');
e_eventNameList.add('FM Expo 2018');

Change the last line of the query
'Floor_Plan_Mapping__c where Edition_Event_Name__c IN:e_eventNameList');

 
Iqra TechIqra Tech
need to dynamically for mutilple values search in where condition can we do that in query no need to add values in list and pass in where condion can it be possiblle??
Iqra TechIqra Tech
Sir one more question 
Like suppose i am passing two values in myrecord like this FM Expo 2017, FM Expo 2018,  then how can we call in our query?
Iqra TechIqra Tech
I mean can we add values in where clause may might know user main be select  FM Expo 2017, only or can be select both FM Expo 2017, FM Expo 2018,  that things need to do via dynamically can it be possible can u please ... SIr
Rajesh_KumarRajesh_Kumar
You can accomplish this via custom settings. Go through the documentation and some example of it.
Iqra TechIqra Tech

@Rajesh_Kumar Sir

Like this we are using in vf page to store the muliple or single values in wheere condtion dynamically can u please chehk this i am stuck to add thiss logic can you please chehk this and provide me a solution

 WHERE '+fldAPINameMap.get(selectedField)+' IN (\'' + String.join( criteria.split( ',' ), '\',\'' ) +'\') limit 999 ';
Rajesh_KumarRajesh_Kumar
Give some debug of the query string. How it's constructing.
Iqra TechIqra Tech
which one just i provided this vf page dynamic query?
Iqra TechIqra Tech

15:56:56.0 (169021698)|HEAP_ALLOCATE|[203]|Bytes:234
15:56:56.0 (169029968)|VARIABLE_SCOPE_BEGIN|[203]|queryStr1|String|false|false
15:56:56.0 (169048259)|VARIABLE_ASSIGNMENT|[203]|queryStr1|"SELECT createdDate,l (214 more) ..."
15:56:56.0 (169058985)|STATEMENT_EXECUTE|[204]
15:56:56.0 (169062509)|HEAP_ALLOCATE|[204]|Bytes:11
15:56:56.0 (169070573)|HEAP_ALLOCATE|[204]|Bytes:245
15:56:56.0 (169105832)|USER_DEBUG|[204]|DEBUG|queryStr1-=SELECT createdDate,lastModifiedDate,P_Opportunity_ID__c,p_Opprotuny_name__c,P_Stage__c,P_Banner__c,Stand_Number_s__c,Edition_id__c,P_Edition__c  FROM opportunitylineitem WHERE P_Edition__c IN ('FM Expo 2017','FM Expo 2018') limit 999 
15:56:56.0 (169124659)|STATEMENT_EXECUTE|[205]

 

Please chehk this

Rajesh_KumarRajesh_Kumar
Have you tried like this
 WHERE '+fldAPINameMap.get(selectedField)+' IN :criteria.split( ',' ) limit 999 ';
Iqra TechIqra Tech
'Floor_Plan_Mapping__c where Edition_Event_Name__c IN :criteria.split( ',' );
Iqra TechIqra Tech
but i am getting error on the above statment
Rajesh_KumarRajesh_Kumar
Below code working for me. Change it as per your need.
String nameStr = 'Test,Rajesh';
List<String> nameList = nameStr.split(',');
System.debug(Database.query('SELECT Id, Name FROM Account WHERE Name IN :nameList'));

 
Iqra TechIqra Tech
String nameStr = 'Test,Rajesh';
List<String> nameList = nameStr.split(',');
System.debug(Database.query('SELECT Id, Name FROM Account WHERE Name IN :nameList'));

yes but what if String nameStr = 'Test,Rajesh'; insted of String nameStr = 'xyz,pqr';  came that why need to dynamically can it be possible??
Rajesh_KumarRajesh_Kumar
For example, I wrote that code. You can bind the nameStr from VF page as you did with criteria.
Iqra TechIqra Tech
actually we storeing vf page values in our custom FLoor plan object ('Floor_Plan_Mapping__c) and this field
 Edition_Event_Name__c stroing our values like  FM Expo 2017 this or  FM Expo 2017,FM Expo 2018  tahts why we need to do dynamically i am unble to understand how to do that can you write that one...
Rajesh_KumarRajesh_Kumar
1.Query on Field plan mapping.
Floor_Plan_Mapping__c fpmRec = [SELECT Id, Edition_Event_Name__c FROM Floor_Plan_Mapping__c WHERE Edition_Event_Name__c != null LIMIT 1 ];
2. Query on Opportunity Line Item
if(fpmRec != null && fpmRec.Edition_Event_Name__c != null){
    List<String> editionList = fpmRec.Edition_Event_Name__c.split(',');
    List<opportunitylineitem> oppItemList = [SELECT Id, P_Edition__c  FROM 
      opportunitylineitem WHERE P_Edition__c IN :editionList];
}

 
Iqra TechIqra Tech
How to create schedule class for this batch class for shechedulling?
Iqra TechIqra Tech
And test class to for this Batch class??