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
Sam KamenskySam Kamensky 

Why have database rows not been inserted when the debug log says they have?

I have code that creates CaseShare records for the case based on some criteria from another class.

The code that creates the CaseShare is called both on update and creation of the case. It works smoothly when the case is updated, but not when it's created. When attempting to debug my issue I got confused because the debug logs apparently showed that the records were being inserted. In fact, the debug logs show the same exact things for when the case is inserted (the DML doesn't work then) as when the case is updated (the DML does work then).

Here is the debug log for both when the case is created (DML doesn't work then) and when the case is updated (DML does work) line 55 is where my insert is happening:
USER_DEBUG|[54]|DEBUG|Value of csShareList: (CaseShare:{CaseAccessLevel=Edit, CaseId=500180000050EsoAAE, UserOrGroupId=005E0000004JDY4IAO}, CaseShare:{CaseAccessLevel=Edit, CaseId=500180000050EsoAAE, UserOrGroupId=005E0000000dNNWIA2}, CaseShare:{CaseAccessLevel=Edit, CaseId=500180000050EsoAAE, UserOrGroupId=005E0000000dBqjIAE}, CaseShare:{CaseAccessLevel=Edit, CaseId=500180000050EsoAAE, UserOrGroupId=005E0000005aSlXIAU})
00:53:58.516 (755158871)|SYSTEM_METHOD_EXIT|[54]|System.debug(ANY)
00:53:58.516 (755161876)|STATEMENT_EXECUTE|[55]
00:53:58.516 (755228490)|DML_BEGIN|[55]|Op:Insert|Type:CaseShare|Rows:4
00:53:58.516 (755248812)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:20
00:53:58.516 (825013969)|DML_END|[55]

Additionally, here's the code that does the insert:
if (!csShareList.isEmpty()){
                System.debug('Value of csShareList: ' + csShareList);
                insert (csShareList);
                
            }

 
Best Answer chosen by Sam Kamensky
Sam KamenskySam Kamensky
When a queue owns a case record in SF and the sharing model for the case is private
Only queue members and their direct reports can view or take ownership of [those] cases.
http://resources.docs.salesforce.com/198/13/en-us/sfdc/pdf/salesforce_case_implementation_guide.pdf#page=21

Here are two exceptions I learned about from this experience.
Other users can view the case if they have 'view all' on the object level or are in role hierarchy above someone with 'view all.'
The calculation for this specific sharing event happens when you change or initially set the owner to a queue but will not be calculated in for updates to the record that do not change the ownership from a user to a queue, or from one queue to another.

See http://salesforce.stackexchange.com/questions/117529/why-have-database-rows-not-been-inserted-when-the-debug-log-says-they-have/117556#117556 for more detail.

All Answers

doravmondoravmon
try using this:
try
{
  insert(csShareList);
}
catch (exception ex)
{ system.debug(ex.message());
}

You may catch the exception this way.
Sam KamenskySam Kamensky
doravmon, thanks for the suggestions! Apparently, it's not causing an exception, it's simply not creating the records.

Updated Debug file:
12:44:45.507 (734016198)|USER_DEBUG|[55]|DEBUG|Value of csShareList: (CaseShare:{CaseAccessLevel=Edit, CaseId=500180000050HLpAAM, UserOrGroupId=005E0000004JDY4IAO}, CaseShare:{CaseAccessLevel=Edit, CaseId=500180000050HLpAAM, UserOrGroupId=005E0000000dNNWIA2}, CaseShare:{CaseAccessLevel=Edit, CaseId=500180000050HLpAAM, UserOrGroupId=005E0000000dBqjIAE}, CaseShare:{CaseAccessLevel=Edit, CaseId=500180000050HLpAAM, UserOrGroupId=005E0000005aSlXIAU})
12:44:45.507 (734027280)|SYSTEM_METHOD_EXIT|[55]|System.debug(ANY)
12:44:45.507 (734032362)|STATEMENT_EXECUTE|[56]
12:44:45.507 (734130540)|DML_BEGIN|[56]|Op:Insert|Type:CaseShare|Rows:4
12:44:45.507 (734156729)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:20
12:44:45.507 (800983201)|DML_END|[56]

Updated code:
 
if (!csShareList.isEmpty()){
                try{
					System.debug('Value of csShareList: ' + csShareList);
					insert (csShareList);
				}
				catch(exception e){
				
					system.debug(e.message());
				
				}
                
            }

 
Sam KamenskySam Kamensky
I actually used e.getmessage() since message isn't a method
doravmondoravmon
hmm.... try 'insert csShareList', without the ( )
Sam KamenskySam Kamensky
Unfortunately it's still the same.

Updated Debug File:
14:14:54.417 (656673502)|USER_DEBUG|[55]|DEBUG|Value of csShareList: (CaseShare:{CaseAccessLevel=Edit, CaseId=500180000050HSWAA2, UserOrGroupId=005E0000004JDY4IAO}, CaseShare:{CaseAccessLevel=Edit, CaseId=500180000050HSWAA2, UserOrGroupId=005E0000000dNNWIA2}, CaseShare:{CaseAccessLevel=Edit, CaseId=500180000050HSWAA2, UserOrGroupId=005E0000000dBqjIAE}, CaseShare:{CaseAccessLevel=Edit, CaseId=500180000050HSWAA2, UserOrGroupId=005E0000005aSlXIAU})
14:14:54.417 (656680026)|SYSTEM_METHOD_EXIT|[55]|System.debug(ANY)
14:14:54.417 (656682834)|STATEMENT_EXECUTE|[56]
14:14:54.417 (656754459)|DML_BEGIN|[56]|Op:Insert|Type:CaseShare|Rows:4
14:14:54.417 (656778446)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:20
14:14:54.417 (717863949)|DML_END|[56]
 
if (!csShareList.isEmpty()){
                try{
                    System.debug('Value of csShareList: ' + csShareList);
                    insert csShareList;
                }
                catch(exception e){
                
                    system.debug('Value of e.getMessage: ' + e.getMessage());
                
                }
                
            }

 
doravmondoravmon
how do u know it's not insert? did you put the caseId in ur url to check if that exist or not?
Sam KamenskySam Kamensky
I checked in three ways.
1. By querying the CaseShares related to the case I just inserted. 
2. I pressed the "Sharing" button on the case record to view the case shares
3. I tried to view the record as one of the users I attempted to share it with

On create, the query shows no CaseShare records with the users the code is sharing with, the sharing button also shows no CaseShare records with the users the code is sharing with, and the user gets insufficient access when trying to access the record.

However, upon edit, the query shows the CaseShare records with the users the code is sharing with, the sharing button also shows the CaseShare records with the users the code is sharing with, and the user gets is able to view the record when trying to access the it.
Sam KamenskySam Kamensky
When a queue owns a case record in SF and the sharing model for the case is private
Only queue members and their direct reports can view or take ownership of [those] cases.
http://resources.docs.salesforce.com/198/13/en-us/sfdc/pdf/salesforce_case_implementation_guide.pdf#page=21

Here are two exceptions I learned about from this experience.
Other users can view the case if they have 'view all' on the object level or are in role hierarchy above someone with 'view all.'
The calculation for this specific sharing event happens when you change or initially set the owner to a queue but will not be calculated in for updates to the record that do not change the ownership from a user to a queue, or from one queue to another.

See http://salesforce.stackexchange.com/questions/117529/why-have-database-rows-not-been-inserted-when-the-debug-log-says-they-have/117556#117556 for more detail.
This was selected as the best answer