You need to sign in to do that
Don't have an account?
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!
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. } } }
All Answers
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. } } }
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 {}
Thank you very much for your assistance.