The CreatorCon Call for Content is officially open! Get started here.

How do i make this client script work on service portal

rev3
Mega Expert

Hi all,

I have an onChange catalog client script on a list collector variable. I have made UI type to both to apply on service portal but it does not work. I am aware that some functionalities do not work on Service Portal side and need to be written in a different way. But i am not sure how to change the script for a list collector. Here is my script

Type: onChange

Catalog client script

variable type: list collector

logic: Make description mandatory if request type is empty or other request type is selected. Here request type is list collector and description is string.  

script:

function onChange(control, oldValue, newValue, isLoading) {

            var rightBucket = $('request_type_select_1');

            for(i=0; rightBucket.options.length;i++) {

                      if(rightBucket.options[i].text=='Other' || newValue == '') {

                                g_form.setMandatory("description",true);           // Set description mandatory if other request type is selected

                                return;

                                }

                      else {

                                  g_form.setMandatory("description",false);   // reset description not mandatory if other is not selected

                                          }

                    }

}

Thank You

Reva

1 ACCEPTED SOLUTION

lorisd_avanzo
ServiceNow Employee
ServiceNow Employee

You would just need to use the split() JavaScript function in your Client Side code.



I have tried to modify the script accordingly, but I have not tested, it. You can use it as starting point for your development, please see the below:


function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }






  var arrSysId = newValue.split(",");




  var currentSysId="";




  for (var i=0 ; arrSysId.length > i ; i++){




      currentSysId = arrSysId[i];




      var ga = new GlideAjax('requestAjax');


      ga.addParam('sysparm_name','Description');


      ga.addParam('sysparm_type', currentSysId);


      ga.getXML(DescriptionParse);


   


      }







      function DescriptionParse(response) {


          var results = response.responseXML.getElementsByTagName("answer");


          var type1 = results[0].getAttribute("type1");


          alert("Type1: " +type1);


          if(type1) {


              g_form.setMandatory('description' , true); // if type1 has value


                      }


            }






}



Please mark Correct / Helpful / Like based on the effect of this response



Best Regards,


Loris D'Avanzo | Application Developer | ServiceNow


www.servicenow.com


View solution in original post

8 REPLIES 8

lorisd_avanzo
ServiceNow Employee
ServiceNow Employee

Hi Reva,



By using a List Collector, you will have a referenced table (defined in the List Table field of the Variable form, under the Type Specifications section).


The sys ids shown in the newValue variable are related to the sys_id in the table defined in List Table.



The easiest way to achieve the requirement would be picking the sys id value of the Other entry in the defined referenced List Table, and then use that sys id in the code of your IF condition.



Another way, would be using GlideRecord (Client Side API) in order to query the table defined in List Table for the sys_id s, then get the name (or the display value).


For this, the below links may help:


- Glide Record on developer.servicenow


- Get record on a Service Portal Widget via client script



From the second link, you can use a similar code in your client script (it should work fine, I just tried from my side). However, the first solution (pick the sys id value of the Other variable and check for it in the IF condition) would be the most recommended.



Please, if the question has been answered mark the discussion accordingly



Regards,


Loris


Hello Lori,



Unfortunately we cannot use the easiest way as the list collector references to a custom table that has more than 1 other types because of other dependent fields.



After going through your response and researching more. We decided to go for Glide ajax route which is more efficient and is the best practice. Here is my script where i can read the first value of the list collector. I am not sure how to check for all the comma seperated sys ids.



1. Client Side: onChange client script on list collector variable type.


function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }



  var ga = new GlideAjax('requestAjax');


  ga.addParam('sysparm_name','Description');


  ga.addParam('sysparm_type', newValue);


  ga.getXML(DescriptionParse);



  function DescriptionParse(response) {


  var results = response.responseXML.getElementsByTagName("answer");


  var type1 = results[0].getAttribute("type1");


  alert("Type1: " +type1);


  if(type1) {


  g_form.setMandatory('description' , true); // if type1 has value


                      }


            }


}



    2. Server Side:




var requestAjax = Class.create();


requestAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  Description: function() {


            var Type = this.getParameter('sysparm_type');


            var result;


            var gr = new GlideRecord('u_request_type');


            gr.addQuery('sys_id', Type);


            gr.query();


            if(gr.next()) {


            result = this.newItem('answer');


            result.setAttribute("type1", gr.u_name);


            gs.log('The type1 is= ' +gr.u_name);


              }


}



});


This script is pulling only the first sys_id of the list collector. Any clue how to make it work for series sys ids ?



Thank You


lorisd_avanzo
ServiceNow Employee
ServiceNow Employee

You would just need to use the split() JavaScript function in your Client Side code.



I have tried to modify the script accordingly, but I have not tested, it. You can use it as starting point for your development, please see the below:


function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }






  var arrSysId = newValue.split(",");




  var currentSysId="";




  for (var i=0 ; arrSysId.length > i ; i++){




      currentSysId = arrSysId[i];




      var ga = new GlideAjax('requestAjax');


      ga.addParam('sysparm_name','Description');


      ga.addParam('sysparm_type', currentSysId);


      ga.getXML(DescriptionParse);


   


      }







      function DescriptionParse(response) {


          var results = response.responseXML.getElementsByTagName("answer");


          var type1 = results[0].getAttribute("type1");


          alert("Type1: " +type1);


          if(type1) {


              g_form.setMandatory('description' , true); // if type1 has value


                      }


            }






}



Please mark Correct / Helpful / Like based on the effect of this response



Best Regards,


Loris D'Avanzo | Application Developer | ServiceNow


www.servicenow.com


Hi Loris,



That worked like a charm.



Thank your for being there constantly.



Thank You


Reva