7

The standard object Account has a master-detail relationship with a child object called Address__c. In the Address__c object we store the various addresses an account might have.

My question is as following:

Can you make the creation of an address mandatory when an account is created by the user in the UI?

Thank you

10

UI

If you would like to enable creation of both of these records at the same time, you will need to create a Flow or a VisualForce wizard. I have more experience with the latter, but believe the former is more configurable and overall lower effort.

Validation

EDIT As noted by SFDCFox, you will need to have a clause that checks NOT(ISNEW()), because the requirements you list do not allow for Account creation in the first place.

You can accomplish this purely via config! Simply create a rollup that sums the child records and then a validation that makes sure that rollup is > 0. This does not work for lookups but is achievable because the relationship is Master-Detail.

WARNING:

This solution can cause some updating issues if you have Apex logic around the relationship, in which case an Apex Trigger can certainly address this need instead. Please post a code sample if you would like Apex help.

  • 1
    Close one pirate, will get the bounty next time:D!!! – Rao Jan 20 '15 at 17:14
  • 5
    By this logic, you could never create an account... – sfdcfox Jan 20 '15 at 17:21
  • Thank you very much for your answer. I like the idea, but it checks if the account has addresses after the account is created and not at the time the account is created. I think my question is more towards if the user can create an Account and an Address at the same time in the UI, thus making the Address mandatory. – Mihai Neagoe Jan 20 '15 at 17:32
  • That would definitely require a VisualForce wizard or a Flow solution. – Adrian Larson Jan 20 '15 at 17:48
  • 1
    You wouldn't be able to do what's referred to as a Mixed DML on the Account and Child by inserting both at the same time. They'd need to be sequential. A Visual Flow would be one solution as suggested by @AdrianLarson. – crmprogdev Jan 20 '15 at 17:51
8

You can't force record creation of the Address__c record on insert of the Account, because there will never be a child record when the record is created (the parent must exist before the child can be linked to it). You can prevent any further edits on the account record after creation by using a validation rule and a roll up summary field, as noted in the other answers.

  • Thank you very much for your answer. I'm not trying to force the creation of an address record if I don't have a parent record and I agree that the parent must exist first. I think my question is more towards if you the user can create an Account and an Address at the same time in the UI. – Mihai Neagoe Jan 20 '15 at 17:33
3

I agree with Adrian that a rollup field would be the best solution. If you cannot create a rollup field for any reason (for example, you've reached your limit already), you will have to use a trigger like below.

AccountTrigger.trigger

trigger AccountTrigger on Account (before update) {
    AccountTriggerHelper.checkAddress(Trigger.new);
}

AccountTriggerHelper.class

public class AccountTriggerHelper {
    public static void checkAddress(Account[] records) { 
        Set<Id> hasChildren = new Set<Id>(); 
        for(Address__c record: [SELECT Account__c FROM Address__c WHERE Id IN :records]) { 
            hasChildren.add(record.Account__c); 
        } 

        for(Account record: records) { 
            if(!hasChildren.contains(record.Id)) { 
                record.addError('An address is required to update this account.'); 
            } 
        } 
    } 
}

Keep in mind that I typed this code in a textbox and it may contain errors! Feel free to ask me to correct it.

  • 1
    public class AccountTriggerHelper { public static void checkAddress(Account[] records) { Set<Id> hasChildren = new Set<Id>(); for(Address__c record: [SELECT Account__c FROM Address__c WHERE Id IN :records]) { hasChildren.add(record.Account__c); } for(Account record: records) { if(!hasChildren.contains(record.Id)) { record.addError('An address is required to update this account.'); } } } } – sfdcfox Jan 20 '15 at 17:28
2

If this is a master detail create a roll up summary field on account and check if the value is >= 1, if not pop an error saying address is required for the master record.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.