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
IshIsh 

How to insert data to a custom object using apex controller

Hi,

 

I have created a UserDetail custom object. I tried to insert data into the object using binding.create() and passed the sObject. I got an error message stating "Only concrete SObject lists can be created ". How can acheive this task.

 

Thanks in Advance

Best Answer chosen by Admin (Salesforce Developers) 
wesnoltewesnolte

Hey

 

Yeah I think you were looking at the wrong API initially, at least you got the insert sorted out. To perform an update you'd need to fetch the data

 

UserDetail__c u = [SELECT first_name__c FROM UserDetail__c WHERE last_name__c='Smith'];

 

u.first_name__c = 'Jim';

 

update u;

 

You may also want to look into 'upsert', which is a command that intelligently decides whether to insert or update a record e.g.

 

 

UserDetail__c u;

 

 

try{

 

u = [SELECT first_name__c FROM UserDetail__c WHERE last_name__c='Smith']; //try to fetch the record

 

} catch (System.QueryException e){

 

 System.debug(e);

 

u = new UserDetail__c(last_name__c='Smith'); // no user with surname smith exists so create one

}

 

u.first_name__c = 'Jim';

 

upsert u; // if it's a new record insert it. if it's a change to a record perform update

 

Cheers,

Wes

 

All Answers

wesnoltewesnolte

Hey

 

Are you using Java? Can you post a bit more code please?

 

Wes 

IshIsh
I am not using java. I am using apex to do my business logic. I need to how to insert and update the data to the custom object which i have created.
IshIsh

Hi

 

I have used the following code to insert the data and it does the insert properly. Now i would like to know how to update the data.

 

UserDetail__c insertUser = new UserDetail__c();
insertUser.First_Name__c =firstName;
insertUser.Last_Name__c=lastName;
insertUser.Name =eid; 
insert insertUser;

 

wesnoltewesnolte

Hey

 

Yeah I think you were looking at the wrong API initially, at least you got the insert sorted out. To perform an update you'd need to fetch the data

 

UserDetail__c u = [SELECT first_name__c FROM UserDetail__c WHERE last_name__c='Smith'];

 

u.first_name__c = 'Jim';

 

update u;

 

You may also want to look into 'upsert', which is a command that intelligently decides whether to insert or update a record e.g.

 

 

UserDetail__c u;

 

 

try{

 

u = [SELECT first_name__c FROM UserDetail__c WHERE last_name__c='Smith']; //try to fetch the record

 

} catch (System.QueryException e){

 

 System.debug(e);

 

u = new UserDetail__c(last_name__c='Smith'); // no user with surname smith exists so create one

}

 

u.first_name__c = 'Jim';

 

upsert u; // if it's a new record insert it. if it's a change to a record perform update

 

Cheers,

Wes

 

This was selected as the best answer
IshIsh
Thanks. Now i am able to update the data.
Abdullah Sikandar 11Abdullah Sikandar 11
Thanks guys, can anyone tell me what will be the code to write for enterting the record in Cases.

This is the Scenerio, I want the new case will be build and all the records which are on fields on VF page, will go into comments?
IS it possible,

Actually, its a page, where customer will enter the information and once the customer hit Submit Button, then a new case will be build and  it will insert all the record into a new case Comments field. 

Help me guys.