Script include for record producer variable length

Joe Weisiger
Giga Expert

Hello,

I have multiple similiar record producers that all require a common variable 'Description' to be a minimal of 251 characters.  I also need to validate that 'Description' is not the same as 'Short Description'.

I wrote an onChange catalog client script on the description field that works for this, but I want to see if it would be smarter to create a script include with the logic for length so that it can be shared across all required forms to pass in the length so it can be dynamic as a parameter.

How would I go about creating the script include and how should I call it in the record producer?

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


    if (isLoading || newValue === '') {
          return;
    }

    //Verify that Short Description and Description are not the same

        var title = g_form.getValue('short_description');
        if(title == newValue)
        {
                  alert('Description cannot be the same as the Short Description.  Please enter additional information.');
                  g_form.clearValue('description');
        } 
	
	//Verify that Description is at least 250 characters


       var short_desc = g_form.getValue('description');
       if(short_desc.length < 251){


                  alert("Please enter a description longer than 250 characters.");

        }

}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you want to validate this on catalog/record producer form right?

Also I assume both description and short description are variables on that record producer.

Why not add those 2 variables as part of single variable set and create catalog client script which applies on variable set?

You can add this variable set to all your catalog items and the script will work on every form

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Sameer18
Tera Contributor

You can do this using UI script Checkout this article for detailed steps:

Client side utility using script UI script

Cris P
Tera Guru

Hi there,

Here is a Script Include which returns true/false based on your specs (make sure 'Client Callable' is ticked on the same):

var CPcheckDescriptionLength = Class.create();
CPcheckDescriptionLength.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	isLengthSatisified: function(){
		// We can store the value 251 in a system property, or can just set it statically here:
		var minimum = gs.getProperty('STORE_THE_VALUE_IN_PROPERTY') || 251;
		
		// Check if our short desc is less than minimum, return true/false
		return this.getParameter('sysparm_sd') < minimum;
	},

    type: 'CPcheckDescriptionLength'
});

 

Then from your client script call it like this:

function onLoad() {
   //Type appropriate comment here, and begin script below
	
	var ga = new GlideAjax('CPcheckDescriptionLength'); // The name of the Script Include
	ga.addParam('sysparm_name', 'isLengthSatisified'); // The name of the method we want to use in the SI
	ga.addParam('sysparm_sd', g_form.getValue('short_description')); // Pass your short description to the param we define in the SI
	ga.getXMLAnswer(function(response){ // Callback function to use the 'response' from our SI
		jslog(response); // Will be true or false
	});
   
}

Thanks Cris!  I am trying to use this in the onChange script that I created to compare the short description and description.  I am getting a javascript error on the portal record producer though.

Here is the onChange client script.  Also, how to I add an alert popup if the description is under 251 characters?

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


    if (isLoading || newValue === '') {
          return;
    }

    //Verify that Short Description and Description are not the same

        var title = g_form.getValue('short_description');
        if(title == newValue)
        {
                  alert('Description cannot be the same as the short description.  Please enter additional information.');
                  g_form.clearValue('description');
        } 
	
	//Verify that Description is at least 250 characters

		var ga = new GlideAjax('CPcheckDescriptionLength'); //The name of the Script Include
		ga.addParam('sysparm_name', 'isLengthSatisified'); //The name of the method we want to use in the SI
		ga.addParam('sysparm_sd', g_form.getValue('description')); //Pass your description to the param we define in the SI
		ga.getXMLAnswer(function(response){ //Callback function to use the 'response' from our SI
			jslog(response); //Will be true or false
		});
 
}

I tried this with the onLoad script and cannot get it to work.  I get a javascript error when the record producer loads