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

DML insert/update succeeds but records disappear

I have written a visual force page and the controller to support a new custom object.  The custom object is deployed and I can create rows by using the built-in tab.


When I access the visual force page, I receive no errors and the page displays correctly.  When I look at the debug log, I see successful dml insert/update statements, and can retrieve the object via SOQL.  I have put a debug on the retrieved record and it has an ID and all other fields are set to their initial values.  As far as I can see from the debug log, everything looks successful.


15:25:34.58 (704508000)|USER_DEBUG|[11,4]|DEBUG|ReportStats__c:{Month__c=8, WonCount__c=0, RecordTypeId=012Q00000000FW2IAM, TotalInstallWon__c=0.00, AvgInstall__c=0.00, Count__c=0, Year__c=2010, Closed__c=0, OppCount__c=0, TotalInstall__c=0.00, TotalRMRWon__c=0.00, AvgInstallWon__c=0.00, AvgRMRWon__c=0.00, Id=a12Q00000004moUIAQ, TotalRMR__c=0.00, AvgRMR__c=0.00}


The problem is when I return to the tab for the object, the new records don't exist. 


If I use the ID from the debug log HTTPS:// I get "Data Not Available"



System.debug([select id from ReportStats__c ALL ROWS ]); 

 Running the above command from the system log, only returns the objects that I manually created.


The standard name field on the object is auto-incrementing.


If I manually create another record, the name will have increased by the number of records that should have been created by the page.


I have taken every troubleshooting step I can think of, so any ideas on how I could identify the problem would be greatly appreciated.





From you description it looks that there is no problem in the code, but it will be better if you can share the code snippet.


Also, you can check if there is any trigger on the object that is deleting the records as soon as you create them.





I am working from two classes.  I wanted to pull out the main function into it's own class so we can schedule it down the road.


Main class:

transient reportstats__c RiskRev;
RiskRev = ReportStatsProcessor.init('01280000000UAaXAAW','Task',null,thisPeriod);


Isolated Class: (Edited for readability.  Sections marked with < and >)

global class ReportStatsProcessor {
	public static ReportStats__c init(string in_ID, String rptType, string leadSrc, Period coveredPeriod) 
		ReportStats__c rs;
		List<ReportStats__c> t_rs = [Select <desired Fields> From ReportStats__c <Limiting factors>];
		if (t_rs.isEmpty()){
			rs = new ReportStats__c(<initialize fields>);
			insert rs;
			rs = [Select <desired Fields> From ReportStats__c <Limiting factors>];
		} else {
			rs = t_rs[0];
  		update rs;
		return rs;

Both of the second Debug is where I am getting the record ID in my first post.


As far as Triggers go, I just added an "after delete" to try to see if it fires, but it doesn't appear to.   There are no other triggers.


I created both a "before insert' and a"before delete' trigger on the object.  The insert firing but the delete isn't.


We were able to work out a solution, but I don't understand why it was required.


Our Original Controller code:

public ReportStats__c RiskReviewStats { 
    get {
        if (RiskRev == null){
             RiskRev = new ReportStatsProcessor().init('01234567890123456','Task','',thisPeriod);
        return RiskRev; 

 Working Controller code:

public void init()
	RiskRev = new ReportStatsProcessor().init('01234567890123456','Task','',thisPeriod);
public ReportStats__c RiskReviewStats { get { return RiskRev; } }


We then needed to add Action="{!init} into the apex:page declaration.


Can anyone explain why it is needed like this?




Big EarsBig Ears

I know it has been a few years, but I wonder if you ever worked out what was happening here? I have a similar situation whereby I'm inserting some records (as part of another variable's getter) and those records are "disappearing". The debug log shows them being successfully created and returns ids for those created records. The VF page even has access to those ids (they're used in URLs on the page).

However, the records don't exist. They're not deleted, as there is no delete action in the debug log and they aren't in the recycling bin. It's a bit of a mystery.

Ahmed MohammedAhmed Mohammed
Hi Andy,

I was wondering, if you found any solution for the issue you are facing. Similar issue I encountered this morning and unable to find a way to solve it.

Big EarsBig Ears
This may be part of the issue -

DML shouldn't be included in constructors. This may be what was occuring here. It's an odd behaviour, though, that the code completes without any errors, so there is no way to know what may be going wrong.

Best practice - Keep DML out of constructors and use another method to handle it.