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
Sam1980Sam1980 

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.
Arunkumar RArunkumar R
Hope the below code will helpful to you...!


global class CreateRecordFromJson
{
    public List<JSONParse> jsonList{get;set;}
	public List<YourObjectName__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"}]';
		
        jsonList = (List<JSONParse>) System.JSON.deserialize(jsonString, List<JSONParse>.class);
		
		for(JSONParse jp: jsonList)
		{

		YourObjectName__c objInstance = new YourObjectName__c();
		objInstance.Shop_ID__c = jp.Id;
		//Map other field similar to above.
		objList.add(objInstance);

		}
		
		insert objList;

		}
   
    global class JSONParse
    {
		public String 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;
       
   
    }
   
}

Thanks
sharathchandra thukkanisharathchandra thukkani
go through JSONParser class in below link
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_class_System_JsonParser.htm#apex_System_JsonParser_readValueAs
Sam1980Sam1980
I have done some changes in above code still I got error.

User-added image

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;
      
  
    }
  
}
Alexander TsitsuraAlexander Tsitsura

Hi Sam, u have error becouse you using List<SObject> instead of List<JSONParse>.

 

global class ShopRecordsCtrlNew
{
    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<JSONParse> jsonList = (List<JSONParse>)System.JSON.deserialize(jsonString, List<JSONParse>.class);
        List<Contact> objList = new List<Contact>();
          
        for(JSONParse jp: (List<JSONParse>)jsonList)
        {

            Contact objInstance = new Contact();
            objInstance.FirstName = '' + 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;
      
  
    }
  
}