1

I encountered a weird issue which i wanted to get feedback from you guys. Currently we have 2 sandboxes where we are doing development currently. We are experiencing an odd behavior where none of the database insert or upserts are working. When we do a database.insert or upsert, there is no exception . We also get an ID but when do a soql query or look up on the url by id, we are not seeing any data at all.So the try catch thinks there is no exception but there will be no record in the custom object at all.

Our current application is a multistep wizard which runs across 60 pages step by step. On each page there is a next and previous button. On next button click, our application queries a custom object database to figure out the next page and previous page. It also features to jump to a page or loop through one page multiple times. So here is our question.

  1. In our code, we currently rely on 2 custom objects to be inserted on the starting of the page. The reason for this problem was that some where our code was throwing a null pointer but Salesforce rolled back the entire transaction. So what are the situations or conditions apex is looking to do a database rollback ? What triggers it? In our case, we got the id and no exception but the record never existed in the database at all.

  2. The debug logs currently do not have any trace of a database roll back and so we need to know where can we see that the database transaction has been rolled back?

  3. If our code has a run time exception like a null pointer, how can we force apex to still commit the database transaction and insert the record? What object and method with parameter should we use for it?

  • 2
    Do you have apex:messages on the wizard pages? Do you have some Database.rollback or Database.savePoint? – Sergej Utko Apr 6 '13 at 14:36
3

In our code, we currently rely on 2 custom objects to be inserted on the starting of the page. The reason for this problem was that some where our code was throwing a null pointer but Salesforce rolled back the entire transaction.

Could you post the code you're using to do this? Unless you're also saving the page state when it loads, those objects won't be inserted into the database. So are you using some kind of custom controller or are you using javascript to do an autosave?

The debug logs currently do not have any trace of a database roll back and so we need to know where can we see that the database transaction has been rolled back?

Does your code include database rollback savepoints?

If our code has a run time exception like a null pointer, how can we force apex to still commit the database transaction and insert the record? What object and method with parameter should we use for it?

That's something I can directly answer. You need to use a try catch block. Here's some example code that would do the trick to catch a null pointer exception. In this use case it was to prevent a null pointer exception when assigning a queried list variable that might have been null to a static variable:

    try{

       If (opp.Event_Notes__c == null) notes = 'None';

       }catch(NullPointerException e){

         notes = 'None';

      } 

To force a database insert even when there's a DML exception, use code that looks like this:

Map<Id,string> IdtoEvntNotes = new map<Id,string>();
list<Event> ToInsert = new list<Event>();


// later in the code


if(ToInsert.IsEmpty() == false){

list<Database.SaveResult> InsertResults = Database.Insert(ToInsert,false);
// the false option above allows results to be inserted with errors

    // Iterate through the Save Results
    // and create list of error messages
    // add error messages to IdtoEvntNotes map

    for(Integer i=0; i<InsertResults.size(); i++){

      If (!InsertResults[i].isSuccess()) IdtoEvntNotes.put( InsertResults[i].Id , 'Error reported was: ' + InsertResults[i].getErrors()[0].getMessage() + ' ' + IdtoEvntNotes.get(InsertResults[i].Id)); 

    } 
  • Thanks for your code and comments. We are using a custom controller and i dont have try catch exceptions now in my code to handle runtime exceptions. So i think your tips on try catch and handling null pointers would certainly help me. – buyan kumar Apr 8 '13 at 2:28
  • @buyankumar. Glad you found the code useful. Try catch blocks are very useful for many purposes. If you think this provides the solutions to your issue, please mark your question answered for the benefit of others. :) – crmprogdev Apr 8 '13 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.