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 

i am not able to post my all records from custom object to thrid party api please help

This class is working fine it feteches data from Opportunity_Prod_Batch_Record__c (custom object ) and stored the records in third party api but in my custom object having lost of records but when i run this class manually then it is taking randamly only one record i need all the records to post in third party api how can i achive this please help..............


public class Flr_Insert_Records {
    
    Public static void Insert_Records(){
        /* Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPData');
request.setMethod('POST');
request.setHeader('Content-Type','application/json');
//request.setHeader('Content-Type','multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW');
//request.setHeader('Authorization','Bearer '+ AuthorizationToken);*/
        
        List<Opportunity_Prod_Batch_Record__c> oppbatch=[select Edition_Name__c,Opportunity_Id__c,Opportunity_Name__c,Opportunity_Stage__c,Stand_No__c,Banner_name__c
                                                         from Opportunity_Prod_Batch_Record__c  ];
        system.debug('oppbatch List query '+oppbatch);
        // system.debug('Query after serialization '+json.serialize(oppbatch));
        OppWrapper oppwrappervar=new OppWrapper(oppbatch[0].Edition_Name__c,oppbatch[0].Opportunity_Id__c,oppbatch[0].Opportunity_Name__c,
                                                oppbatch[0].Opportunity_Stage__c,oppbatch[0].Stand_No__c,oppbatch[0].Banner_name__c);
        string jsonbody=Json.serialize(oppwrappervar);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPD');
        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 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 OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String StandNo,String BannerName)
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            
        }
    }
    
}

ex:- in my custom object having 8 records but when i run the class it is posting only one recrod so plesse help to post all records in third party api need urgent........
Best Answer chosen by Iqra Tech
Meghna Vijay 7Meghna Vijay 7
Hi,
List<Opportunity_Prod_Batch_Record__c> oppbatch=[select Edition_Name__c,Opportunity_Id__c,Opportunity_Name__c,Opportunity_Stage__c,Stand_No__c,Banner_name__c from Opportunity_Prod_Batch_Record__c  ];
system.debug('oppbatch List query '+oppbatch);
        // system.debug('Query after serialization '+json.serialize(oppbatch));
// Here you are fetching only 1 record of that oppbatch i.e. oppbatch[0];        
OppWrapper oppwrappervar=new OppWrapper(oppbatch[0].Edition_Name__c,oppbatch[0].Opportunity_Id__c,oppbatch[0].Opportunity_Name__c,
                                                oppbatch[0].Opportunity_Stage__c,oppbatch[0].Stand_No__c,oppbatch[0].Banner_name__c); // Wrong code

// Right code 
List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
for(Opportunity_Prod_Batch_Record__c oppbtch : oppbatch) {
    OppWrapper oppwrappervar=new OppWrapper(oppbtch .Edition_Name__c,oppbtch .Opportunity_Id__c,oppbtch .Opportunity_Name__c, oppbtch.Opportunity_Stage__c,oppbtch.Stand_No__c,oppbtch.Banner_name__c);
oppWrapperVarList.add(oppwrappervar);
}
 string jsonbody=Json.serialize(oppWrapperVarList);


Hope it helps, if it does mark it as solved to keep this community clean.
Thanks
 

All Answers

Meghna Vijay 7Meghna Vijay 7
Hi,
List<Opportunity_Prod_Batch_Record__c> oppbatch=[select Edition_Name__c,Opportunity_Id__c,Opportunity_Name__c,Opportunity_Stage__c,Stand_No__c,Banner_name__c from Opportunity_Prod_Batch_Record__c  ];
system.debug('oppbatch List query '+oppbatch);
        // system.debug('Query after serialization '+json.serialize(oppbatch));
// Here you are fetching only 1 record of that oppbatch i.e. oppbatch[0];        
OppWrapper oppwrappervar=new OppWrapper(oppbatch[0].Edition_Name__c,oppbatch[0].Opportunity_Id__c,oppbatch[0].Opportunity_Name__c,
                                                oppbatch[0].Opportunity_Stage__c,oppbatch[0].Stand_No__c,oppbatch[0].Banner_name__c); // Wrong code

// Right code 
List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
for(Opportunity_Prod_Batch_Record__c oppbtch : oppbatch) {
    OppWrapper oppwrappervar=new OppWrapper(oppbtch .Edition_Name__c,oppbtch .Opportunity_Id__c,oppbtch .Opportunity_Name__c, oppbtch.Opportunity_Stage__c,oppbtch.Stand_No__c,oppbtch.Banner_name__c);
oppWrapperVarList.add(oppwrappervar);
}
 string jsonbody=Json.serialize(oppWrapperVarList);


Hope it helps, if it does mark it as solved to keep this community clean.
Thanks
 
This was selected as the best answer
Iqra TechIqra Tech
yes it working fine but records was not post in third party api User-added image

see this screen shot records are not posted in api why so?
Meghna Vijay 7Meghna Vijay 7
Hi,
But your records are getting created on the third party API as 200 status code is returning. To check if the records are created in the third party use 'GET' method to retrieve those records and response body not POST.
Hope it helps, if it does, mark it as solved.
Thanks
 
Iqra TechIqra Tech
yes i chehked in third party api but there is no records are created 
Meghna Vijay 7Meghna Vijay 7
How about you change request.setBody('['+jsonbody+']') to request.setBody(jsonbody) ?
 
Iqra TechIqra Tech
when one record is created the response body shows like this please chehk this screen shot

User-added image
Iqra TechIqra Tech

public class Flr_Insert_Records {
    
    Public static void Insert_Records(){
        /* Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPData');
request.setMethod('POST');
request.setHeader('Content-Type','application/json');
//request.setHeader('Content-Type','multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW');
//request.setHeader('Authorization','Bearer '+ AuthorizationToken);*/
        
        List<Opportunity_Prod_Batch_Record__c> oppbatch=[select Edition_Name__c,Opportunity_Id__c,Opportunity_Name__c,Opportunity_Stage__c,Stand_No__c,Banner_name__c
                                                         from Opportunity_Prod_Batch_Record__c  ];
        system.debug('oppbatch List query '+oppbatch);
        // system.debug('Query after serialization '+json.serialize(oppbatch));
        /*OppWrapper oppwrappervar=new OppWrapper(oppbatch[0].Edition_Name__c,oppbatch[0].Opportunity_Id__c,oppbatch[0].Opportunity_Name__c,
                                                oppbatch[0].Opportunity_Stage__c,oppbatch[0].Stand_No__c,oppbatch[0].Banner_name__c);
        string jsonbody=Json.serialize(oppwrappervar);*/
        // Right code 
List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
for(Opportunity_Prod_Batch_Record__c oppbtch : oppbatch) {
   OppWrapper oppwrappervar=new OppWrapper(oppbtch.Edition_Name__c,oppbtch.Opportunity_Id__c,oppbtch.Opportunity_Name__c,oppbtch.Opportunity_Stage__c,oppbtch.Stand_No__c,oppbtch.Banner_name__c);
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/FP');
        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 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 OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String StandNo,String BannerName)
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            
        }
    }
    
}

 

after added your code in my http callout class please chek this where did i miss something?

Meghna Vijay 7Meghna Vijay 7
You are seeing debugs only there'll be some error logs in the dev console. Can you try with request.setBody(jsonbody) instead of request.setBody('['+jsonbody+']')?
 
Iqra TechIqra Tech

yes finally it is working but now i need to do one thing what if record alredy exists

 

see this User-added image

Iqra TechIqra Tech
thanks alot @Meghna Vijay 7 can you do a one favor for me what if record alredy exsits then skipp that record and insert remaing record how can we achive that?
Meghna Vijay 7Meghna Vijay 7
Do not send recordId in the JSON. 
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 OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String StandNo,String BannerName)
        {
            this.EventName =EventName;
           // this.RecordId =RecordId; --> Comment it
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            
        }
    }
    
}
Iqra TechIqra Tech
no no i need take record id must beacuse in record id we storing Opp id so that is importnat i need to do if record is exitis then it will skeep and store the rmaing records that need to i have to achive that
Meghna Vijay 7Meghna Vijay 7
thanks alot @Meghna Vijay 7 can you do a one favor for me what if record alredy exsits then skipp that record and insert remaing record how can we achive that? :- If that's the case, then you may have to use GET method to retrieve those records and compare it with your org records and filter those records.

 
Iqra TechIqra Tech

If that's the case, then you may have to use GET method to retrieve those records and compare it with your org records and filter those records.

 

ohhh ok can you please help me on that as i am started learing and working on intergation part so i need your help too to achive the my last requirment..

Meghna Vijay 7Meghna Vijay 7
Is there a uniqueId (External Key) on dev org on Opportunity_Prod_Batch_Record__c Object based on which you'll filter those records?
 
Iqra TechIqra Tech
Is there a uniqueId (External Key) on dev org on Opportunity_Prod_Batch_Record__c Object based on which you'll filter those records?

yes based we are storeing opp line item records in Opportunity_Prod_Batch_Record__c Object  and we also stored OPP id in  Opportunity_Prod_Batch_Record__c Object  and now as a record id we are posting opp id in third party api
Iqra TechIqra Tech


@Meghna Vijay 7 

 

please help me i am stuck on it

Meghna Vijay 7Meghna Vijay 7
Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPD'); // End point for getting records
        request.setMethod('GET');
        request.setHeader('Content-Type','application/json');
HttpResponse response = http.send(request);
oppWrapperVarList = JSON.deserialize(response.getBody(), List<OppWrapper>.class);
List<Opportunity_Batc_Records> oppBatchRecs = [SELECT blah blah from Opportunity_Batch_Records WHERE Opportunity_ID__c NOT IN: oppWrapperVarList ];
and then your code of posting.
Hope it helps.
Iqra TechIqra Tech

public class MyApiPostClass {
 
    
    Public static void Insert_Records(){
       
        
        List<Opportunity_Prod_Batch_Record__c> oppbatch=[select Edition_Name__c,Opportunity_Id__c,Opportunity_Name__c,Opportunity_Stage__c,Stand_No__c,Banner_name__c
                                                         from Opportunity_Prod_Batch_Record__c  ];
        system.debug('oppbatch List query '+oppbatch);
        
        // Right code 
List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
for(Opportunity_Prod_Batch_Record__c oppbtch : oppbatch) {
   OppWrapper oppwrappervar=new OppWrapper(oppbtch.Edition_Name__c,oppbtch.Opportunity_Id__c,oppbtch.Opportunity_Name__c,oppbtch.Opportunity_Stage__c,oppbtch.Stand_No__c,oppbtch.Banner_name__c);
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.setMethod('POST');
        request.setHeader('Content-Type','application/json');
       
        
       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 OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String StandNo,String BannerName)
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            
        }
    }
    
}

 

can you please add your code in my class as i am confused didnt understand where did i need to post this new code provided by can you please add this the last thing please..

Meghna Vijay 7Meghna Vijay 7
public class MyApiPostClass {
 
    
    Public static void Insert_Records(){
     // Changes by MV Start  
       Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPD'); // End point for getting records
        request.setMethod('GET');
        request.setHeader('Content-Type','application/json');
HttpResponse response = http.send(request);
List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
oppWrapperVarList = (List<OppWrapper>)JSON.deserialize(response.getBody(), List<OppWrapper>.class);
Set<String> recordIdSet = new Set<String>();
for(OppWrapper opp : oppWrapperVarList ) {
     if(String.isNotBlank(opp.RecordId)) {
        recordIdSet.add(opp.RecordId);
}
}


        List<Opportunity_Prod_Batch_Record__c> oppbatch=[select Edition_Name__c,Opportunity_Id__c,Opportunity_Name__c,Opportunity_Stage__c,Stand_No__c,Banner_name__c
                                                         from Opportunity_Prod_Batch_Record__c  
WHERE  Opportunity_Id__c NOT IN: recordIdSet ];
  // End       
        
        // Right code 
List<OppWrapper> oppWraperListToBeInserted = new List<OppWrapper>();
for(Opportunity_Prod_Batch_Record__c oppbtch : oppbatch) {
   OppWrapper oppwrappervar=new OppWrapper(oppbtch.Edition_Name__c,oppbtch.Opportunity_Id__c,oppbtch.Opportunity_Name__c,oppbtch.Opportunity_Stage__c,oppbtch.Stand_No__c,oppbtch.Banner_name__c);
oppWraperListToBeInserted.add(oppwrappervar);
}
 string jsonbody=Json.serialize(oppWraperListToBeInserted);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
       
        
       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 OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String StandNo,String BannerName)
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            
        }
    }
    
}

There may be some errors in the syntax . If yes, please see this documentation :-
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_httprequest.htm
Iqra TechIqra Tech
Thanks a lot @Meghna Vijay 7
i will chehk and let you know after running sucessfully nice to meet you thanks a lot once agian
Iqra TechIqra Tech
i am getting this error
Error:-
Line: 32, Column: 1
System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set