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
niki s 7niki s 7 

How to show custom validation from trigger

I have two custom object one is parent and another one is child and there are some fields which are same in both the object. Now if I update fields in parent object then it will also update the same in child object. custom validation is present in child object. so I have used try nd catch block but what I am getting it handle the custom validation and updated the parent object but child object is not getting updated due to custom validation present in child object . What I want is that I want to show the message from parent object nd stop it from updating the parent record also. I want to achieve this through code. Please help me
VinayVinay (Salesforce Developers) 
Hi Niki,

There is no direct answer to the above requirement.  I would suggest to implement logic and come back if you face or stuck on any error.

Below are examples that you can take a look at.

http://sfdcsrini.blogspot.com/2014/09/salesforce-validation-in-trigger.html
https://andyinthecloud.com/2015/04/19/considerations-placing-validation-code-in-an-apex-triggers/
https://krissparks.medium.com/apex-triggers-for-custom-validations-fcac30c76281

Thanks,
niki s 7niki s 7
trigger AccountUpdate on Account (after update) {
List<Account> parentOppList =[select Id,lookup_opp__c,test__c,Num__c,(select id,test__c,Num__c from contacts) from Account where Id in : trigger.new ] ;      
       List<Contact> oppList = new List<Contact>();
        for(Account parOpp : parentOppList)
        {
            System.debug('inside for '+parOpp.lookup_opp__c);
       
             System.debug('inside for '+parOpp.lookup_opp__c);
             for(Contact childContacts : parOpp.contacts ){
            Contact opp = new  Contact();
            opp.lookup_opp__c = parOpp.lookup_opp__c;
            opp.id=childContacts.id;
            opp.test__c = parOpp.test__c;
            opp.Num__c = parOpp.Num__c;
            System.debug('inside for '+opp.lookup_opp__c);

            oppList.add(opp) ;
         
       // }
    try{
             if (!oppList.isEmpty()){
                 update oppList;
         System.debug('inside catch');
          }
           }
               catch(Exception e)
                 {
                 System.debug('inside catch');
                             
                         throw new DmlException('This is bad number');    
                        // Trigger.New[0].addError('Write error message');

                     }
    }

           
         }
                     
}
niki s 7niki s 7
Hi thanks , this is my code I want to show the custom validation message please help this is my code.. there is a custom validation present in contact on num__c field . Num fields should be less than 50 . And I am updating this field from account field.. while updating if I insert num value greater than 50 than getting error. I just want to display the message .. please help