You need to sign in to do that
Don't have an account?

Why isn't my insert DML statement working?
Hi all,
I'm just starting Apex coding to try and get some custom functionality. I created a custom object called Settings__c in which I want to hold user configurable settings. There should only ever be one record there. I'm making a custom VF page to allow users to modify the settings.
I want my custom VF page to display the current settings. If a settings record does not exist, I want the code to insert a new record and populate it with the values from the form. If an existing record exists I want the form to update the settings.
The problem is that the command button doesn't seem to do anything. I deleted all the records from my custom object so my understanding of the code I have written is that it should execute the insert statement resulting in a new record. But when I check, no record is created. I don't know why, can anyone tell me what the coding error is?
Controller code:
public class Settings { //Settings variables public String URL {get; set;} public ID SettingID{get; set;} //Constructor public Settings() { //Return list of settings list<Settings__c> settings = [select ID,URL__c from Settings__c limit 1]; //Check for existing settings record if (!settings.isempty()) { //If one exists, loop through and assign settings variables for (Settings__c a : settings) { SettingID = a.ID; URL = a.URL__c; } } } //Save settings public void save() { //Check for existing settings record indicated by presence of SettingID if (SettingID == null) { //Create a new record Settings__c newSettings = new Settings__c(URL__c = URL); try { Insert newSettings; } catch (DMLException e) { newSettings.addError('There was a problem adding the settings. The error message returned is: ' + e.getMessage()); } } else { //Update the existing settings record Settings__c updateSettings = new Settings__c(URL__c = URL); try { update updateSettings; } catch (DMLException e) { updateSettings.addError('There was a problem updating the settings. The error message returned is: ' + e.getMessage()); } } } }
VF page code
<apex:page controller="Settings"> <apex:form > <apex:outputlabel value="URL"/> <apex:inputtext value="{!URL}"/> <apex:outputlabel value="Default Status"/> <apex:inputtext value="{!DefaultStatus}"/> <apex:inputText value="{!SettingID}"/> <apex:commandButton action="{!save}" value="Save Settings"/> </apex:form> </apex:page>
try this ............you have to change the id property to string property..
All Answers
you have to compare like this
if (SettingID == null || SettingID == '') in your save method code.....then it works..
regards,
Kiran
Hi Kiran,
Thanks for your suggestion. I already tried debugging by commenting out the if statement so it can't be that. Basically, the code beocmes:
However, when I check, a record is still not created in Settings__c
try this ............you have to change the id property to string property..
After checking your code, You didnt mention your Object setings.. like did you make name as Auto number or not.. If not then you have to set the value in Name filed of Settings Object.
Please pass all Required value in object..
Thanks
Anand Singh
cantchanand, Yeah I just double checked that I made the Name field in Settings__c autonumber. I also tried modifying my insert statement to:
However it won't even compile giving error: Error: Settings Compile Error: Field is not writeable: Settings__c.Name at line 30 column 72 so it proves that the object is set up correctly.
Kiran, changing the type for ID won't make a difference because I'm not using that variable for the insert statement as Name is autonumber.
I think I'm just going to go right back to basics and start from scratch with a really simple VF page and work my way forward to work out what the problem is.
Thanks Kiran.
You were right, changing the type to string worked. After putting in a <apex:pagemessages> tag at the top of my VF page I could see that the code was not getting past the constructor and hence not even execuring the DML because of a type conversion error.
Custom objects ID fields are not of type ID 15 char length identifier like the standard objects so using a variable of type ID to hold the Name field from my custom object threw an error.