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 one or more objects to save an Account

I had help with this trigger previously and it worked great until the users changed the way they wanted it used.  Here is a link to my previous question.

 

Previous Apex Code Help

 

 

The new issue is that the users want to be able to have more than one Entity Account before they save. Currently the trigger requires a user to have one entity account active in order to change the operational status of an Account to "Operational." The trigger also has the limitation that they can currently only have one Entity Account active before they can change the operational Status to "Operational" then save, the users want to be able to create as many Entity Accounts as they want related to the Account then change the operational status to "Operational" then save.  Currently this produces the error that I have listed in the trigger in the link above.

 

Here is the actual trigger I wrote which is pretty much the same as the one in the link.

 

trigger AccountReqEntity on Account (after insert, after update) { Set<Id> practiceIds = new Set<Id>(); for(Account acct : trigger.new){ //practice record type if(acct.RecordTypeId == '012700000009R7B'){ practiceIds.add(acct.Id); //a list of practices if(acct.Operational_Status__c == 'Operational') { try { Entity_Account__c eAcct = [SELECT Id, Active__c, Account__c FROM Entity_Account__c WHERE Account__c in :practiceIds]; } catch(exception e) { acct.Operational_Status__c.addError('Must have an Entity Account Activated'); } } } } }

 

Any suggestions would be greatly appreciated.

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
kerwintangkerwintang

Then you don't need the try block.

 

You can just check the size of eAcct.

if(eAcct.size() == 0){

  acct.Operational_Status__c.addError('Must have an Entity Account Activated');

}

 

or if you still want to use the try block, throw an error instead if size = 0.

 

Regards,

Kerwin

All Answers

kerwintangkerwintang

Sorry im a little confused with your explanation.

 

Based on trigger,

Current behavior : user can set status to Operational if there is at least one Entity Account for the current Account.

Expected behavior : ?

 

Thanks and Regards,

Kerwin

MSVRadMSVRad

Current behavior: user can set status to Operational if there is one Entity Account for the Current Account. Currently not allowing for multiple Entity Accounts to be created prior to setting the status to Operational.

 

Expected Behavior: The user should be able to create as many Entity Accounts as they want (for the current account) before they set the status to Operational.

 

Sorry for the confusion.  My guess is that I am off in my query - but I am not sure what to do to make it behave how I want.

 

Thanks!

kerwintangkerwintang

Try to replace the Entity_Account__c eAcct with a List (i.e. Entity_Account__c[] eAccts). The original trigger threw an error when there is more than 1 entity account retrieved since you're assigning it to a single object.

 

Regards,

Kerwin

MSVRadMSVRad

This approach does allow more than one Entity Account to be added but now if I do not add one at all no error occurs.

 

here is the code...

trigger AccountReqEntity on Account (after insert, after update) { Set<Id> practiceIds = new Set<Id>(); for(Account acct : trigger.new){ //practice record type if(acct.RecordTypeId == '012700000009R7B'){ practiceIds.add(acct.Id); //a list of practices if(acct.Operational_Status__c == 'Operational') { try { Entity_Account__c[] eAcct = [SELECT Id, Account__c FROM Entity_Account__c WHERE Account__c in :practiceIds]; } catch(exception e) { acct.Operational_Status__c.addError('Must have an Entity Account Activated'); } } } } }

 

kerwintangkerwintang

Then you don't need the try block.

 

You can just check the size of eAcct.

if(eAcct.size() == 0){

  acct.Operational_Status__c.addError('Must have an Entity Account Activated');

}

 

or if you still want to use the try block, throw an error instead if size = 0.

 

Regards,

Kerwin

This was selected as the best answer
MSVRadMSVRad
Thanks a lot! This seems to have worked - I guess I overlooked something pretty obvious.