Mark a field Required only for new records

How to make a field mandatory only if creating a record?

For example lets make “Office phone” required

Lets do this,

Steps are as below,

1.Add a reference to the JavaScript file which will be needed for event binding.

Path: custom/modules/<desired_module>/metadata/editviewdefs.php

<?php

$viewdefs['<desired_module>']['EditView']['templateMeta']['includes'] =
    array (
        array (
        'file' => 'custom/modules/<desired_module>/js/editview.js',
        ),
    );
?>

2.Add the JavaScript file you want to include into the location you referenced above(custom/modules/<desired_module>/js/editview.js).

3. Write following code in editview.js file.

$(document).ready(function() {
    var record = $("input[name=record]").val();
	if(record == ""){
		makerequired();
	}
});

function makerequired(){
	addToValidate('EditView','phone_office','varchar',true,'Office Phone');    // mark Office Phone field required
	$('#phone_office_label').html('Office Phone: <font color="red">*</font>'); // with red * sign next to label
}

4. Quick Repair, then hard refresh your browser. All Done !

5. Done! Refresh the page and start testing.

Note :

1. Read the comments in code carefully and replace the variables as asked.
2. Here, <desired_module> means the module name you see in the URL, for example, Contacts, Leads, Accounts, etc.
3. You may find the field names and its labels in Admin > Studio > <Your_module> > Fields > <Choose correct field> > Take 
Field Name and System Label.
After Customization

Hope you find this blog post helpful.

Feel free to add comments and queries, that helps us to improve the quality of posts.

You can contact us at info@infotechbuddies.com

Thank you.

Spread the love

One thought on “Mark a field Required only for new records

Leave a Reply

Your email address will not be published. Required fields are marked *