You need to sign in to do that
Don't have an account?
jackpushpan
Save using SObject
Hello,
I am new to Apex coding.
I have a piece of code which needs to update records of Standard and custom objects. When I run the below code it runs without throwing any errors but does not save the data.
Code:
ApexPages.StandardSetController setCon = new ApexPages.StandardSetController(Database.getQueryLocator('select id, name from Account where id=\'' + RecordID + '\''));
SObject acc = setCon.getRecord();
acc.put('name','testName');
setCon.save();
Help much appreciated.
Thanks,
jack
I found the issue.
It appears DML cannot be accessed via getters or setters. It should be accessed through action methods.
Changed my code as follows:
Code in Visual force page:
<apex:page Controller="clsTestUpdates" action="{!SaveDbOperation}">
Code in controller
public PageReference SaveDbOperation() {
SaveDetails(); // function which performs DML operations
return null;
}
refer : http://forums.sforce.com/t5/Visualforce-Development/Receiving-quot-DML-currently-not-allowed-quot-exception/m-p/156522
All Answers
Make the changes as below,
SObject acc = setCon.getRecord();
acc.put('name','testName');
upsert acc;
Hi Imran,
Thanks for your reply. I amended the code as per your advice.
I now recieve another error - DML currently not allowed. I have pasted the error below.
--------------------------------------
System.LimitException: DML currently not allowed
Class.clsTestUpdates.SaveRowData: line 655, column 4 Class.clsTestUpdates.getSaveDetails: line 634, column 11 External entry point
--------------------------------------
Do I need to enable any property to get this working?
Thanks
Jack
I am not sure what could be the reason for the error.
If possible, can you post the code?
The code I am working on is below:
//Controller Class
public with sharing class clsTestUpdates
{
public string getSaveDetails()
{
try
{
CRUDObject obj = new CRUDObject()
return obj.SaveRowData('Account', '001Q000000Dg5xrIAB','saveparams');//TODO : Remove hardcoded parms
}
catch(Exception ex)
{
return 'Error occured ' + ex;
}
}
}
//Helper Class
public with sharing class CRUDObject
{
public string SaveRowData(string ObjectName, string RecordID, string SaveParameter)
{
try
{
string sqlQuery = 'select id, name from ' + ObjectName + ' where id=\'' + RecordID + '\'';
ApexPages.StandardSetController setCon = new ApexPages.StandardSetController(Database.getQueryLocator(sqlQuery));
SObject acc = setCon.getRecord();
//below code will be replaced by SaveParameter
acc.put('name','testName');
upsert acc;
return 'success';
}
catch (Exception ex)
{
return 'error:' + ex;
}
}
}
Thanks for the help.
Can you also post the code for getSaveDetails()?
I found the issue.
It appears DML cannot be accessed via getters or setters. It should be accessed through action methods.
Changed my code as follows:
Code in Visual force page:
<apex:page Controller="clsTestUpdates" action="{!SaveDbOperation}">
Code in controller
public PageReference SaveDbOperation() {
SaveDetails(); // function which performs DML operations
return null;
}
refer : http://forums.sforce.com/t5/Visualforce-Development/Receiving-quot-DML-currently-not-allowed-quot-exception/m-p/156522
Thanks for sharing the info.
It will be useful info for other members of the forum.