0

I have a SOAP callout to an external webservice and I'd like to retrieve the records from the external service and insert them into Salesforce. The problem I am facing is that I am unable to insert multiple records into Salesforce when I make the callout to the webservice. How can I retrieve the records from the callout, loop through each new record from the callout, and insert these records in bulk into Salesforce? Thank you.

public class ListOutboundFax {

      public static void getListOutBoundRecords(Integer customerId ){     
        Concord__c con = new Concord__c();
        List<Concord__c> lstConcord = new  List<Concord__c>();

        accountmanagementreportingwebservice.AccountManagementReportingWebserviceSoap s= new accountmanagementreportingwebservice.AccountManagementReportingWebserviceSoap();
        accountmanagementreportingwebservice.ListOutboundActivityRequest  req = new accountmanagementreportingwebservice.ListOutboundActivityRequest();
        accountmanagementreportingwebservice.ListOutboundActivityResponse res = new accountmanagementreportingwebservice.ListOutboundActivityResponse();
        accountmanagementreportingwebservice.Authentication a =new accountmanagementreportingwebservice.Authentication();
        accountmanagementreportingwebservice.ArrayOfString filter = new accountmanagementreportingwebservice.ArrayOfString();
        accountmanagementreportingwebservice.Filter filters = new accountmanagementreportingwebservice.Filter();


        a.Username = '-----------';
        a.Password = '--------'; 
        req.Authentication = a;
        req.CustomerId = customerId;
        req.EndDate = date.valueOf('2019-11-12T06:03:15-08:00');
        req.ExcludeSubLevelEntries = Boolean.valueOf('true');
        req.PageIndex = 0;
        req.PageSize = 100;
        req.SortOption = 0;
        req.StartDate = date.valueOf('2019-11-11T06:03:15-08:00');

        res = s.ListOutboundActivity(req);
        String jsonResponse = (JSON.serializePretty(res));

        System.debug(jsonResponse);
   Integer count = 0;     
   JSONParser parser = JSON.createParser(jsonResponse);
          while(parser.nextToken() != null && count < lstConcord.size()){
              if(parser.getCurrentToken() == JSONToken.FIELD_NAME){
               parser.nextValue();
                if(parser.getCurrentName() == 'id') {
                   con.JOB_ID__c = parser.getText(); 
    //               System.debug(con.Name);
                }

                 else if(parser.getCurrentName() == 'dur'){
                            con.Duration__c = Integer.valueOf(parser.getText());
    //                        System.debug(con.Duration__c);

                        }

                        else if(parser.getCurrentName() == 'pgct'){
                            con.Delivered_Pages__c = Integer.valueOf(parser.getText());
    //                        System.debug(con.Delivered_Pages__c);
                        }

                        else if(parser.getCurrentName() == 'dt'){
                            con.Stop_Date__c = Date.valueOf(parser.getText());
  //                          System.debug(con.Stop_Date__c);
                        }

                        else if(parser.getCurrentName() == 'FaxNumber'){
                            con.Fax__c = parser.getText();
 //                           System.debug(con.Fax__c);
                        }  

                   else if(parser.getCurrentName() == 'fcc'){
                            con.Sender_Country__c = parser.getText();
   //                         System.debug(con.Sender_Country__c);
                        } 

                  else if(parser.getCurrentName() == 'cid'){
                            con.Concord_Account_ID__c = parser.getText();
    //                        System.debug(con.Concord_Account_ID__c);
                        } 


                  lstConcord.add(con);
                  System.debug('Size of List' + lstConcord.size());
              }  

              System.debug('Size of List' + lstConcord.size());
              count++;


          }

                        if(lstConcord.size() > 0 ){
            try{

                insert lstConcord;
            }

            catch(Exception ex){
                System.debug(ex);
            }
        }



              }
          }
  • Please either edit your existing post or ask a new, distinct question. When you do both, it gives the appearance of posting duplicates. We need a specific description of the problem to be able to help. See How to Ask for more. – David Reed Nov 13 '19 at 20:13
  • Why are you serializing and then parsing it in JSON? Wouldn't it be much easier to just use the variable directly? – sfdcfox Nov 13 '19 at 20:17
  • Hi @sfdcfox. So instead of creating the jsonResponse varaible, I can just use the res variable and map it to my custom object's fields directly? – AL 600 Nov 13 '19 at 21:01
  • 1
    @AL600 Sure, they are just normal variables. Plus, you might understand your code better if you're not dealing with a JSON parser. – sfdcfox Nov 13 '19 at 21:02
  • @DavidReed Sorry for the confusion. I edited the post to specify the problem I'm facing. – AL 600 Nov 13 '19 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.