Script to auto populate fields based on chosen asset in Service Catalog

DT
Kilo Contributor

Dear Community Members,

I've been working on a form, where the model name and warranty end date fields are auto populated based on the chosen user and his/hers asset:
find_real_file.png This has been achieved by using reference qualifiers only, because I'm not good in scripting.
Unfortunately, this approach has been rejected by the Project Lead. It has been said that it should be done via a script.
This is the point where my problem starts, because I'm new to ServiceNow and my scripting skills are less than zero.
I've tried to find something on the forum, but it all is pure black magic to me.
Could you please give me a hand with this and suggest any examples or something I could start with, which won't make me confused?

My variable set name is: "pc_replacement"

My variables are:
- "damaged_pc" (table: "alm_hardware", field name: "display_name")

- "model_of_pc" (table: "alm_hardware", field name: "model")

- "warranty_end_date" (table: "alm_hardware", field name: "warranty_expiration")

If there is additional information needed, please let me know!
Thank you in advance!


Regards,
Dawid

1 ACCEPTED SOLUTION

Hi DT,

Hopefully this is the fix.

Clear the label field values on Model of PC field

find_real_file.png

 

Replace scripts:

 

Client script: populateAssetDetails

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	
	var glide = new GlideAjax('populateAssetInfo');
	glide.addParam('sysparm_name', 'populateAssetDetails');
    glide.addParam('sysparm_damagedpc', g_form.getValue('damaged_pc'));
	glide.getXML(updateFields);

}


function updateFields(response) {
      var answer = response.responseXML.documentElement.getAttribute("answer");
      var clearvalue; // Stays Undefined
      if (answer) {
              var returneddata = JSON.parse(answer);
		  g_form.clearOptions('model_of_pc');
		  g_form.addOption('model_of_pc', returneddata.display_name, returneddata.display_name);
		 g_form.setValue('warranty_end_date', returneddata.warranty);
      } else {
			  g_form.setValue('warranty_end_date', clearvalue);
      }

}

   

Script include: populateAssetInfo

var populateAssetInfo = Class.create();
populateAssetInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	populateAssetDetails: function(){ //function being called asynchronously from client script
		var damagedpc = this.getParameter('sysparm_damagedpc'); //store the forms damaged pc sys id value
		var assetGR = new GlideRecord('alm_hardware'); //open a search on the alm_hardware table
		assetGR.addQuery('sys_id', damagedpc); //filter for a record with sys id of the sys id from before
		assetGR.query(); //look if theres a match
		if(assetGR.next()){ //if there is a match for the sys id
			gs.log('[test] found a match');
			var json = new JSON();
			var assetDetails = { //create a new object to store information
				"model" : assetGR.model.toString(), //store the model of the asset
				"warranty" : assetGR.warranty_expiration.toString(), //store the warranty expiration date
				"display_name" : assetGR.model.display_name.toString()
			};
			gs.log('[test] model: ' + assetGR.model + ' / warranty ' + assetGR.warranty_expiration + ' / display_name: ' + assetGR.model.display_name + ' / ' + assetDetails.display_name);
			return JSON.stringify(assetDetails); //return the object (encoded)
			
		}else{
			return null; //if we didnt find a record, return empty
		}
	},
	
	loadAssetsForUser: function(){
		var impacteduser = this.getParameter('sysparm_impacteduser');
		var assetGR = new GlideRecord('alm_hardware');
		assetGR.addQuery('assigned_to', impacteduser);
		assetGR.query();
		var assetList = [];
		
		while(assetGR.next()){
			var assetObj = {
				"display_name":assetGR.display_name.toString(),
				"sys_id":assetGR.sys_id.toString()
			};
			
			assetList.push(assetObj);
			gs.log('[test] pushing assetGR.display_name: ' + assetGR.display_name);
		}
		return JSON.stringify(assetList);
	},
    type: 'populateAssetInfo'
});

and theres no need to change the other client script

 

 

View solution in original post

23 REPLIES 23

Hi,

In my scripts the impacted user is also the trigger to populate the damaged pc list.

The reference qualifier on the damaged pc field looks at the impacted user, loads their list of assets. As it did before.

What are the field types for Model/Warranty on your end? In my PDI I had "model of the damaged PC" as a "Reference". Referencing the alm_asset table.  And warranty expiration as a "Date" field.

In my script, when I change the damaged PC by selecting an asset, those two fields are then populated.

I suggest changing the client script slightly to show what values it has stored (if any) when the damaged pc field is changed, to help us debug this. An alert will popup in the browser when the field is changed and hopefully will contain some data.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

	var glide = new GlideAjax('populateAssetInfo'); //glide is a instantiated object for class populateAssetInfo (which is the Script include name)
	glide.addParam('sysparm_name', 'populateAssetDetails'); //name of the function we will call in the script include (populateAssetDetails)
    glide.addParam('sysparm_damagedpc', g_form.getValue('damaged_pc')); //pass the script include the value for damaged_pc that is set on the form
	glide.getXML(updateFields); //updateFields is the function below

}


function updateFields(response) {
      var answer = response.responseXML.documentElement.getAttribute("answer"); //response is the asynchronous response from the script include function and answer is storing it
      var clearvalue; // Stays Undefined
      if (answer) {
              var returneddata = JSON.parse(answer); //returned data is now the holding the object and its properties from the script include
alert(answer);
alert(returneddata);
		  
              g_form.setValue('model_of_pc', returneddata.model); //set form field model of pc 
			  g_form.setValue('warranty_end_date', returneddata.warranty); //set form field warranty
      } else {
               g_form.setValue('model_of_pc', clearvalue); //set to empty if there was an issue
			  g_form.setValue('warranty_end_date', clearvalue); //set to empty if there was an issue
      }

}

DT
Kilo Contributor

The project requirement is to have them set as Lookup Select Box.
I've copy/pasted your changed script, but I don't get any alert when choosing the affected asset on the form.

find_real_file.png

find_real_file.png

-***************************************************************

find_real_file.png

 

find_real_file.png

Okay, that hints that the client script may be misconfigured. 

Could you share a screenshot of the client script configuration you added?

find_real_file.png

DT
Kilo Contributor

Sure, this is what I've got:
find_real_file.png

thanks DT. Configuration looks good.

I've added more debugging statements to the scripts, please update both.

We'll be able to see what is/isnt working in better detail.

 

Script include:

var populateAssetInfo = Class.create();
populateAssetInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	populateAssetDetails: function(){ //function being called asynchronously from client script
		gs.log('[test] inside script include: populateAssetInfo -> populateAssetDetails()');
		var damagedpc = this.getParameter('sysparm_damagedpc'); //store the forms damaged pc sys id value
		var assetGR = new GlideRecord('alm_hardware'); //open a search on the alm_hardware table
		assetGR.addQuery('sys_id', damagedpc); //filter for a record with sys id of the sys id from before
		assetGR.query(); //look if theres a match
		if(assetGR.next()){ //if there is a match for the sys id
			var json = new JSON();
			var assetDetails = { //create a new object to store information
				"model" : assetGR.model.toString(), //store the model of the asset
				"warranty" : assetGR.warranty_expiration.toString() //store the warranty expiration date
				};
			gs.log('[test] model: ' + assetGR.model + ' / warranty ' + assetGR.warranty_expiration);
			return JSON.stringify(assetDetails); //return the object (encoded)
			
		}else{
			return null; //if we didnt find a record, return empty
		}
	},
    type: 'populateAssetInfo'
});

 

Client script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	alert('client script is working');
	var glide = new GlideAjax('populateAssetInfo');
	glide.addParam('sysparm_name', 'populateAssetDetails');
    glide.addParam('sysparm_damagedpc', g_form.getValue('damaged_pc'));
	glide.getXML(updateFields);

}


function updateFields(response) {
      var answer = response.responseXML.documentElement.getAttribute("answer");
      var clearvalue; // Stays Undefined
      if (answer) {
              var returneddata = JSON.parse(answer);
		  
              g_form.setValue('model_of_pc', returneddata.model);
			  g_form.setValue('warranty_end_date', returneddata.warranty);
      } else {
               g_form.setValue('model_of_pc', clearvalue);
			  g_form.setValue('warranty_end_date', clearvalue);
      }

}

   

 

First thing you should check

- When damaged PC asset is selected, you should see an Alert saying "client script is working"

- Depending on whether or not its being triggered, we will see what's happening in the script include.

Go to System logs -> All and add filter: Message contains [test] and see what the results are.

 

Let me know if you see the first alert with the client script or not