Pull variable from another Requested Item Record and set it to a current variable in a new record

Jimmy45
Giga Guru

Hey community!  I am sure this is simple, but am new to the scripting world.  Please bear with me:

 

Record 1 - Requested Item has a variable that is filled in by the user

Variable is called app_name

They fill out the rest of the form and submit it and they get their REQ/RITM number.

 

Record 2 - User goes to fill out the same form, they choose to follow up on the previous ticket

Variable is called previous_ritm (user types in their old RITM number) 

They fill out the rest of the form and submit it and they get their REQ/RITM number.

 

Now here is my requirement.  How do I pull in the app_name variable (from the old ticket) into the new ticket under the variable name previous_app?  I created an onLoad catalog client script with the following code and keep getting either 'undefined' or the field just stays blank.  Maybe extra sets of eyes can tell me what it is I am missing here or what that code looks like?

Here is my code:

function onLoad() {
    //Type appropriate comment here, and begin script below

    var cRITM = g_form.getValue('variables.previous_ritm'); //get value of variable previous_ritm
    var pRITM = new GlideRecord('sc_req_item'); // check previous RITMS with value of current variable previous_ritm
    pRITM.addQuery('number', cRITM); 
    pRITM.query();
    if (pRITM.next()) 
        variables.previous_app = g_form.getValue('pRITM.variables.app_name');
	


}

 

1 ACCEPTED SOLUTION

This is a limitation of running a GlideRecord in a client script, which I should have mentioned from the outset is not supported nor considered best practice anyway.  The variables are stored as an object on the sc_req_item record, so it's just like if you wanted to return the Name (display value) of a reference field, you would need to dot-walk to get to it, which you can do all day on a server script.

 

To populate variables from a supplied RITM that is a reference variable, you would need a Client Script that looks like this - calling a Script Include via GlideAjax.  You can do this onLoad, and/or onChange when the RITM value changes:

function onLoad() {
    var gr = new GlideAjax('RITMUtils');
    gr.addParam('sysparm_name', 'getVars');
    gr.addParam('sysparm_ritm', g_form.getValue('previous_ritm'));
    gr.getXML(vars);
}

function vars(response) {
	var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
	g_form.setValue('previous_app', answer.app_name);
}

Next create a Script Include using the same name as in the GlideAjax call.  Ensure the Client callable box is checked.

var RITMUtils = Class.create();
RITMUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getVars: function() {

        var varObj = {};
		var json = new JSON();
		var ritm = this.getParameter('sysparm_ritm');
        var gr = new GlideRecord('sc_req_item');
        gr.addQuery('sys_id', ritm);
        gr.query();
        if(gr.next()){
			varObj.app_name = gr.variables.app_name.toString();
            //add a similar line for any other fields or variables from the RITM that you want to use in the Client Script
		}
		return json.encode(varObj);
    },
    type: 'RITMUtils'
});

View solution in original post

8 REPLIES 8

This is a limitation of running a GlideRecord in a client script, which I should have mentioned from the outset is not supported nor considered best practice anyway.  The variables are stored as an object on the sc_req_item record, so it's just like if you wanted to return the Name (display value) of a reference field, you would need to dot-walk to get to it, which you can do all day on a server script.

 

To populate variables from a supplied RITM that is a reference variable, you would need a Client Script that looks like this - calling a Script Include via GlideAjax.  You can do this onLoad, and/or onChange when the RITM value changes:

function onLoad() {
    var gr = new GlideAjax('RITMUtils');
    gr.addParam('sysparm_name', 'getVars');
    gr.addParam('sysparm_ritm', g_form.getValue('previous_ritm'));
    gr.getXML(vars);
}

function vars(response) {
	var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
	g_form.setValue('previous_app', answer.app_name);
}

Next create a Script Include using the same name as in the GlideAjax call.  Ensure the Client callable box is checked.

var RITMUtils = Class.create();
RITMUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getVars: function() {

        var varObj = {};
		var json = new JSON();
		var ritm = this.getParameter('sysparm_ritm');
        var gr = new GlideRecord('sc_req_item');
        gr.addQuery('sys_id', ritm);
        gr.query();
        if(gr.next()){
			varObj.app_name = gr.variables.app_name.toString();
            //add a similar line for any other fields or variables from the RITM that you want to use in the Client Script
		}
		return json.encode(varObj);
    },
    type: 'RITMUtils'
});

Hi Brad,

 

If we did not have a previous_ritm field, how could you identify the first RITM that is completed in the order guide and get the value from that RITM?  

When RITM records are created via an order guide, the order_guide field is populated, so the client script could pass in this value from the current RITM, and the Script Include would first do a GlideRecord on the sc_req_item table where the order_guide field matches that value, then whatever State, Item, or Active status makes that one unique.  Once the GlideRecord finds that record you can return to the client any fields or variables from it.

Jimmy45
Giga Guru

@Chuck Tomasi - Your videos and guidance have helped me tremendously.  I know you are probably busy, but do you know why I keep getting undefined with the last code snippet?  For the life of me, I can't figure it out.  Much appreciation in advance.