How to build a scanner in Now Agent mobile app to scan bulk assets and create a record in table?

Aloke Das
Tera Contributor

I have a requirement to build a scanner in Now Agent app to scan bulk assets which should create a record and should not duplicate the record if its already present in the table. 

 

 

 

9 REPLIES 9

Konner Lester
Tera Expert

Did you ever figure this out? I have a similar project where I am needing to accomplish this.

@Konner Lester  & @Aloke Das,

You can accomplish this using the OOB barcode input type and a scripted action item for a mobile function. 

 

When creating an input on an input form screen using the Mobile App Builder, you may not see the barcode input type. Start by assigning the input type to a string, then open the record in the platform using the ellipses in the top left, and from there, you should be able to select the Barcode input type. You can configure the input for bulk scanning by adding the attribute MaxEntries.

 

Once you have your input(s) configured, you will need to write an Execution script. I've found the script editor in the Mobile App Builder is very buggy, so I recommend writing your script in another IDE and then pasting it into the box once you are done. Also, I've found that I have to use parm_input as the function parameter rather than just input. 

 

Then write the functionality to update or create the records looping through each of the barcode inputs when necessary. Here is an example of how you may create or update existing records based on an asset tag you are scanning.

 

(function WriteBackAction(parm_input) {
    
    // For each barcode scanned
    for (var i = 0; i < parm_input.computerAssetTagInputs.length; i++) {
    
        // Query for an existing computer based on scanned asset tag
        var computerRecord = new GlideRecord('cmdb_ci_computer');
        computerRecord.addQuery('asset_tag', parm_input.computerAssetTagInputs[i]);
        computerRecord.query();

        // If a computer is found...
        if (computerRecord.next()){
            //Update a field
            computerRecord.fieldToUpdate = parm_input.fieldToUpdate;
            computerRecord.update();

        // If no computer is found...
        }else{
            // Create new computer record
            computerRecord.field1 = value1;
            computerRecord.field2 = value2;
            //etc...
            
            // Insert new record
            computerRecord.insert();
        }
    }

    })(parm_input);

 

Thank you @Benjamin A for the reply. I have put your steps into action, but I am still getting the failed message that gets defined in the "Failure message" on the function. Below is your script that has been changed to fit my use case. I have also included some screenshots of my function and action item. Not sure what I am doing wrong. Any direction on what to try would be greatly appreciated. Thank you

(function WriteBackAction(parm_input) {
    
    // For each barcode scanned
    for (var i = 0; i < parm_input.serialNumber.length; i++) {
    
        // Query for an existing computer based on scanned asset tag
        var computerRecord = new GlideRecord('alm_hardware');
        computerRecord.addQuery('serial_number', parm_input.serialNumber[i]);
        computerRecord.query();

        // If a computer is found...
        if (computerRecord.next()){
            //Update a field
            computerRecord.model = parm_input.Model;
			computerRecord.stockroom = parm_input.Stockroom;
            computerRecord.update();

        // If no computer is found...
        }else{
            // Create new computer record
            computerRecord.serial_number = parm_input.serialNumber;
            computerRecord.model_category = parm_input.modelCategory;
			computerRecord.stockroom = parm_input.Stockroom;
            computerRecord.model = parm_input.Model;
            //etc...
            
            // Insert new record
            computerRecord.insert();
        }
    }

    })(parm_input);

 

Rather than using Input Parameters, try using an Input Parameter screen and adding inputs to that. Attached below is an example of what you may expect to see in the Mobile App Builder.

 

Also, does the system log show any particular error when you click submit on the mobile device?

 

The first step for troubleshooting this beyond changing the inputs would be to check that the parm_input object is actually returning your values by adding some log statements at the start of the script and then checking the system logs on submission. 

Thank you once again for your help. The error is below. To fix it I just changed parm_input to input and it worked. 

com.glide.script.RhinoEcmaError: "parm_input" is not defined.
   sys_sg_write_back_action_item.c056310087322d10b1f432ec0ebb35eb : Line(29) column(0)
     26:         }
     27:     }
     28: 
==>  29:     })(parm_input);

I have completed all I needed to do. I appreciate your help with this. Thank you and have a great day.