trigger triggerOnContact on Contact (before Delete) {
if(Trigger.isDelete){
if(trigger.isBefore){
contactTriggerHelper.restrictToDeleteContact(trigger.old);
}
}
Helper Class
public class contactTriggerHelper{
public static void restrictToDeleteContact(list<Contact> ContactList){
try{
for(Contact conObj : ContactList){
if(conObj .AccountId == null){
conObj .AddError('Cannot delete contact because it is not associated with account');
}
}
}catch(exception e){
system.debug('get error message ==>'+e.getMessage()+' in line number ==> '+e.getLineNumber());
}
}
}
If you find your Solution then mark this as the best answer.
trigger Trigger_Example1 on Contact (before insert) { List<contact>cnlist=new List<contact>(); for(contact c:Trigger.new){ if(c.AccountId==null){ c.adderror('contact record should have an account'); }
trigger triggerOnContact on Contact (before Delete) {
if(Trigger.isDelete){
if(trigger.isBefore){
contactTriggerHelper.restrictToDeleteContact(trigger.old);
}
}
Helper Class
public class contactTriggerHelper{
public static void restrictToDeleteContact(list<Contact> ContactList){
try{
for(Contact conObj : ContactList){
if(conObj .AccountId == null){
conObj .AddError('Cannot delete contact because it is not associated with account');
}
}
}catch(exception e){
system.debug('get error message ==>'+e.getMessage()+' in line number ==> '+e.getLineNumber());
}
}
}
If you find your Solution then mark this as the best answer.
try below code ,it will help you
If you find your Solution then mark this as the best answer.
Thank you!
Regards,
Suraj Tripathi
All Answers
Try Below Trigger Please Mark It As Best Answer If It Helps
Thank You!
trigger Trigger_Example1 on Contact (before insert) {
List<contact>cnlist=new List<contact>();
for(contact c:Trigger.new){
if(c.AccountId==null){
c.adderror('contact record should have an account');
}
}
}
try below code ,it will help you
If you find your Solution then mark this as the best answer.
Thank you!
Regards,
Suraj Tripathi