+ Start a Discussion
raju123raju123 

check children s exist through validation

Hi,

 

     Let us suppose i have two objects(A,B) with look up relationship. A is parent and B is child.  

 

 

Now i want to write a validation that if A doesn't have childrens give an error message.

 

I am trying the following in "A" Object but its not working

 

 A's  Id<> $ObjectType.B.Fields.A__c  

 

 

Can any body help me Please please........................ How Can i Know Childrens exist or not through validation.

 

 

If above is not correct what is the significance of $ObjectType. please help me

 

 

please help me out

kerwintangkerwintang

I think performing a simple SOQL query could do the trick.

 

List<B> children = [select Id from B where B.A__c = A.Id];

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

    // display error

}

Niket SFNiket SF

Summary Hello Raju,

         Create a roll up summary which counts the children if count is zero you fire validation rule

goabhigogoabhigo

I don't have much information regarding $ObjectType's use in Validation rule.

 

For your question, you cannot write validation rule to check for child records if you have lookup relationship. You need master-detail relationship. Change the lookup relationship to master-detail. Create a roll-up summary field in parent to calculate no. of child records, check through validation rule for count=0.

goabhigogoabhigo

Also, you may also want to exclude records that are newly created (or created now) from this validation rule, as you cannot have child records if parent is not yet created.

Jerun JoseJerun Jose
As others have mentioned in a lookup relationship there is no standard way of throwing a validation for parent records that do not have children.

You will either need to change it to master detail or use a trigger to throw an error message.