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
Jayamaruthyraman JaganathJayamaruthyraman Jaganath 

StandardController's Save method

I have a custom object called DummyObject and it has just one column called DESCR (text, 50 characters length). When I execute the following anonymous block, the ID field is null. Calling the Insert method works, however. Is it safe to assume that the Save method does not update the ID field unlike the Insert method?

dummyobject__c doobj = new dummyobject__c();
doobj.Descr__c = 'test';
ApexPages.StandardController controller = new ApexPages.StandardController(doobj);
controller.save();
controller = new ApexPages.StandardController(doobj);
system.debug(doobj.id);
SandhyaSandhya (Salesforce Developers) 
Hi,

I see that when you execute this code in anonymous block record is not created in the model that is the reason id is null.

I don't think You can use save method in Anonymous block we generally use save() method in the controller as this is a controller method.

Please refer below link for more information.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_ApexPages_StandardController_methods.htm

Hope this helps you!

If this helps you please mark it as solved.

Thanks and Regards
Sandhya

 
Jayamaruthyraman JaganathJayamaruthyraman Jaganath
Thanks, Sandhya, for the response. What I am confused about is the Save call updates the ID field when called from inside a controller. But it does not do that when called from an anonymous block.

However, the Insert method behaves completely, irrespective of where it is called from.

Jay