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
MSVRadMSVRad 

How to require another object in order to save account

I have a picklist field called "Operational Status" on my Account Object.  When "Operational" is picked from the picklist I want to require that an associated custom object called "Entity Account" is in place before the user can save.

 

The Entity Account object has a master-detail relationship with the Account object. 

 

So if I have account ABC and I want to change its Operational Status to "Operational" I first need to create an Entity Account where account ABC is its master. 

 

I am attempting to write a trigger to make an error pop-up if an account entity associated to the account is not created first. If an entity account is already exising then all is good.

 

I just cant figure out how to check if an Entity Account object is already created for the account or not.  Is there a way to relate two objects like this and how would I go about doing it?

 

Thanks in advance!

Best Answer chosen by Admin (Salesforce Developers) 
WhyserWhyser

Your trigger should be

trigger TriggerName on Account( after insert, after update ) { for ( Account acct : Trigger.new ) { if ( acct.Operational_Status__c == 'Operational' ) { // Check to see if Entity Account Exists try { Entity_Account__c eAcct = [select id from Entity_Account__c where Account__c = :acct.Id]; } catch( Exception e ) { acct.addError( 'Cannot save changes: No Account Entity object associated to the Account' ); } // If you get here, then just do nothing and allow it to save. } } }

 

Message Edited by Whyser on 02-23-2009 12:51 PM

All Answers

WhyserWhyser

Your trigger should be

trigger TriggerName on Account( after insert, after update ) { for ( Account acct : Trigger.new ) { if ( acct.Operational_Status__c == 'Operational' ) { // Check to see if Entity Account Exists try { Entity_Account__c eAcct = [select id from Entity_Account__c where Account__c = :acct.Id]; } catch( Exception e ) { acct.addError( 'Cannot save changes: No Account Entity object associated to the Account' ); } // If you get here, then just do nothing and allow it to save. } } }

 

Message Edited by Whyser on 02-23-2009 12:51 PM
This was selected as the best answer
MSVRadMSVRad
Thanks so much for your help,  it got me to where I needed to move forward with adding more to this trigger.
MSVRadMSVRad
How would my test class look for this trigger?  I encountering more of a problem with existing test classes for account triggers. I am not sure how to make an Account have an Entity Account. typically I create a test class and make dummy Accounts and then run my trigger on those dummy accounts to test and see if it works. Well now the dummy accounts need to include a related entity account. The entity Account and Account object have a master detail relationship as I mentioned previously.
WhyserWhyser

static testMethod void test() { // Instantiate an Account Account a = new Account( Name = 'testAccountName' ); // Instantiate an Entity, do not associate with Account yet Entity_Account__c ea = new Entity_Account__c( fieldName = value, fieldName = value, etc ); // Test to see if we update the Account with Operational_Status__c = 'Operational', make sure it fails try { a.Operational_Status__c = 'Operational'; update a; // If the update PASSES, then we have failed, throw an exception throw new MyException( 'Shouldn't be able to update the Account' ); } catch( Exception e ) { // We are supposed to fail, so we do nothing an continue } // Associate the Entity_Account__c to the Account ea.Account__c = a.Id; update ea; // Now try to update the Account with Operation_Status__c = 'Operational', this time it should pass a.Operational_Status__c = 'Operational'; update a; // Retrieve the account information again to confirm it was updated a = [select Id, Operational_Status__c from Account where Id = :a.Id]; system.assert( a.Operational_Status__c == 'Operational', 'Account was not able to update'); }

 

To throw your own Exceptions, you'll need to create this class in your test class

 

public class MyException extends Exception {}

 

Message Edited by Whyser on 03-02-2009 01:10 PM
MSVRadMSVRad

Thank you very much for your assistance.