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
TN AdminTN Admin 

showing error in parent object if two objects have M-D/lookup relationship

Objects: Opportunity and custom object(CE) have M-D relation ship

Q: Is it possible to check the child object fields on stage change in opportunity and throw the error in parent object if child object fields are blank.

please let me know how can i achive this ...
BALAJI CHBALAJI CH
Hi TN Admin,
Yes, it is possible. We can use trigger on Opportunity object. Please find below sample code.
trigger ChldObjCheck on Opportunity (before update) {
    
    for(Opportunity p: trigger.new)
    {
        CustomObject__c c = [select id /* Fields you want to check. Example: test__c*/ from CustomObject__c where OpportunityId =: p.Id limit 1];
        
        if(c.test__c = null || c.test__c = '')
        {
            p.StageName.addError('Child Object's fields are Empty');
        }
    }
}

Please let me know if that helps you.

Best Regards,
BALAJI
Vasani ParthVasani Parth
TN Admin :

You can do this in two ways
  1.  If there is a Master-Detail relationship you might be able to create a Rollup Summary field that returns a Count or SUM of a numeric value from the Child object.  The numeric value would be a "score" that is set when the Child record that is editied based on the check-off criteria being true/false. Then write a VR on the Opportunity that evaluates the Opportunity Stage and the RS Field and throws an error if the RS(Score) is not sufficient.  
  2. if youcreate a Lookup field that connects the Opportunity to the second object, remove the master detail relationship, You can just use some standard validation rules.  
Please mark this as the best answer if this helps
Pankaj_GanwaniPankaj_Ganwani
Hi,

You can use trigger approach to accomplish this. 
 
trigger test on Opportunity(before update)
{
    Set<Id> oppIds = new Set<Id>();
	for(Opportunity obj : Trigger.new)
	{		
		if(obj.StageName!=Trigger.oldMap.get(obj.Id).StageName)
		{
			oppIds.add(obj.Id);
		}
	}
	
	for(Custom_Object__c obj : [select Opportunity__c from Custom_Object__c where Opportunity__c In : oppIds AND field__c=NULL AND field2__c=NULL...so on])
	{
		oppIds.remove(obj.Opportunity__c);
	}
	
	for(Opportunity obj : Trigger.new)
	{		
		if(obj.StageName!=Trigger.oldMap.get(obj.Id).StageName && !oppIds.contains(obj.Id))
			obj.addError('error..........');
	}
}

Just replace Custom_Object__c with your child object API name and field__c, field1__c, field2__c with the fields on this custom object.

Thanks,
Pankaj