To populate the requestor of catalog item from IMS record in Agent Workspace

rishabh31
Mega Sage

Dear Team,

I am following the below-solved URL to achieve that same requirement and tried the steps and script as provided in this:

https://www.servicenow.com/community/csm-forum/passing-variables-from-interaction-record-ti-catalog-...

In my case, I need to populate 'requestor' instead of 'requested for' of catalog item. Accordingly, I modified the onload catalog client script (screenshot and script below) as provided in the above URL-

rishabh31_0-1682582860777.png

Script:

 

 

 

function onLoad() {
    //Type appropriate comment here, and begin script below
    var reqContact = localStorage.getItem("lastname");
    if (reqContact)
        g_form.setValue('requestor', reqContact);
    localStorage.clear();
}

 

 

 

After saving and testing, found it does not work as expected. Expectation is when user clicks on button 'Create Request' on Agent Workspace IMS record, it redirected user to a Service Catalog contains all Catalog items, then user selects any Catalog item per their requirement to create a request under that respective IMS record and when that catalog item form opens then its variable 'requestor' (part of a variable set in several catalog items and is of reference type Variable with Default Value set as 'javascript:gs.getUserID()' i.e., Currently logged in user) should reflect the same name as of 'opened_for' of its respective Interaction Record in Agent Workspace. But this is not working as expected as shown in below screenshots.

My testing Result:

rishabh31_2-1682583340260.png

Here Interaction record Opened for is 'David Miller'

when clicked on 'Create Incident', the 'service catalog' page opens on Agent workspace (shown in below screenshot)

rishabh31_3-1682583509399.png

Then here clicked on a catalog item- 'Access Change Request', it opens form but 'Requestor' of the form is Currently logged in user NOT David Miller (screenshot below)

rishabh31_4-1682583738912.png

This is intended to be the same as Opened for of Interaction record per Onload catalog client script written (screenshot and script provided above) because this request is opening through the 'Create Request' button on the Interaction record in the Agent workspace.

 

Please help me get this accomplished with a correct script to implement and steps.

 

Thanks in advance

 

1 ACCEPTED SOLUTION

In the 1st script you only need to set the value.

You are setting it but also clearing it outright.

Of course because of the clearing, the other script will find no value.

 

You could also use sessionStorage in place of localStorage.

 

Thus the 1st script (Interaction onChange of opened_for) would be:

 

function onChange (control, oldValue, newValue, isLoading, isTemplate) {
	sessionStorage.setItem('openedFor', g_form.getValue('opened_for'));
}

 

The 2nd script onLoad Catalog Client Script would be:

 

function onLoad () {
	var openedFor = sessionStorage.getItem('openedFor');

	if (openedFor)
		g_form.setValue('requestor', openedFor);

	sessionStorage.removeItem('openedFor');
}

 

View solution in original post

19 REPLIES 19

@-O- Sir, Thank you for the explanation I created one on change client script on Interaction table, Field Name: Opened for, UI Type- Mobile/Service Portal, Isolate Script-true (below is the script); but not getting expected result as the Catalog Item 'requestor' when open through IMS record 'Create Request' button is not same as 'opened_for' of respective IMS record.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
        localStorage.setItem("lastname", g_form.getValue('opened_for'));

        var name = localStorage.getItem('lastname');

        g_form.setValue('lastname', name);

        localStorage.removeItem('lastname');

        localStorage.clear('lastname');
    }

Further I Kept other Catalog Onload Client Script on the variable set containing 'requestor' as it is (Script shown below)

function onLoad() {
    //Type appropriate comment here, and begin script below
    var reqContact = localStorage.getItem("lastname");
    if (reqContact)
        g_form.setValue('requestor', reqContact);
    localStorage.clear();
}

 

Please help me with these script modifications to get the desired result. 

 

Thanks in advance

 

In the 1st script you only need to set the value.

You are setting it but also clearing it outright.

Of course because of the clearing, the other script will find no value.

 

You could also use sessionStorage in place of localStorage.

 

Thus the 1st script (Interaction onChange of opened_for) would be:

 

function onChange (control, oldValue, newValue, isLoading, isTemplate) {
	sessionStorage.setItem('openedFor', g_form.getValue('opened_for'));
}

 

The 2nd script onLoad Catalog Client Script would be:

 

function onLoad () {
	var openedFor = sessionStorage.getItem('openedFor');

	if (openedFor)
		g_form.setValue('requestor', openedFor);

	sessionStorage.removeItem('openedFor');
}

 

Hi @-O-  Sir, It's working fine🙂, see the result below of same catalog item opened through the 'Create Request' button on Agent workspace of the respective IMS record.

rishabh31_1-1682601590517.png

rishabh31_2-1682601655429.png

Scripts screenshots (as modified by you):

1st script (Interaction onChange Client Script of opened_for) is looks like:

rishabh31_3-1682601756055.png

2nd script onLoad Catalog Client Script on variable set contains variable 'requestor' is looks like:

rishabh31_4-1682601885013.png

 

Thank you so much sir, Marking your response as helpful and Accepted as solution.

Last thing could you please share some URL where I can understand the usage, difference and applicability of localStorage, sessionsStorage etc. Also why in onchange client script (1st script) 

'if (isLoading || newValue === '') {
//         return;
//     }'
is not kept?
 
Rest thank you so much

 

I'm glad it worked out and appreciate marking the correct answer.

Removing the isLoading and newValue checks is necessary to account for the situations where the Opened for information does not change after opening the Catalog Item or to force the user to select the Opened for user if that information no longer exists on the Interaction for some reason.

Especially the latter is really a corner case, but the more corner cases taken care of, the better.

Thanks, @-O- Sir for your explanation, I am working on this and additionally could you please help me if I want to similarly auto populate the 'Opened by' field on the respective request record with the user (currently logged in) who is performing the conversion from IMS record in Agent workspace to Request through 'Create Request' button. I mean the user who is accessing the Agent workspace> Open any IMS Record> click on Create Request button>Choose and open any Cat item> cat item form opens > need to auto populate the 'Opened by' field on this cat item form with same name as of user who is doing this conversion/accessing the agent wspce.

 

I tried but lost b/w the steps and scripts.

 

thanks in advance