Mandatory Fields on a tab

paradise624
Tera Contributor

Hi All,

I have the following requirement that I need assistance with.

I have to make all fields within a tab mandatory if a field on a related record is true, however the field is hidden on the related record so creating a UI policy doesn't work for me. So I've created the following client script, however this doesn't work. What am I doing wrong here?

function onLoad() {
var color = g_form.getValue('u_form1.u_color');   ***dot-walked field from related form (1)**
if (color == 'red') {                                                  **field name on related form(1), if field name color = red
g_form.setMandatory('u_brick', true);                    ***Set field (brick) on form (2) to mandatory true
}}

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi,

Please follow the steps below to achieve your requirement:

Since it's a hidden field it will always give you blank when you will try to fetch it in a On Load Client Script.

So, you need to define a Display Business Rule first on your Parent Table and check if Related List record field value is True or not.

Say for example, I have done this On Incident Table and I am checking if my field "u_field_to_check" present on Incident Task table is True or not then I am making All fields under Related Record On Incident table as Mandatory

Business Rule on Parent Table and use the script below:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var gr = new GlideRecord('incident_task'); // Related List Child Table Name Here
	gr.addQuery('incident',current.sys_id); // Replace "incident" with the field which connect both your table and should be present on your Related List Table
	gr.addQuery('u_field_to_check',true); // Replace "u_field_to_check" with field against which you want to check
	gr.query();
	if(gr.next()){
		g_scratchpad.makeMandate = true;
	}else{
		g_scratchpad.makeMandate = false;
	}

})(current, previous);

find_real_file.png

find_real_file.png

Now write an On Load Client Script on your Incident Table and use the script below:

function onLoad() {
    //Type appropriate comment here, and begin script belowactive=true^work_start=NULL
    if (g_scratchpad.makeMandate == true) {
        var fields = ['problem_id','field2','field3']; // Add your field Name here which you want to make mandatory
        for (var i = 0; i < fields.length; i++) {
            g_form.setMandatory(fields[i], true);
        }


    }
}

find_real_file.png

Result:

I have a field "Field to Check" marked as True on my Related List table i.e. Incident Task and when I reload my Incident form Problem field on Incident under Related record gets mandatory as shown below:

Related List Table:

find_real_file.png

Parent Incident Form Field Mandatory:

find_real_file.png

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

3 REPLIES 3

Ct111
Tera Sage

Chk answer of Shlok from this thread , it might be good reference point .

Link : https://community.servicenow.com/community?id=community_question&sys_id=cc6687e1db1cdbc01dcaf3231f9619b4

 

Mark my Answer as Correct and Helpful if it helps

Saurav11
Kilo Patron
Kilo Patron

Description is a plain text field, so you cannot show a hyperlink in it. You could add an HTML description field (HR Case includes one OOB called rich_description) and replace the regular Description field on your form.

Please Mark as ✅ Correct if this solves your issue and also mark ???? Helpful if it helps resolve your problem.

Thanks.

shloke04
Kilo Patron

Hi,

Please follow the steps below to achieve your requirement:

Since it's a hidden field it will always give you blank when you will try to fetch it in a On Load Client Script.

So, you need to define a Display Business Rule first on your Parent Table and check if Related List record field value is True or not.

Say for example, I have done this On Incident Table and I am checking if my field "u_field_to_check" present on Incident Task table is True or not then I am making All fields under Related Record On Incident table as Mandatory

Business Rule on Parent Table and use the script below:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var gr = new GlideRecord('incident_task'); // Related List Child Table Name Here
	gr.addQuery('incident',current.sys_id); // Replace "incident" with the field which connect both your table and should be present on your Related List Table
	gr.addQuery('u_field_to_check',true); // Replace "u_field_to_check" with field against which you want to check
	gr.query();
	if(gr.next()){
		g_scratchpad.makeMandate = true;
	}else{
		g_scratchpad.makeMandate = false;
	}

})(current, previous);

find_real_file.png

find_real_file.png

Now write an On Load Client Script on your Incident Table and use the script below:

function onLoad() {
    //Type appropriate comment here, and begin script belowactive=true^work_start=NULL
    if (g_scratchpad.makeMandate == true) {
        var fields = ['problem_id','field2','field3']; // Add your field Name here which you want to make mandatory
        for (var i = 0; i < fields.length; i++) {
            g_form.setMandatory(fields[i], true);
        }


    }
}

find_real_file.png

Result:

I have a field "Field to Check" marked as True on my Related List table i.e. Incident Task and when I reload my Incident form Problem field on Incident under Related record gets mandatory as shown below:

Related List Table:

find_real_file.png

Parent Incident Form Field Mandatory:

find_real_file.png

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke