Hi SK, Please try the below code it had completed trailhead challenge on my Org and let me know if this works for you. If still need modifications do let me know.
Apex Class:
public class ContactInsert {
public static void insertContact(List<Contact> conList) {
for(Contact conObj : conList){
if(conObj.AccountId == null) {
conObj.addError('Account not Exist');
}
}
}
}
You can use below validation rule on Contact object:
ISBLANK( AccountId )
If you want to use trigger then below is the sample code:
trigger ContactError on Contact (before insert) {
for(Contact con : trigger.new){
if(con.AccountId==null){
con.addError('Account should not be null');
}
}
}
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Try the following it works according to your requirement:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
Please try the below code it had completed trailhead challenge on my Org and let me know if this works for you. If still need modifications do let me know.
Apex Class:
Trigger Handler:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Greetings to you!
+1 Chetan
You can use below validation rule on Contact object:
If you want to use trigger then below is the sample code:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas