0

Aside from standard Account object, I have this custom object named Object1__c. This custom object contains a field that references the Account object named Account__c.

This is what I aim to achieve:

There must be an error message preventing the creation of Account record if Object1__c related list from Account record does not contain any records, and if Picklist__c from Account object is equal to this value: Value1.

I created a validation rule in Account object with this criteria:

AND(ISNULL($ObjectType.Account.Fields.Id),ISPICKVAL(Picklist__c,'Value1')) 

There's no error when I check the syntax. But the problem is that it still saves the record when it's supposed not to be inserted to the database when the criteria above becomes true.

  • 1
    How can you create an Account record with a related Object1__c record? It has to be a two step process. 1. Create Account record 2. Create/Edit Object1__c record to lookup to Account record. – Nick Cook Sep 24 '16 at 15:26
  • @NickCook let's assume that I have already previous records from both objects. I have three picklist values: Value1, Value2, and Value3. The app allows to create records having the first two picklist values. Only the last picklist value should prevent a record from being saved when there's no related Object1__c record to the Account record. – curdual Sep 24 '16 at 15:35
  • @curdual but by definition if the account is being created there will never ever be any object1__c records related to it as it does not exist yet. #confused – Eric Sep 24 '16 at 17:59
0

I don't think that it is possible (or even logical) to achieve what you are asking. How can you have your Object1__c pointing to an Account record that has not been created yet? If you are inserting an Account record, the related list in your question will always be empty, since no Ojbect1__c record will be pointing to an Account record that doesn't even exists yet.

Aside to this, if you need to check if your Account record has any records in a related list, you have two options depending on the type of relationship between Account and Object1__c.

1) If you have a Master-Detail relationship between the Account and Object1__c, you need to create a Roll-up Summary field on your Account object that will COUNT the number of related Object1__c records. Then, in your validation rule you need to check the value of this Roll-up Summary field to see if it is 0.

2) If you have a lookup relationship between the Account and Object1__c, you can't use the Roll-up Summary field, but you can use a trigger that will perform the similar logic. You can run this trigger on before update to count the number of related Object1__c records and throw a validation error using the addError(..) method on sObject class.

Also, as I mentioned at the beginning of my answer, note that this validation only makes sense on Account record update.

  • I have a lookup relationship between two objects. Can you show me how to get the related list count? – curdual Sep 24 '16 at 16:37
  • 1
    hey, I've followed your suggestion #2, and it is working now, thanks for your help. – curdual Sep 24 '16 at 18:06

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.