You need to sign in to do that
Don't have an account?

How to add data in custom objects in Apex
I am reading Json from a url the output of the JSON is like below
JSON String:
{"id":"1","name":"asdadsra","username":"karura","password":"0s01","supplierid":"3","pin":"pin","location":"Kiambu","telephone":"333333","email":"abc@test.co","gender":"1","type":"1","lastLogin":"1412770127","createtime":"0","salesforcestatus":"1"}
And i want to add the data in Custom Object: Shop__c
Custom Field:
id : Shop_ID__c
name : Name
username : User_Name__c
password : Password__c
supplierid : Supplier_ID__c
pin : Pin__c
location : Location__c
telephone : Telephone__c
email : Email__c
gender : Gender__c
type : Type__c
lastLogin : Last_Login__c
createtime : Create_Time__c
salesforcestatus : Salesforce_Status__c
Please advise how to add data in custom objects.
JSON String:
{"id":"1","name":"asdadsra","username":"karura","password":"0s01","supplierid":"3","pin":"pin","location":"Kiambu","telephone":"333333","email":"abc@test.co","gender":"1","type":"1","lastLogin":"1412770127","createtime":"0","salesforcestatus":"1"}
And i want to add the data in Custom Object: Shop__c
Custom Field:
id : Shop_ID__c
name : Name
username : User_Name__c
password : Password__c
supplierid : Supplier_ID__c
pin : Pin__c
location : Location__c
telephone : Telephone__c
email : Email__c
gender : Gender__c
type : Type__c
lastLogin : Last_Login__c
createtime : Create_Time__c
salesforcestatus : Salesforce_Status__c
Please advise how to add data in custom objects.
Thanks
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_class_System_JsonParser.htm#apex_System_JsonParser_readValueAs
Please check attached Image to see error.
Code:
global class ShopRecordsCtrlNew
{
public List<JSONParse> jsonList{get;set;}
public List<Shop__c> objList;
public static void createRecord()
{
String jsonString = '[{"id":"1","name":"asdadsra","username":"karura","password":"0s01","supplierid":"3","pin":"pin","location":"Kiambu","telephone":"333333","email":"abc@test.co","gender":"1","type":"1","lastLogin":"1412770127","createtime":"0","salesforcestatus":"1"}]';
List<SObject> jsonList = new List<SObject>();
List<Shop__c> objList= new List<Shop__c>();
jsonList = (List<SObject>) System.JSON.deserialize(jsonString, List<JSONParse>.class);
for(JSONParse jp: jsonList)
{
Shop__c objInstance = new Shop__c();
objInstance.Shop_ID__c = jp.Id;
objList.add(objInstance);
}
insert objList;
}
global class JSONParse
{
public Decimal id;
public String name;
public String username;
public String password;
public String supplierid;
public String pin;
public String location;
public String telephone;
public String email;
public String gender;
public String type;
public String lastLogin;
public String createtime;
public String salesforcestatus;
}
}
Hi Sam, u have error becouse you using List<SObject> instead of List<JSONParse>.