function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Iqra TechIqra Tech 

need to convert this class into the batch class beacuse of governer limits & scheduling the batch

public class My_2nd_Api {
    
    Public static void Insert_Records(){
   
        
List<Opportunity> oppbatch=[select id,name,stageName, Edition__r.name,                          
                            (select id, name from OpportunityLineItems  ), 
                            (Select contact.firstname,contact.lastname,contact.Email,contact.Phone,contact.Salutation,contact.title  from OpportunityContactRoles) 
                           from Opportunity where Edition__r.name ='FM EXPO 2017' AND  HasOpportunityLineItem = true ];
                            
        system.debug('oppbatch List query.. '+oppbatch);
        
List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
for(Opportunity opp : oppbatch) 
{
 system.debug('Chehking'+opp.OpportunityLineItems);
 OppWrapper oppwrappervar = new OppWrapper(opp.id,opp.name,opp.StageName,opp.Edition__r.name,                                        
                                                                                  
                                           null, null, null, null, null); 

    
    for(OpportunityLineItem li : opp.OpportunityLineItems)
    {    
        oppwrappervar.SFDC_Record_Unique_ID=li.id;
        oppwrappervar.Product_Name=li.name;
              
    }
   for(OpportunityContactRole ocr : opp.OpportunityContactRoles)
    {
        oppwrappervar.First_Name = ocr.contact.firstname;
        oppwrappervar.Last_Name =ocr.contact.lastname ;
        oppwrappervar.Email=ocr.contact.Email;
        oppwrappervar.Mobile=ocr.contact.Phone;
        oppwrappervar.Salutation=ocr.contact.Salutation;
        oppwrappervar.Job_Title=ocr.contact.title;        
    }
    
oppWrapperVarList.add(oppwrappervar);
}
 string jsonbody=Json.serialize(oppWrapperVarList);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        //request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setEndpoint('http://sfdata.iqratechnology.com/api/fs_metadata');

        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
        //request.setHeader('Content-Type','multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW');
        //request.setHeader('Authorization','Bearer '+ AuthorizationToken);
//request.setBody('[{ "RecordId": "456","RecordName": "moosa traders pvt ltd / The Big 5 Construct Egypt 2018 /  / 093061123", "EventName": "a2JD0000001ns6uMAA U", "RecordStage": "Test Stage Amjad","BannerName": "Test Banner Amjad","StandNo": "Test Stand Amjad"}]');
        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 SFDC_RecordID {get;set;}
        public String SFDC_RecordName {get;set;}
        public String SFDC_RecordStage {get;set;}
        public String SFDC_EventName {get;set;}       
        public String SFDC_Record_Unique_ID {get;set;}
        public String Product_Name {get;set;}       
        public String First_Name {get;set;}
        public String Last_Name {get;set;}
        public String Email {get;set;}
        public String Mobile {get;set;}
        public String Salutation {get;set;}
        public String Job_Title {get;set;}     
        
       
        
        public OppWrapper(String SFDC_RecordID,String SFDC_RecordName,String SFDC_RecordStage,String SFDC_EventName,
                          String SFDC_Record_Unique_ID,String Product_Name,String First_Name,String Last_Name,String Email,String Mobile,String Salutation,
                          String Job_Title)
        {
            this.SFDC_RecordID =SFDC_RecordID;
            this.SFDC_RecordName =SFDC_RecordName;
            this.SFDC_RecordStage =SFDC_RecordStage;
            this.SFDC_EventName=SFDC_EventName;           
            this.SFDC_Record_Unique_ID=SFDC_Record_Unique_ID;
            this.Product_Name=Product_Name;          
            this.First_Name=First_Name;
            this.Last_Name=Last_Name;
            this.Email=Email;
            this.Mobile=Mobile;
            this.Salutation=Salutation;
            this.Job_Title=Job_Title;
            
            
        }
    }
    
}

i have created this class for posting the data in third party API but facing governer limits now i need to convert this class into the batch class can any one please help me to achive this requiremnt need urgent please help...........
Harsh P.Harsh P.
Hi Iqra,
 Instead of contvert whole class we can call that class from batch class.
You can call like below:

In start mtd you need to insert an your query.
global void execute(Database.BatchableContext BC, List<sObject> scope) {

My_2nd_Api  instance = new My_2nd_Api ();
instance.Insert_Records();

}
Iqra TechIqra Tech
@Harsh P. thanks for the reply 
where did i need to add this can you please show me please........ like adding in my code... please
Iqra TechIqra Tech
ok ok got it you i need to call this class into the batch class right?
Harsh P.Harsh P.
Try the below one:


global class My_2nd_ApiBatch implements Database.Batchable<sObject>{
global string edition = 'FM EXPO 2017';
global string hasLineItem = 'true';
global Database.QueryLocator start(Database.BatchableContext BC){
    
string oppbatch='select id,name,stageName, Edition__r.name,(select id, name from OpportunityLineItems),(Select contact.firstname,contact.lastname,contact.Email,contact.Phone,contact.Salutation,contact.title  from OpportunityContactRoles) from Opportunity where Edition__r.name =:edition AND  HasOpportunityLineItem =: hasLineItem';

    return Database.getQueryLocator(oppbatch);
}

global void execute(Database.BatchableContext BC, List<Opportunity> scope){
        
List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
for(Opportunity opp : scope) 
{
 system.debug('Chehking'+opp.OpportunityLineItems);
 OppWrapper oppwrappervar = new OppWrapper(opp.id,opp.name,opp.StageName,opp.Edition__r.name,null, null, null, null, null); 

    
    for(OpportunityLineItem li : opp.OpportunityLineItems)
    {    
        oppwrappervar.SFDC_Record_Unique_ID=li.id;
        oppwrappervar.Product_Name=li.name;
              
    }
   for(OpportunityContactRole ocr : opp.OpportunityContactRoles)
    {
        oppwrappervar.First_Name = ocr.contact.firstname;
        oppwrappervar.Last_Name =ocr.contact.lastname ;
        oppwrappervar.Email=ocr.contact.Email;
        oppwrappervar.Mobile=ocr.contact.Phone;
        oppwrappervar.Salutation=ocr.contact.Salutation;
        oppwrappervar.Job_Title=ocr.contact.title;        
    }
    
oppWrapperVarList.add(oppwrappervar);
}
 string jsonbody=Json.serialize(oppWrapperVarList);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        //request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setEndpoint('http://sfdata.iqratechnology.com/api/fs_metadata');

        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
        //request.setHeader('Content-Type','multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW');
        //request.setHeader('Authorization','Bearer '+ AuthorizationToken);
//request.setBody('[{ "RecordId": "456","RecordName": "moosa traders pvt ltd / The Big 5 Construct Egypt 2018 /  / 093061123", "EventName": "a2JD0000001ns6uMAA U", "RecordStage": "Test Stage Amjad","BannerName": "Test Banner Amjad","StandNo": "Test Stand Amjad"}]');
        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());
}
        
}

global void finish(Database.BatchableContext BC){

}
   Public class OppWrapper
    {
        public String SFDC_RecordID {get;set;}
        public String SFDC_RecordName {get;set;}
        public String SFDC_RecordStage {get;set;}
        public String SFDC_EventName {get;set;}       
        public String SFDC_Record_Unique_ID {get;set;}
        public String Product_Name {get;set;}       
        public String First_Name {get;set;}
        public String Last_Name {get;set;}
        public String Email {get;set;}
        public String Mobile {get;set;}
        public String Salutation {get;set;}
        public String Job_Title {get;set;}     
        
       
        
        public OppWrapper(String SFDC_RecordID,String SFDC_RecordName,String SFDC_RecordStage,String SFDC_EventName,
                          String SFDC_Record_Unique_ID,String Product_Name,String First_Name,String Last_Name,String Email,String Mobile,String Salutation,
                          String Job_Title)
        {
            this.SFDC_RecordID =SFDC_RecordID;
            this.SFDC_RecordName =SFDC_RecordName;
            this.SFDC_RecordStage =SFDC_RecordStage;
            this.SFDC_EventName=SFDC_EventName;           
            this.SFDC_Record_Unique_ID=SFDC_Record_Unique_ID;
            this.Product_Name=Product_Name;          
            this.First_Name=First_Name;
            this.Last_Name=Last_Name;
            this.Email=Email;
            this.Mobile=Mobile;
            this.Salutation=Salutation;
            this.Job_Title=Job_Title;
            
            
        }
    }
    
}

Hope it will work .!
 
Iqra TechIqra Tech
global class My_Final_ApiBatch implements Database.Batchable<sObject> //ERROR :- Class My_Final_ApiBatch must implement the method: void   Database.Batchable<SObject>.finish(Database.BatchableContext)
{
global string edition = 'FM EXPO 2017';
global string hasLineItem = 'true';
global Database.QueryLocator start(Database.BatchableContext BC){
    
string oppbatch= 'select id,name,stageName,Edition__r.name,Edition__c,Sub_Allocation_Name__r.name,Account.Account_Long_Name__c,Account.fax,Account.Website,Account.BillingCountry,Account.BillingState,Account.BillingCity,Account.BillingStreet,Account.BillingPostalcode,Account.Banner_Name__c,(select Unique_id__c,Product_Sub_Category_New__c,stand_Number_s__c,Hall_s__c,Type_of_Stand__c,Stand_Depth__c,Stand_Width__c,Contractual_Sq_M__c,Exhibitor_Id__c,Open_Sides__c from OpportunityLineItems  where Unique_id__c != null),(Select contact.firstname,contact.lastname,contact.Email,contact.Phone,contact.Salutation,contact.title  from OpportunityContactRoles) from Opportunity where    Edition__r.name  :edition AND  HasOpportunityLineItem =: hasLineItem';

    return Database.getQueryLocator(oppbatch);
}

global void execute(Database.BatchableContext BC, List<Opportunity> scope){
        
List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
for(Opportunity opp : scope) 
{
 system.debug('Chehking'+opp.OpportunityLineItems);
 OppWrapper oppwrappervar = new OppWrapper(opp.id,opp.name,opp.StageName,opp.Edition__r.name,opp.Edition__c,opp.Sub_Allocation_Name__r.name,
                                           opp.Account.Account_Long_Name__c,opp.Account.Fax,opp.Account.Website,opp.Account.BillingCountry,opp.Account.BillingState,
                                           opp.Account.BillingCity,opp.Account.BillingStreet,opp.Account.BillingPostalcode,opp.Account.Banner_Name__c,                                            
                                           null, null, null, null, null, null, null, null, null, null, null, null, null, null); 

    
    for(OpportunityLineItem li : opp.OpportunityLineItems)
    {    
        oppwrappervar.SFDC_Record_Unique_ID=li.Unique_id__c;
        oppwrappervar.Product_Subcategory=li.Product_Sub_Category_New__c;
        oppwrappervar.Stand_no=li.Stand_Number_s__c;
        oppwrappervar.Hall=li.Hall_s__c;
        oppwrappervar.Type_of_Stand=li.Type_of_Stand__c;
        oppwrappervar.Height=li.Stand_Depth__c;
        oppwrappervar.Width=li.Stand_Width__c;
        oppwrappervar.Contractual_Sq_M=li.Contractual_Sq_M__c;
              
    }
   for(OpportunityContactRole ocr : opp.OpportunityContactRoles)
    {              
        oppwrappervar.First_Name = ocr.contact.firstname;
        oppwrappervar.Last_Name =ocr.contact.lastname ;
        oppwrappervar.Email=ocr.contact.Email;
        oppwrappervar.Mobile=ocr.contact.Phone;
        oppwrappervar.Salutation=ocr.contact.Salutation;
        oppwrappervar.Job_Title=ocr.contact.title;     
    
oppWrapperVarList.add(oppwrappervar);
}
 string jsonbody=Json.serialize(oppWrapperVarList);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        //request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setEndpoint('http://sfdata.iqratechnology.com/api/fs_metadata');

        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
        //request.setHeader('Content-Type','multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW');
        //request.setHeader('Authorization','Bearer '+ AuthorizationToken);
//request.setBody('[{ "RecordId": "456","RecordName": "moosa traders pvt ltd / The Big 5 Construct Egypt 2018 /  / 093061123", "EventName": "a2JD0000001ns6uMAA U", "RecordStage": "Test Stage Amjad","BannerName": "Test Banner Amjad","StandNo": "Test Stand Amjad"}]');
        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());
}
        
}

global void finish(Database.BatchableContext BC){ // ERROR :- Unexpected token 'global'. AND Missing ';' at 'finish' AND  Extra ')', at 'BC'.

}
   Public class OppWrapper // ERROR :- Unexpected token 'Public'.
    {
     public String SFDC_RecordID {get;set;}
        public String SFDC_RecordName {get;set;}
        public String SFDC_RecordStage {get;set;}
        public String SFDC_EventName {get;set;}
        public String SFDC_EditionId {get;set;}
        public String Stan_Sub_Allocation_Name {get;set;}
        public String Acount_Long_Name {get;set;}
        public String Fax {get;set;}
        public String Web_Site {get;set;}
        public String Country {get;set;}
        public String Country_State {get;set;}
        public String City {get;set;}
        public String Street {get;set;}
        public String Post_code {get;set;}
        public String Banner_Name {get;set;}
        public String SFDC_Record_Unique_ID {get;set;}
        public String Product_Subcategory {get;set;}
        public String Stand_no {get;set;}
        public String Hall {get;set;}
        public String Type_of_Stand {get;set;}
        public Decimal Height {get;set;}
        public Decimal Width {get;set;}
        public Decimal Contractual_Sq_M {get;set;}
        public String First_Name {get;set;}
        public String Last_Name {get;set;}
        public String Email {get;set;}
        public String Mobile {get;set;}
        public String Salutation {get;set;}
        public String Job_Title {get;set;}       
        
       
        
        public OppWrapper(String SFDC_RecordID,String SFDC_RecordName,String SFDC_RecordStage,String SFDC_EventName,String SFDC_EditionId,String Stan_Sub_Allocation_Name,
                          String Acount_Long_Name,String Fax,String Web_Site,String Country,String Country_State,String City,String Street,String Post_code,String Banner_Name,
                          String SFDC_Record_Unique_ID,String Product_Subcategory,String Stand_no,String Hall,String Type_of_Stand,Decimal Height,
                          Decimal Width,Decimal Contractual_Sq_M,String First_Name,String Last_Name,String Email,String Mobile,String Salutation,
                          String Job_Title)
        {
             this.SFDC_RecordID =SFDC_RecordID;
            this.SFDC_RecordName =SFDC_RecordName;
            this.SFDC_RecordStage =SFDC_RecordStage;
            this.SFDC_EventName=SFDC_EventName;
            this.SFDC_EditionId=SFDC_EditionId;
            this.Stan_Sub_Allocation_Name=Stan_Sub_Allocation_Name;
            this.Acount_Long_Name=Acount_Long_Name;
            this.Fax=Fax;
            this.Web_Site=Web_Site;
            this.Country=Country;
            this.Country_State=Country_State;
            this.City=City;
            this.Street=Street;
            this.Post_code=Post_code;
            this.Banner_Name=Banner_Name;
            this.SFDC_Record_Unique_ID=SFDC_Record_Unique_ID;
            this.Product_Subcategory=Product_Subcategory;
            this.Stand_no=Stand_no;
            this.Hall=Hall;
            this.Type_of_Stand=Type_of_Stand;
            this.Height=Height;
            this.Width=Width;
            this.Contractual_Sq_M=Contractual_Sq_M;
            this.First_Name=First_Name;
            this.Last_Name=Last_Name;
            this.Email=Email;
            this.Mobile=Mobile;
            this.Salutation=Salutation;
            this.Job_Title=Job_Title;
            
        }
    }
    
}

@Harsh P.  hii

getting too many error can you please look into that....
i have marked errors in bold lines please chehk this......... thanks