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

J Siva
Tera Sage

Hi @dvelloriy 
If you prefer not to automatically set the "Requested For" field to the user submitting the request, you can create a "Requested For" type variable in your catalog item. This will allow the requester to select a user, and that selected user will then be mapped to the "Requested For" field in the RITM.

 

Requested for type variable:

JSiva_2-1742614005989.pngJSiva_3-1742614043499.png

 

 

Now coming to your script. As you mentioned in the script comments, the sys id of the form changes after submission.
That's why it's not giving you the expected output.


Sys id from On Submit client script:

JSiva_0-1742613864575.png

Sys id of the actual RITM record:

JSiva_1-1742613960753.png

 

Hope this helps.
Regards,
Siva

 

Hi @J Siva creating a requested for type variable works fine. However when i create the request from an incident through service operations workspace, it gives an error that the following mandatory field is not provided RITM requested for.

 

is there a way to resolve that sys id issue ?

Ahh ok understood. I don't think we can achieve that using catalog client script.

Instead you can create a after business rule on sc_req_item. In the condition, you can mention "item = <catlog item>". So that it'll be triggered only for the requests which satisfy the condition.

And in the script condition you can write below script

 

current.requested_for = current.variables.primary_requested_for;

current.update();

 

Regards,

Siva

Shivalika
Mega Sage

Hello @dvelloriy 

 

I don't think catalog client script is the right element for this. Please use " On Load Client script" directly on sc_req_item table. You won't need all this. 

 

var primaryRequestedFor = gForm.getValue('primary_requested_for');

 

if (primaryRequestedFor) {

    gForm.setValue('requested_for', primaryRequestedFor);

}

 

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