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
Jerry ClifftJerry Clifft 

JSON not adding individual records, instead repeats same record * number of unique records.

Hoping for some help on this one, I think it might be an easy one.

I have an @future REST API callout that just sends some data. (It works just fine)

I have 1 Opportunity with a check box, when checked, the API is called.
Master/Detail to Opportunity is Equipment__c.

I have/will have between 1 and 100 peices of equipment per Opportunity.

The below class works, except for the equipment part, instead of listing the unique equipment, it repeats the same peice of equipment over and over (If there are 3 unique pieces of equipment, it will repeat the same peice of equipment 3x. The debug log indicates the query worked fine and the equipment is there, but this peice is not doing what I want it to do.

    jsonObj.writeStartObject();
        jsonObj.writeFieldName('EquipmentList');
        jsonObj.writeStartArray();
            jsonObj.writeStartObject();
                jsonObj.writeStringField('SiteID', E[0].Id);
            jsonObj.writeEndObject();

        jsonObj.writeEndArray();
             jsonObj.writeEndObject();
            
Reults are:           
        {
          "EquipmentList": [
            {
              "SalesForcEquipmentID": "a0f18000001YetmAAC",
              "SalesForcCAID": "R1234567890",
              "SalesForcSmartCard": "S1234567890"
            }
          ]
        },
        {
          "EquipmentList": [
            {
              "SalesForcEquipmentID": "a0f18000001YetmAAC",
              "SalesForcCAID": "R1234567890",
              "SalesForcSmartCard": "S1234567890"
            }
          ]
        },
        {
          "EquipmentList": [
            {
              "SalesForcEquipmentID": "a0f18000001YetmAAC",
              "SalesForcCAID": "R1234567890",
              "SalesForcSmartCard": "S1234567890"
            }

Instead of:
        {
          "EquipmentList": [
            {
              "SalesForcEquipmentID": "a0f18000001YgrmAAD",
              "SalesForcCAID": "R2345678901",
              "SalesForcSmartCard": "S2345678901"
            }
          ]
        },
        {
          "EquipmentList": [
            {
              "SalesForcEquipmentID": "a0f18000001YlmmAAA",
              "SalesForcCAID": "R3456789012",
              "SalesForcSmartCard": "S3456789012"
            }
          ]
        },
        {
          "EquipmentList": [
            {
              "SalesForcEquipmentID": "a0f18000001YetmAAC",
              "SalesForcCAID": "R1234567890",
              "SalesForcSmartCard": "S1234567890"
            }            

            
Here is the class:
public class AmDocs_CalloutClass_BulkCreate {

    @future(callout=true)

    public static void AmDocsMakeCalloutCreateBulk(String Id) {

 list<Opportunity> O = [select id, Name, AmDocs_Site_Id__c from Opportunity where Id = :id ];
     
 list<Equipment__c> E = [select id, Opportunity__c from Equipment__c where Opportunity__c = :id ];

JSONGenerator jsonObj = JSON.createGenerator(true);
    jsonObj.writeStartObject();
    jsonObj.writeFieldName('CreateBulk');
    jsonObj.writeStartArray();
        jsonObj.writeStartObject();
        jsonObj.writeFieldName('Property / Site');
        jsonObj.writeStartArray();
            jsonObj.writeStartObject();
                jsonObj.writeStringField('SiteID', O[0].AmDocs_Site_Id__c);
                jsonObj.writeStringField('CustomerID', O[0].Name);
                jsonObj.writeStringField('SalesforceID', O[0].Id);
            jsonObj.writeEndObject();
            jsonObj.writeStartObject();
                jsonObj.writeFieldName('EquipmentList');
                jsonObj.writeStartArray();
                    jsonObj.writeStartObject();
                        jsonObj.writeStringField('SiteID', E[0].Id);
                    jsonObj.writeEndObject();
                jsonObj.writeEndArray();
           jsonObj.writeEndObject();
           jsonObj.writeEndArray();  
    jsonObj.writeEndObject();
    String finalJSON = jsonObj.getAsString();
   
        HttpRequest request = new HttpRequest();
            String endpoint = 'http://putsreq.com/KZ2kmwfUvrIf5ARJE05h';
                 request.setEndPoint(endpoint);        
                 request.setBody(jsonObj.getAsString());  
                 request.setHeader('Content-Type', 'application/json');
                 request.setMethod('POST');

         HttpResponse response = new HTTP().send(request);
                 System.debug(response.toString());
                 System.debug('STATUS:'+response.getStatus());
                 System.debug('STATUS_CODE:'+response.getStatusCode());
                 System.debug(response.getBody());
}
}

Thanks for your help.
Jerry
Best Answer chosen by Jerry Clifft
James BoggsJames Boggs
You don't seem to be looping through your list of query results, just using the very first one, E[0].

All Answers

James BoggsJames Boggs
You don't seem to be looping through your list of query results, just using the very first one, E[0].
This was selected as the best answer
Jerry ClifftJerry Clifft
You're a genius James Boggs! Thanks for your help. Per your suggestion, I changed: for(Equipment__c EList: E) { jsonObj.writeStartObject(); jsonObj.writeStringField('SalesForcEquipmentID', E[0].Id); jsonObj.writeStringField('SalesForcCAID', E[0].Name); jsonObj.writeStringField('SalesForcSmartCard', E[0].Receiver_S__c); jsonObj.writeEndObject(); } To this: for(Integer i = 0; i < E.size(); i++){ jsonObj.writeStartObject(); jsonObj.writeStringField('SalesForcEquipmentID', E[i].Id); jsonObj.writeStringField('SalesForcCAID', E[i].Name); jsonObj.writeStringField('SalesForcSmartCard', E[i].Receiver_S__c); jsonObj.writeEndObject(); } And it now works perfectly. Thanks again! Jerry
James BoggsJames Boggs

Glad to help :D

If you'd kindly mark as solved I'd appreciate it!