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
jackpushpanjackpushpan 

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

 

Best Answer chosen by Admin (Salesforce Developers) 
jackpushpanjackpushpan

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

Imran MohammedImran Mohammed

Make the changes as below,

SObject acc = setCon.getRecord();

acc.put('name','testName');

upsert acc;

jackpushpanjackpushpan

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. 

--------------------------------------

 

Visualforce Error

 

 

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

Imran MohammedImran Mohammed

I am not sure what could be the reason for the error.

If possible, can you post the code?

jackpushpanjackpushpan

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.

Imran MohammedImran Mohammed

Can you also post the code for getSaveDetails()?

jackpushpanjackpushpan

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

 

This was selected as the best answer
Imran MohammedImran Mohammed

Thanks for sharing the info.

It will be useful info for other members of the forum.