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 @Shivalika is there a way to limit this to run only for a particular catalog item? I have not tested this however would amazed to see if this works without a server side script in place. 

Hello @dvelloriy 

 

Yes, definitely. Yes you can put a condition on the item field - Request item does have a itne field. 

 

Just add another line to the script I gave above. 

 

if(g_form.getValue('item',YOUR VALUE); 

Then write the script I have given 

 

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

Hello @dvelloriy  

 

Refer the screenshots below, I did this. 

Shivalika_0-1742627639745.png

 

Shivalika_1-1742627728549.png


This is the client script and its working @dvelloriy 

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



 

 




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

Ankur Bawiskar
Tera Patron
Tera Patron

@dvelloriy 

you can use workflow run script or after insert business rule for this

BR Script:

current.requested_for = current.variables.primary_requested_for;
current.setWorkflow(false);
current.update();

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