Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to set RITM state value from Client Script running on SC_TASK

kshaw
Giga Guru

I have a client script running on a sc_task. Script is onChange reacting to a choice variable that is Review Status.  (script  is shown below).

Intent of script is to get the value of the Review Status variable (4 choices: Under Review, Awaiting Client,  Awaiting Job Builder, and Completed) and then use the selected value to set the state on the requested_item from the catalog task. Based on the alerts, I am going to the correct switch elements but the RITM state is not being updated.

Possibly guessing that using g_form.SetValue('request_item.state', 'XX') is not working and maybe I need glide record but I am not sure.

Please help with correct coding to do this.

Thanks

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var status = g_form.getValue('comp_reviewStatus');
	alert("The selected reveiw status is " + status);
    switch (status) {
        case "under_review":
            //g_form.setValue('state', '2');
			g_form.setValue('request_item.state', '2');
			alert("On the Under Review branch");
            break;
        case "awaiting_client":
            //g_form.setValue('state', 'Awaiting Client');
			g_form.setValue('request_item.state', '5');
			alert("On the Awaiting Client branch");
            break;
        case "job_builder":
            //g_form.setValue('state', 'Awaiting Job Builder');
			g_form.setValue('request_item.state', '22');
			alert("On the Awaiting Job Builder branch");
            break;
        case "completed":
            //g_form.setValue('state', '2');
			g_form.setValue('request_item.state', '2');
			alert("On the Completed branch");
            break;
    }
}

 

5 REPLIES 5

Allen Andreas
Tera Patron

Hi,

You're unable to set the RITM state value that was in an sc_task client script. You'd have to utilize GlideAjax to communicate from the client to the server, to set that record's value or...consider using an "after" business rule on the sc_task table to change the RITM value (via GlideRecord) after the sc_task has been saved to the database.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Thank you for your response. I have now used Glide Ajax to set the status on sc_req_item.

Problem I am having is getting the RITM number on the sc_task client script.

 

var ritm = g_form.getValue('current.request_item.number');

 

Dot walking isn't working. Must have something wrong.

 

Full client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var status = g_form.getValue('current.variables.comp_reviewStatus');
	var ritm = g_form.getValue('current.request_item.number');
	alert("The selected reveiw status is " + status + " for " + ritm);
	var ritmStatus;
    
	switch (status) {
        case "under_review":
            ritmStatus = 2; // state = Work in Progress
			alert("On the Under Review branch");
            break;
        case "awaiting_client":
            ritmStatus = 5; // state = Awaiting Client
			alert("On the Awaiting Client branch");
            break;
        case "job_builder":
            ritmStatus = 18; // state = Awaiting Job Builder
			alert("On the Awaiting Job Builder branch");
            break;
        case "completed":
            ritmStatus = 2; // state = Work in Progress
			alert("On the Completed branch");
            break;
    }
	
	// ajax script call to set State on RITM
	var setState = new GlideAjax('SHR_set_RITM_State');
    gaTitle.addParam('sysparm_name', 'setState');
    gaTitle.addParam('sysparm_ritm', ritm);
	gaTitle.addParam('sysparm_status', ritmStatus);
    //gaTitle.getXML(Job_Answer);
}

Hello,

You don't have access to "current" in client script, you have access to g_form to retrieve form values.

In the request_item field on the sc_task is a reference to the RITM table, thus you get an sys_id of the RITM record, that's all you'd need to pass to your script include.

 

In your script include, query the sc_req_item table using the sys_id that was passed to the script include from the client (not number).


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Benlem
Tera Expert

Hi,

 

you cannot change the RITM value about the form. You have to use a GlideRecord.

 

Create a Business Rule on sc_task that will be executed after the update. The filter condition can be based on changes in the Review Status, so that it triggers only then.

 

Benlem_0-1679001665882.png

 

 Then make a script which switches the Review Status and updates the RITM:

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

    var ritmStatus;

    switch (current.comp_reviewStatus) {
        case 'under_review':
            ritmStatus = 2;
            break;

        case 'awaiting_client':
            ritmStatus = 5;
            break;
    }

    var gr = new GlideRecord('sc_req_item');
    gr.get(current.request_item);
    gr.setValue('state', ritmStatus);
    gr.update();

})(current, previous);

 

Greetings

 

Please mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!