Hi Surbhi Modi, I have checked your requirements and I have written the Trigger for the same. The trigger will show the error when a contact is created and the phone and Mobilephone fields are null then it will show an error. But can you please explain what do you mean by the contact field here if it is a custom field then you can also do the same as I have done for phone and Mobilephone fields? Here is Your code for this.
You can have a simple validation rule, to create one you can simply navigate to setup> go to objects> contact object> select fields and relationships >choose the field from the list [phone] >click on the field >under validation rule click new > give a name and use the below line in the Error Condition Formula > add error message >click save
ISBLANK(Enter_Custom_Field_API_Name_Here__c)
Below are the photos for the validation rule I used in my org:
Using the null or empty check we can add the error message(addError() method) like below:
trigger ContactTrigger on Contact(before insert, before update) {
for(Contact c:trigger.new) {
if(c.Phone=='' ) {
c.Phone.addError('This Name is a required field.');
}
}
}
Kindly mark it as best answer if it helps so that it can help others in the future.
I have checked your requirements and I have written the Trigger for the same. The trigger will show the error when a contact is created and the phone and Mobilephone fields are null then it will show an error.
But can you please explain what do you mean by the contact field here if it is a custom field then you can also do the same as I have done for phone and Mobilephone fields?
Here is Your code for this.
trigger contacttrigger on Contact (before insert) {
if(Trigger.IsBefore) {
if(Trigger.IsInsert) {
contacttriggerhandler.beforeinsert(Trigger.new);
}
}
}
trigger handler class
public class contacttriggerhandler {
public static void beforeinsert(List<Contact> contactList) {
try{
if(!contactList.isEmpty()) {
for(Contact conObj : contactList) {
if(conObj.Phone == null || conObj.MobilePhone == null) {
conObj.addError('Phone or Mobilephone is null');
}
}
}
}catch (Exception ex) {
System.debug('ERROR MESSAGE IS '+ex.getMessage()+' ERROR LINE NUMBER IS '+ex.getLineNumber());
}
}
}
Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay
You can have a simple validation rule, to create one you can simply navigate to setup> go to objects> contact object> select fields and relationships >choose the field from the list [phone] >click on the field >under validation rule click new > give a name and use the below line in the Error Condition Formula > add error message >click save
ISBLANK(Enter_Custom_Field_API_Name_Here__c)
Below are the photos for the validation rule I used in my org:
https://help.salesforce.com/articleView?id=000323820&type=1&mode=1
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.
Thanks.
Greetings!
Using the null or empty check we can add the error message(addError() method) like below:
Kindly mark it as best answer if it helps so that it can help others in the future.
Warm Regards,
Shirisha Pathuri
If you use trigger you need to change the above snippet lines to this:
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.
Thanks.