Whats wrong with this script?

dvelloriy
Kilo Sage

Hello All,

I am trying to sync a variable "Primary Requested for" (primary_requested_for) created on a catalog form to the RITM requested for field (requested_for) using a catalog client script and a script include.

However its not working as expected. When i submit the form, i still see my name in the requested for on RITM instead of primary requested for's name.

Can someone please advise?

 

Catalog client script:

Type: On Submit

 

Script:

function onSubmit() {
    // Get the value of the 'Requested For' variable
    var requestedFor = g_form.getValue('primary_requested_for');

    if (requestedFor) {
        // Use GlideAjax to call the Script Include
        var ga = new GlideAjax('SyncRequestedForUtil');
        ga.addParam('sysparm_name', 'updateRITMRequestedFor');
        ga.addParam('sysparm_ritm_sys_id', g_form.getUniqueValue()); // not sure if this is correct, as the RITM has not been generated yet.
        ga.addParam('sysparm_requested_for', requestedFor);

        ga.getXML(function(response) {
            var answer = response.responseXML.documentElement.getAttribute('answer');
            if (answer === 'success') {
                // Optionally, notify the user or log success
                console.log('RITM Requested For updated successfully');
            } else {
                console.log('Error updating RITM Requested For');
            }
        });
    }
    return true; // Allow form submission to proceed
}
--------------------------------------------
 
Script include:
 
var SyncRequestedForUtil = Class.create();
SyncRequestedForUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    updateRITMRequestedFor: function() {
        var ritmSysId = this.getParameter('sysparm_ritm_sys_id');
        var requestedFor = this.getParameter('sysparm_requested_for');

        if (ritmSysId && requestedFor) {
            var gr = new GlideRecord('sc_req_item');
            if (gr.get(ritmSysId)) {
                gr.requested_for = requestedFor;
                gr.update();
                return 'success';
            }
        }
        return 'error';
    },

    type: 'SyncRequestedForUtil'
});
14 REPLIES 14

Hi @Ankur Bawiskar , writing the BR on sc_req_item table might create performance issues. This br will run for all records inserted in req item table. 

is there an efficient way to do this?

Hello @dvelloriy 

 

Did you try the client script I shared ? I have tried it in my instance and it will work. Do you have any concerns with using the same ? 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

@dvelloriy 

in your BR you can give condition

current.cat_item.name == 'Your Item Name Here'

you can have flow configured on your catalog item and do the same

1) Get Catalog Variables flow action

2) Update the Record flow action

3) Set the Requested for = Get Catalog Variables-> Primary Requested For variable

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

@dvelloriy 

Hope you are doing good.

Did my reply answer your question?

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

Shivalika
Mega Sage

Hello @dvelloriy 

 

 

Please confirm if you checked my answer. Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for my efforts and also it can move from unsolved bucket to solved bucket. 

 

Regards, 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeE