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
niraj kumar 45niraj kumar 45 

Validation rule on Master object

I have a validation rule on master object(Opportunity).I have rollup summary field on parent Object. I am checking in the validation that if no records is there in the child object then fire this validation rule. But i am facing issue while deleting the last child object record which is in the related list of parent as the validation rule fire first and does not allow me to delete the last child record. 
Please let me know how do i update my validation ruleso that it allows me to delete all the child records and should fire when when insert or update the parent record. Below is my validation rule:(Total_Resource_Badge__c is a rollup summary field)

AND(OR( 
ISPICKVAL(StageName, '3. Solutioning'), 
ISPICKVAL(StageName, '4. End-Game'), 
ISPICKVAL(StageName, '5. Negotiation'), 
ISPICKVAL(StageName, 'Won') 
), 
INCLUDES(Delivery_Construct__c , "Involves rebadging of resources") ,
Total_Resource_Badge__c=0)
Rakesh ARakesh A
As you have written a validation rule when count is zero, validation rule means, it stops saving record when criteria met and throws error. I think this is as expected.

now tell me, why this vaidation rule. what you are trying to achieve. i will help you
niraj kumar 45niraj kumar 45
Thanks for your quick response Rakesh.
It is working fine for parent record whenever it is inserted/updated. But I am facing issue in child record when I am trying to delete the last child records, the validation rule fires again as the count is upadted to zero. My requirement is that it should allow me to delete all child records.So in what way I can modify my validaion rule so that it works for Parent records the way it is working now and should also allow me to delete all its child records.
Rakesh ARakesh A
add prior to that field as below and try
 
Prior(Total_Resource_Badge__c)=0)
niraj kumar 45niraj kumar 45
I am also thinking of priorValue but I think It will give me error if I try to insert new child records when no childs records are there for that parent.As in validation rule it will get the prior value as zero, validation rule will be fored and it would not allow me to insert the child record.
niraj kumar 45niraj kumar 45
Do you think this would work for both secnarios:
Total_Resource_Badge__c<=
 PRIORVALUE(Total_Resource_Badge__c)
)
Rakesh ARakesh A
Hi Niraj, 
This may work but it should be a overall condition. without changing your code you can keep your entire block in this code, like..

if(Total_Resource_Badge__c<= PRIORVALUE(Total_Resource_Badge__c))
{
your code here.
}

Now my solution:
Roll up summery field cannot be updated by parent. so always prior and current values are equal.
and when you put prior == 0 , this is only for parent records so your validation rule on parent works fine.

Coming to child, once the child records get deleted prior will have some value so validation wont fire.
once it is set zero it wont be updated again to zero, because you cannot empty the child record set when already empty. So validation wont fire.

This is my imagination. Try it, it wont harm the flow as I think
 
niraj kumar 45niraj kumar 45
Hi Rakesh,
I understand that it will not fire when I delete all the child records and try to insert new child record for that parent. But take another scenario where I have a parent records already created/edited and there so no child records for that parent. As per my understanding when I try to insert new record for this parent its prior value will be zero as there was no child records before. I am not sure unless I test it. But do you think it can cause a issue while insertion?
niraj kumar 45niraj kumar 45
Hi Rakesh .....I misunderstood again....if it satisfies the above condition it won't allow me to save the parent record as Validation rule will be fired.
I think it should work the way you have explained.
Rakesh ARakesh A
I am wondering..!!
If you dnt want to stop parent record as well, then y you have validation rule?
At what time you are expecting this to be fired? and upon which object?