Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Mobile Agent - Action Item Script

Mark Lanning
Tera Guru

I need help in creating a Action Item Script to use on the Mobile Agent App.

 Purpose of the App is for our Procurement Group to scan newly arrived hardware and update the cmdb_ci_computer table. – Single or Pallets of Hardware

 Function I need are:

  1. Save the PO Number so it will not have to be re-keyed each time, but also able to change when the PO number changes 
  2. Validate the Name and/or the Serial number has not been saved already into the cmdb_ci_computer

The fields that are captured or preset to a value are the following:

Name

PO Number  - Value Set to Previous used / or able to be Changed

Model Number

Serial Number

Support Staff – Value Set to Desktop Services

Hardware Status – Value Set to In Stock

MAC Address

 

(function WriteBackAction(input) {

// Create New Computer Record
var gr = new GlideRecord('cmdb_ci_computer');
gr.initialize();
gr.setValue('Name', input.Name);
gr.setValue('Serial number', input.serial_number);
gr.setValue('Model number', input.model_number);
gr.setValue('MAC Address', input.mac_address);
gr.setValue('PO number', input.po_number);
// gr.setValue('Support group', 'Desktop Services');
// gr.setValue('Hardware Status', 'In Stock');
if(!gs.nil( input.cmdb_ci))
gr.setValue('cmdb_ci_computer', input.cmdb_ci_computer);
gr.insert();

})(input);

9 REPLIES 9

Aniket15
ServiceNow Employee
ServiceNow Employee

Hi Mark,

 

It will be helpful to resolve your issue if you can elaborate about the problem you are facing while implementing this. Like any error you are able to see? or something not responding etc.

Thanks

Aniket

 

Mark Lanning
Tera Guru

Hello Aniket,

This is the Action Item on the Mobile Agent,

I am trying to do a call to Script Include to check for duplication, but the App is not allowing it.

 

 

(function WriteBackAction(input) {

//Check to find if Serial Number or Name is a Duplication
 var aSerial = new x_sfhs_sfh_compute.validateSerialorNameMobileApp.doesCompSerialExist(input.serial_number);
var aName = new x_sfhs_sfh_compute.validateSerialorNameMobileApp.doesCompNameExist(input.name);

 if(aSerial == true && aName == true){

// Create New Computer Record if not a Duplication
var gr = new GlideRecord('cmdb_ci_computer');
gr.initialize();
gr.setValue('name', input.name);
gr.setValue('serial_number', input.serial_number);
gr.setValue('model_number', input.model_number);
gr.setValue('mac_address', input.mac_address);
gr.setValue('po_number', input.po_number);
gr.setValue('support_group', '6b5b409e4fa6de00bf23f7e18110c760');
gr.setValue('hardware_status', 'in_stock');
gr.insert();

 }else
 {
 if(aSerial == false)
 gs.error('serial number found - please double');
 if(aName == false)
 gs.error('name found - please double');
 }

})(input);

 

the Script Include

var validateSerialorNameMobileApp = Class.create();
validateSerialorNameMobileApp.prototype = {
initialize: function() {

},
doesCompNameExist: function(name) {
var grName = new GlideRecord('cmdb_ci_computer');
grName.addQuery('name', name);
grName.query();
if (gr.next())
return false;
else
return true;
},
doesCompSerialExist: function(serial_number) {
var grSerial = new GlideRecord('cmdb_ci_computer');
grSerial.addQuery('serial_number', serial_number);
grSerial.query();
if (gr.next())
return false;
else
return true;
},

type: 'validateSerialorNameMobileApp'
};

Hi Mark,

If I am understanding it correct in the line
x_sfhs_sfh_compute.validateSerialorNameMobileApp.doesCompSerialExist(input.serial_number); 

x_sfhs_sfh_compute is the scope , and validateSerialorNameMobileApp is the your scriptInclude, if that's so , try usingx_sfhs_sfh_compute.validateSerialorNameMobileApp().doesCompSerialExist(input.serial_number); 

I think () is missing, once test this, hope it should work. 

Feel free to reply if still it troubles you. 

Please mark the answer as correct/helpful if this helps.

Thanks

Aniket

Mark Lanning
Tera Guru

Thanks for the Help, something is still off somewhere

Action Item 

var aSerial = new global.validateSerialorNameMobileApp().doesCompSerialExist(input.serial_number);
if (aSerial == false) {
gs.error('serial number found - please double');
gs.addErrorMessage(gs.getMessage("Found Serial Number already in Use"));
return;
}

 

Script Include

doesCompSerialExist: function(serial_number) {
var grSerial = new GlideRecord('cmdb_ci_computer');
grSerial.addQuery('serial_number', serial_number);
grSerial.query();
if (gr.next())
return false;
else
return true;
},