function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Surbhi ModiSurbhi Modi 

Trigger to make the contact and phone fields mandatory on contact object

Akshay Dhiman 63Akshay Dhiman 63
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.

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
ANUTEJANUTEJ (Salesforce Developers) 
Hi Surabhi,

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:

User-added image

User-added image
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.
ShirishaShirisha (Salesforce Developers) 
Hi Surbi,

Greetings!

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.

Warm Regards,
Shirisha Pathuri
Surbhi ModiSurbhi Modi
Hi, Thank you for your reply. But my question was bit wrong . I need to make the phone and email field mandatory on contact object.
ANUTEJANUTEJ (Salesforce Developers) 
So the one way is to have a trigger as above which checks both and throws an error or the other way is to have a validation rule on email field.

If you use trigger you need to change the above snippet lines to this:
 
trigger ContactTrigger on Contact(before insert, before update) {
    for(Contact c:trigger.new) {
        if( String.isBlank(c.Phone)  || String.isBlank(c.Email)) {
            c.Phone.addError('The Phone and Email Fields are required');
        }
    }
}
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.