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

Mark Lanning
Tera Guru

Evaluator: com.glide.script.RhinoEcmaError: invalid return

   script : Line(5) column(7)

return;

 

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;
}

 

 

Evaluator: com.glide.script.RhinoEcmaError: syntax error

   script : Line(9) column(2)

},

 

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;
},

Hi Mark, 

In the script above, 

 

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

 

It should be grSerial.next() and not the gr.next(). I think it's the typo, once change and run, hopefully it should work.

 

Thanks

Aniket

Mark Lanning
Tera Guru

Aniket,

Do you have any more ideas?  Still having issues with errors.

 

Mark

if (aSerial == false)

I can suspect above line as error prone, please check the type of aSerial before comparing it with 'false' , though you are using == not === , but still I would recommend you to try this and share the outcome (also the log message):

var aSerial = new global.validateSerialorNameMobileApp().doesCompSerialExist(input.serial_number);

gs.info("type : " + typeof aSerial + "and value is " + aSerial) ;
/** if (aSerial == false) {
gs.error('serial number found - please double');
gs.addErrorMessage(gs.getMessage("Found Serial Number already in Use"));
} */

return;

 

 

second thing is , you can try this  (keeping return out of if block)

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;

 

Thanks

Aniket

Mark Lanning
Tera Guru

Is it possible to do the same thing on the Function Action on the Condition Tab?

I have found different usage for this:

1.var gr = new GlideRecord('sc_request'); if (gr.get(current. sysapproval)) answer = gr.canRead(); else answer = false;

2. new global.FSMMobileUtil().hasQuestionnaireForthisRecord(current.sys_id);

3. current.state=='3' || current.state=='4'

So why can't I figure this out 😞  I want to check if Name or Serial Number is already in the cmdb_ci_computer then either create record or send failed message.