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
kcostekcoste 

Singleton Custom Object?

I have created a custom object, named VAR (Value at Risk). It is, and must be, a singleton object. There must be one and only one VAR record in the system. It makes absolutely no sense to have more than one VAR record. Having no VAR record is equally ridiculous.

 

Many other objects must have access this systemwide singleton value. Of course, it needs to be easily updated by authorized users.

 

The VAR record's value is tracked via "Set History Tracking." Each time VAR changes, Salesforce's Track History shows when it was changed, who changed it, and what the change was, leaving a perfect audit trail on the VAR record -- exactly per requirements.

 

I was able to make this work very easily in the sandbox, but I can not deploy it in Production via Eclipse because Salesforce insists on running both the insertVAR and deleteVAR triggers before allowing the deployment to succeed. And, given the triggers, of course it fails, and therefore code coverage is zero for those triggers, and I'm out of luck....

 

Here are the two trivial triggers:

 

trigger insertVAR on CES_VAR__c (before insert) {

Trigger.new[0].addError('\r\nThere can only be one VaR record! To change VaR, click the Edit button.');

}

 

trigger deleteVAR on CES_VAR__c (before delete) {

Trigger.old[0].addError('Can not delete the VAR record! To change VaR, click the Edit button.');

}

 

How can I get this to deploy? Is there some way to fool Salesforce into accepting this? Is there a singleton pattern in Salesforce that I'm not aware of?

 

Has anyone been able to get this (rather standard design pattern) to work on Salesforce?

 

Thank you very much for your time.

 

Best Answer chosen by Admin (Salesforce Developers) 
InsertWittyNameInsertWittyName

This unit test class gives 100% coverage for the insert - I'll leave it up to you to modify it for the delete.

 

 

public class testInsertVar { static testMethod void test() { // Create a new record (with fields as appropriate) CES_VAR__c var = new CES_VAR__c(Name='Test Record'); // Try the insert try { insert var; } // Catch the error (should always happen) catch (DmlException e) { return; } // Assert that we have an error message on the page System.assert(ApexPages.hasMessages(ApexPages.Severity.ERROR)); } }

 

 

 

 

 

Message Edited by InsertWittyName on 01-07-2010 01:29 AM

All Answers

InsertWittyNameInsertWittyName

This unit test class gives 100% coverage for the insert - I'll leave it up to you to modify it for the delete.

 

 

public class testInsertVar { static testMethod void test() { // Create a new record (with fields as appropriate) CES_VAR__c var = new CES_VAR__c(Name='Test Record'); // Try the insert try { insert var; } // Catch the error (should always happen) catch (DmlException e) { return; } // Assert that we have an error message on the page System.assert(ApexPages.hasMessages(ApexPages.Severity.ERROR)); } }

 

 

 

 

 

Message Edited by InsertWittyName on 01-07-2010 01:29 AM
This was selected as the best answer
kcostekcoste

Thank you very much! That worked perfectly. The delete works, too -- 100% coverage.

 

Appreciate it!

 

static testMethod void testDeleteVAR() { CES_VAR__c var = [select id from CES_VAR__c LIMIT 1]; // Try the delete the existing VaR record try { delete var; } // Catch the error (should always happen) catch (DmlException eDelete) { return; } // Assert that we have an error message on the page System.assert(ApexPages.hasMessages(ApexPages.Severity.ERROR)); }