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

tanumoy
Tera Guru

Hi Tanumoy,



Thank You for your response. The thread was about hiding containers but not on list collector. were you trying to point to something specific.



Thank You


lorisd_avanzo
ServiceNow Employee
ServiceNow Employee

Hi Reva,



I believe you can use the oldValue and newValue of the Catalog Client Script. They seem to work fine in the Service Portal for the List Collector variables, I just made a test in order to print value of those variables in an onChange Catalog Client Script (which variable is a List Collector).



See the script:



Catalog Client Script.png





And the results in the browser console:



Service Portal page.png


So I believe you can get rid of rightBucket and then just check if the newValue contains your other value (by using the indexOf JavaScript method for String).



Note: g_form.setMandatory should work absolutely fine in the Service Portal, so whatever is the issue with your script it should not be rooted in that function.



Regards,


Loris


Hello Loris,



Thank You for your response and detailed explanation.



The newValue brings the sys id or sys ids. how do i get the display value if more than one is selected in a list collector ? This is because i have to search through the display value it is other type ?



Thank You