How can I get the value of a variable within another reference variable?

Calli
Tera Contributor

I have a reference field for the "cmdb_ci" table, within the incident form and I need to get the value of a field that is within this record called "u_safe_box" (it's a check box).

Group 1.png

 

(I'm using client script onChange for this, I tried using var clrID = g_form.getValue('u_safe_box'); )

 

When a CI is entered that the record has "u_safe_box" as 'true', it has to clear the field and display a message.

How can I do this?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Calli 

you can get the value using g_form.getReference() with callback method

g_form.getReference('cmdb_ciField', function(ref){
var val = ref.u_safe_box;		
alert();
	});

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Deepak Negi
Mega Sage
Mega Sage

Hi

 

Please follow the steps:

1. Write a Client Callable script include and to return the checkbox value.

2. Write an onchange script to call the above script include's method via GlideAjax

Refer: GlideAjax Example Cheat Sheet (UPDATED) - ServiceNow Community

3. based on the returned value, if it returns true, clear the value of the field

 

Thanks

Deepak

Ankur Bawiskar
Tera Patron
Tera Patron

@Calli 

you can get the value using g_form.getReference() with callback method

g_form.getReference('cmdb_ciField', function(ref){
var val = ref.u_safe_box;		
alert();
	});

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you all for the solutions!

 

I tried to do it with "GlideAjax", but it didn't work, it didn't return the value. However, with "g_form.getReference()", it worked.

 

Code:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {
        return;
    }
    var safeBox = g_form.getReference('cmdb_ci', _incHandle); // Reference Field, Callback
    
    function _incHandle(safeBox) {
        try {
            if (safeBox && String(safeBox).trim() !== '') {
                if (safeBox.getValue('u_safe_box') === 'true') {
                    g_form.clearValue('cmdb_ci');
                    g_form.addErrorMessage(getMessage('This IC cannot be associated with the call'));
                }
            }
        } catch (err) {
            g_form.addErrorMessage('Erro inesperado: ' + err);
        }
    }
}

 

Sumanth16
Kilo Patron

Hi @Calli ,

you can add fields based on your requirement.

Create Script Includes;

Name: commonUtils

Client callable: true (checked)

Script:

var commonUtils = Class.create();
commonUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getUserInfo: function(){
		var user = this.getParameter('sysparm_user');
		var gr = new GlideRecord("sys_user");
		gr.get(user);
		
		var deptname = gr.department.getDisplayValue();
		var dept = gr.getValue('department');
		var manager = gr.getValue('manager');
		var managername = gr.manager.getDisplayValue();
		var location = gr.getValue('location');
		var locationname = gr.location.getDisplayValue();
		
		var response = {};
		response.deptname = deptname;
		response.dept = dept;
		response.manager = manager;
		response.managername = managername;
		response.location = location;
		response.locationname = locationname;

		return JSON.stringify(response);
	},

	type: 'commonUtils'
});

 

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	// Call the GA function
	var ga = new GlideAjax('commonUtils');
	ga.addParam('sysparm_name', "getUserInfo");
	ga.addParam('sysparm_user', newValue); //Onchange field is reference field to sys_user table
	ga.getXMLAnswer(function(answer){
		var response = JSON.parse(answer);
		
		g_form.setValue('department_name', response.deptname); //used for string field
		g_form.setValue('department', response.dept); //used for reference field to department table
		
		g_form.setValue('manager_name', response.managername); //used for string field
		g_form.setValue('manager', response.manager); //used for reference field to user table
		
		g_form.setValue('location_name', response.locationname); //used for string field
		g_form.setValue('location', response.location); //used for reference field to location table
		
	});
}

 

Mark the comment as a correct answer and also helpful once worked.

 

Thanks & Regards,
Sumanth Meda