+ Start a Discussion
Koen (BvD)Koen (BvD) 

unable to insert or update custom object

I am trying to save user specific information in a custom object. However I am unable to create or update a record in my table. My table just has two columns, one is a reference to the user, one is a reference to a second custom object (to store the user's selection). I can create and edit entries using the default interface of salesforce and selecting entries from the controller works ok. I am unable to catch an exception (either with general exception or DMException). There is no information in the system log, even at the finest level.


I have a similar problem when updating. There I do not create my object using new but using a select statement.


Here is the essential part of my code




UserSettings__c settings = new UserSettings__c ();

settings.Product__c = (Id) product;

settings.User__c = (Id) Userinfo.getUserId ();

Database.SaveResult saveResult = Database.insert (settings);


System.debug(System.LoggingLevel.DEBUG, 'Inserted settings);


catch (Exception e)


System.debug(System.LoggingLevel.DEBUG, e);

System.debug(System.LoggingLevel.DEBUG, 'Failed to insert settings');



In the system log everything comes out on one line (something to do with the error)


line 57, column 21: SOBJECT:BvD_UserSettings__c lsettings.Product__c <= Cast 20090922084653.442:Class.BvDProductSelectionController.setCurrentProduct:

line 58, column 21: SOBJECT:UserSettings__c settings.User__c <= Id userId 20090922084653.442:Class.BvDProductSelectionController.setCurrentProduct:

line 59, column 21: DeclareVar: Database.SaveResult saveResult 20090922084653.442:Class.BvDProductSelectionController.setCurrentProduct:

line 59, column 55: Database.insert(SOBJECT:UserSettings__c) 20090922084653.442:External entry point: Exiting user sharing mode Cumulative profiling information:


followed by all timings of the sql queries, including the insert query


Database.insert(SOBJECT:UserSettings__c): executed 1 time in 0 ms 3


And ending with the message that there was an error


j_id0:j_id1:j_id2:j_id4: An error occurred when processing your submitted information. ***Ending Page Log for /apex/BvDProductSelection?sfdc.tabName=01r70000000Pv9C









First of all, DMLException will not be thrown from Database.insert in most cases. According to the reference,


From Apex Language Reference - Apex Data Manipulation Language (DML) Operations

Use DML database methods if you want to allow partial success of a bulk DML operation—if a record fails, the remainder of the DML operation can still succeed. Your application can then inspect the rejected records and possibly retry the operation.When using this form, you can write code that never throws DML exception errors. Instead, your code can use the appropriate results array to judge success or failure. Note that DML database methods also include a syntax that supports thrown exceptions, similar to DML statements.


I assume the saveResult.isSuccess was false, but the strange thing is that the log doesn't show the debug log.


So, I suggest to use "insert settings;" and to see the DMLException so that you can fix the DML problem. Once you fix it, you go back to Database.insert for whatever reason you use Database.insert.




Koen (BvD)Koen (BvD)

Actually I had a little difficult finding the difference between Database.Insert and insert, I thought they were identical. I tried the insert previously and it was out of sheer frustration that things didn't work that I tried almost every variation I could think of, ending with a version that used database.Insert.


So just to make sure I went back and changed now to insert, but stil lthe same information in the log (i.e. none whatsoever) . I tried first with a catch on DMLException and then with a general catch for exception but nothing.


"I  assume the saveResult.isSuccess was false"" == > no, the function does not return and it really seems that the processing of the page stops immediately while executing the insert statement.


There is maybe one more thing I should mention, though I copied this from an example in the doc. I execute my code in the setter of a property bound to the value of a apex-selectlist. The method attached to the ok button is empty (it just returns null as PageReference)


Is there any other way to do this? I don't think this is the problem, I placed a debug in the action and this code seems not reached anyway.





Then, I can't say anything without Apex code which is more similar to your actual code and the VF page source code...


and another thing. What was wrong with insert? What was the reason why you thought insert was wrong and Database.insert was ok? The title of this post is "undable to insert or update custom object" so I assumed that that is the problem, but may be not.




You said the insert was in the 'setter' method. I would think that you would want this in the 'OK' button action, otherwise, the insert would happen everytime the select list is seleted, maybe even when the page is first displaying and sending a null value into one of the values.

Change the DML expression to just 'insert' and move the code into the 'OK' button action.



This error:


j_id0:j_id1:j_id2:j_id4: An error occurred when processing your submitted information.


implies a validation failure of some sort when submitting the page.  Have you got a pageMessages standard component on your page to display errors?  It might be that its never getting to your insert code.

Message Edited by bob_buzzard on 09-24-2009 02:52 AM
Koen (BvD)Koen (BvD)

I didn't have one yet, so I added this


Error messages :


but it stays empty.

For the moment I am going to give up this solution. If I have courage I will build up step by step a new custom object to see what exactly makes it go wrong.  For now I'll try to save settings in cookies (though I learned from another post that I won't be able to access cookies from the controller class)


Before you give up, why not post your page source and see if anyone can spot a problem?  Sometimes things are obvious to a fresh pair of eyes.