2

I'm fairly new to Apex and I am completely at a loss at what to do next. So far I was able to grab and deserialize my JSON I grabbed from a Web Service(This does a GET Method for info for salespeople). Now what I want to do is that I want to insert (by dml & loop?) my deserialized Salesperson JSON into my SF Custom Object called: Salesperson__c. The fieldnames are the same. Any suggestions or especially example code will be helpful. Thank you, appreciate the help and input.

JSON below:

{
"Collection": {
    "Entities": [
        {
            "WineryId": "OWS",
            "Id": "JIM   ",
            "Name": "Jim Edmonds",
            "Street": "123 Wrecks St",
            "City": "Houston",
            "State": "TX",
            "Zipcode": "77001",
            "Telephone": "7135629245"
        },
        {
            "WineryId": "OWS",
            "Id": "CHOW ",
            "Name": "Robert Chow",
            "Street": "456 Checkers St.",
            "City": "Artesia",
            "State": "CA",
            "Zipcode": "90701",
            "Telephone": "5625552345"
        }
    ],
    "TotalCount": 2
}

}

My Class generated by JSON2Apex:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

    public class WIMSGetSalesperson {

        public class Collection {
            public List<Entities> Entities;
            public Integer TotalCount;
        }

        public class Entities {
            public String WineryId;
            public String Id;
            public String Name;
            public String Street;
            public String City;
            public String State;
            public String Zipcode;
            public String Telephone;
        }

        public Collection Collection;


        public static WIMSGetSalesperson parse(String json) {
            return (WIMSGetSalesperson) System.JSON.deserialize(json, WIMSGetSalesperson.class);
        }

        static testMethod void testParse() {
            String json = '{\"Collection\":{\"Entities\":[{\"WineryId\":\"OWS\",\"Id\":\"JIM   \",\"Name\":\"Jim Edmonds\",\"Street\":\"123 Wrecks St\",\"City\":\"Houston\",\"State\":\"TX\",\"Zipcode\":\"77001\",\"Telephone\":\"7135629245\"},{\"WineryId\":\"OWS\",\"Id\":\"CHOW \",\"Name\":\"Robert Chow\",\"Street\":\"456 Checkers St.\",\"City\":\"Artesia\",\"State\":\"CA\",\"Zipcode\":\"90701\",\"Telephone\":\"5625552345\"}],\"TotalCount\":2}}';
            WIMSGetSalesperson obj = parse(json);
            System.assert(obj != null);
        }
    }
5

Well, you do not want to do DML in a loop. Thats an apex anti-pattern and will quickly run you afoul of governor limits.

What you need to do instead, is use a for loop to iterate over the collection of Entities building sObjects from them, and adding them to a list. then, with one dml call, insert that entire list. here's some psuedo code:

List<sObjectName__c> toInsert = new List<sObjectName__c>();
for(classname.entities e: collection.entities) {
  sObjectName__c n = new sObjectName__c();
  n.WineryId = e.WineryId;
  n.SomethingOtherThanID = e.Id;
  ...
  toInsert.add(n);
}
try {
  insert toInsert;
} catch(DMLException dmle) {
  // do something with the exception here
}
  • Note well, the bit about somethingOtherThanId there. You're Entity subclass has an Id field, but your sObject cannot include iD as a field for inserting – Kevin P Oct 11 '13 at 17:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.