Client Script | on SC_req_item

Ankita NA
Tera Contributor

Hi All,

I am in need of help for below onLoad client script which is written for sc_req_item table , the scope is RITM is getting created for catalog item in request table so based on the variables value of req type and application name in this RITM I need to set value of the cmdb_ci file on RITM.


My Req:

I req type is one of new, old or existing, then get value of application name and set the value of cmdb_ci field.


With below script its working only when the req type is new and for other two its setting blank value.


function onLoad() {

var item = g_form.getValue('cat_item');

var req_type = g_form.getValue('what_type_of_request'); // a select box field with with 4 choices(new, old, existing, modified)
var application_name = g_form.getValue('variables.u_application_name');// a reference field (referencing application table)which comes only when the request type is selected

if (item == '347ea76d97b51dd0bdc08f171153af8e' && (req_type == "new" ||req_type == "existing" || req_type == "old") {

g_form.setValue("cmdb_ci", application_name);

}
}


Thanks,
Ankita

5 REPLIES 5

Tushar
Kilo Sage
Kilo Sage

Hi @Ankita NA 

 

I hope this should work for you -

 

function onLoad() {
    var item = g_form.getValue('cat_item');
    var req_type = g_form.getValue('what_type_of_request');
    var application_name = g_form.getValue('variables.u_application_name');

    if (item == '347ea76d97b51dd0bdc08f171153af8e' && (req_type == "new" || req_type == "existing" || req_type == "old")) {
        // Check if the application_name is not empty before setting the cmdb_ci field
        if (application_name) {
            g_form.setValue("cmdb_ci", application_name);
        } else {
            // If the application_name is empty, you may want to handle this case or display an error message.
            // For now, we will just log a message in the console.
            console.log("Application name is empty or null. Unable to set cmdb_ci field.");
        }
    }
}

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

Ankur Bawiskar
Tera Patron
Tera Patron

@Ankita NA 

this is catalog client script or normal client script?

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

@Ankita NA 

if it's normal client script on RITM table then update as this

I assume what_type_of_request is variable of type select box

function onLoad() {

var item = g_form.getValue('cat_item');

var req_type = g_form.getValue('variables.what_type_of_request'); // a select box field with with 4 choices(new, old, existing, modified)
var application_name = g_form.getValue('variables.u_application_name');// a reference field (referencing application table)which comes only when the request type is selected



if (item == '347ea76d97b51dd0bdc08f171153af8e' && (req_type == "new" ||req_type == "existing" || req_type == "old")) {

g_form.setValue("cmdb_ci", application_name);

}
}

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

Hi Ankur, yes, it's a normal client script I tried this way the issue is its setting the value only for when req type is new and for rest its setting blank, do let me know what the possible reason for it could be if you have any idea.