You need to sign in to do that
Don't have an account?

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
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,
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');
}
}
}
}