How to pass variable name while creating Catalog Client Script using Table API?

iwsservicen
Tera Contributor

I am trying to create Catalog Client Scripts using ServiceNow Table API like given below. The Variable Name property is not getting updated even though I am passing the sys_id of the variable in "cat_variable". How can we add the Variable Name as well during the creation of Catalog Client Script?

 

 

Content-Type: application/json
Accept: application/json
Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxx
 
{
    "name": "Update Email Adress",
    "type": "onChange",
    "cat_item": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "cat_variable": "IO:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "script":
"function onChange(control, oldValue, newValue, isLoading) {\n   if (isLoading || newValue == '') {\n      return;\n   }\n   // First Field Value\n   var FirstFieldValue = g_form.getValue('mailbox_display_name');\n\n   // Second Field\n   g_form.setValue('mailbox_email_address', FirstFieldValue + '@tenantname.onmicrosoft.com');\n}"
}
3 REPLIES 3

Community Alums
Not applicable

Hi @iwsservicen

 

Passing sys_id of catalog variable is not enough, you must pass catalog variable name as well so that your catalog client script would recognize.
Here's the solution with shots I implemented on my PDI:

First I created example catalog item and created two single line text variables:

MIftikhar_0-1761043253868.png

MIftikhar_2-1761043410502.png

Then created POST method Table API all through System Web Services > REST > REST API Explorer

Configure the options as mentioned in shot below:

MIftikhar_3-1761043601549.png

Put the following JSON in Request Body's Raw section:

{
  "name":"Update Email Address",
  "type":"onChange",
  "cat_item":"d17a9710933076109d0a7c5efaba10b8", // sys_id of Cat item
  "cat_variable":"03ba9f90933076109d0a7c5efaba108f", // sys_id of Cat variable 
  "variable_name":"mailbox_display_name", // name of Cat variable
  "script":"function onChange(control, oldValue, newValue, isLoading) {\n   if (isLoading || newValue == '') {\n      return;\n   }\n   // First Field Value\n   var FirstFieldValue = g_form.getValue('mailbox_display_name');\n\n   // Second Field\n   g_form.setValue('mailbox_email_address', FirstFieldValue + '@tenantname.onmicrosoft.com');\n}"
}

Replace sys_ids and catalog variable name with your actual one. You can find the variable name here:

MIftikhar_4-1761043784930.png

Now click on the Send button in REST API Explorer.

MIftikhar_5-1761043866048.png

Now you should be able to see a success message and Client script created for your catalog item:

MIftikhar_6-1761043917115.png

MIftikhar_7-1761043996079.png

when you open this client script record, don't forget to set Variable name to mailbox_display_name:

MIftikhar_8-1761044068856.png

 

Now test this by clicking on Try it button on your catalog item form:

MIftikhar_10-1761044174351.png

you can see in above shot when I wrote my name in Mailbox Display Name field, Mailbox Email Adress field automatically got updated.

 

Hope this helps!

 

If my response helped, please mark it as the accepted solution and helpful so others can benefit as well.

 

 

Hi @Community Alums , Attaching my input JSON and screenshots here

Hi @Community Alums , Thank you for your reply, I tried calling the Table API again with the same JSON input body as you tried , but still I am facing the same issue as before  - Variable name is not getting updated. Also wanted to add one thing, I am having this variable inside a variable set.

This is my input body and the output screenshot.

{
    "name": "Update Email Address",
    "type": "onChange",
    "cat_item": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", //sys Id of catalog item
    "cat_variable": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", //sys Id of variable
    "variable_name": "mailbox_display_name",
    "script": "function onChange(control, oldValue, newValue, isLoading) {\n   if (isLoading || newValue == '') {\n      return;\n   }\n   // First Field Value\n   var FirstFieldValue = g_form.getValue('mailbox_display_name');\n\n   // Second Field\n   g_form.setValue('mailbox_email_address', FirstFieldValue + '@tenantname.onmicrosoft.com');\n}"   
}

 

iwsservicen_1-1761204331065.png

 

 

iwsservicen_0-1761204183074.png