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
phantom1982phantom1982 

Updating a custom object

Can someone please explain the standard way to update a custom object from an APEX class?

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Anup JadhavAnup Jadhav

Hiya,

 

One of the ways to update a custom object is to get the object record from the database, and calliing an update on the record.

 

 

My_Custom_Object__c customObject = [Select name, country from My_Custom_Object__c where name='John' limit 1];

customObject.name = 'Jim';

//This will update the custom object
update customObject;

 

If you are new to the Force.com platform, you might want to read the Force.com workbook which contains very good examples for beginners.

 

 

http://www.salesforce.com/us/developer/docs/workbook/index.htm

 

Hope this helps!

 

A J

All Answers

bob_buzzardbob_buzzard

You'd simply use the update method, e.g.

 

 

MyCustom__c obj=new MyCustom__c(Name='Test Name');
insert obj;

obj.Name='New Name';
update obj;

 Obviously this is a rather simplistic example, but if you have a reference to the object that's all you need to do.

 

Anup JadhavAnup Jadhav

Hiya,

 

One of the ways to update a custom object is to get the object record from the database, and calliing an update on the record.

 

 

My_Custom_Object__c customObject = [Select name, country from My_Custom_Object__c where name='John' limit 1];

customObject.name = 'Jim';

//This will update the custom object
update customObject;

 

If you are new to the Force.com platform, you might want to read the Force.com workbook which contains very good examples for beginners.

 

 

http://www.salesforce.com/us/developer/docs/workbook/index.htm

 

Hope this helps!

 

A J

This was selected as the best answer
phantom1982phantom1982

Thanks for the replies.

 

I found out what was wrong, had been trying to update from within the constructor which is not allowed and getting this error:

 

System.LimitException: DML currently not allowed
Class.LControllerFalcoceta.getpoint: line 142, column 29 External entry point

 

Thanks