You need to sign in to do that
Don't have an account?
kishore goud 3
Check parent field is null before inserting child record
Hi every one,
As per my requirement,I need to check whether phone field of account object is null before inserting contact.If it is null,I should not allow user to create contact.I created trigger for this scenario,but unable to achieve functionality .I am posting my trigger code,any one please help me out.
trigger CheckAcctPhoneBlank on Contact (Before insert) {
for(Contact con:[Select id,account.phone,account.name from Contact where id in:trigger.new]){
system.debug('Contact size**'+con);
system.debug('??test2??');
system.debug('Phone details**'+con.account.phone);
if(con.account.phone == null){
con.addError('Contact cannot be inserted');
}
}
}
As per my requirement,I need to check whether phone field of account object is null before inserting contact.If it is null,I should not allow user to create contact.I created trigger for this scenario,but unable to achieve functionality .I am posting my trigger code,any one please help me out.
trigger CheckAcctPhoneBlank on Contact (Before insert) {
for(Contact con:[Select id,account.phone,account.name from Contact where id in:trigger.new]){
system.debug('Contact size**'+con);
system.debug('??test2??');
system.debug('Phone details**'+con.account.phone);
if(con.account.phone == null){
con.addError('Contact cannot be inserted');
}
}
}
Try following trigger, we need to check first that contact account field is not null, if it not null then we will check that account phone is not equal to null otherwise you will get System.NullPointerException in case of null account Let us know if it helps you
Thanks
All Answers
You are not querying the account phone that is why its not checking the account phone.
Please check the below code and let me know if that solves your problem.
--
Thanks,
Prashant
Try following trigger, we need to check first that contact account field is not null, if it not null then we will check that account phone is not equal to null otherwise you will get System.NullPointerException in case of null account Let us know if it helps you
Thanks