The CreatorCon Call for Content is officially open! Get started here.

Mobile Agent

ShriramS
Tera Contributor

Hi,
I am currently working on Mobile agent story. The requirement is to prefill the stockroom field with the value the logged-in user selects on the first login. Currently, the stockroom field value disappears and the user has to select everytime. 
So, the scenario would be,

  1. As part of the Termination use-case, i need to have the storeroom saved as a personal preference always to a particular value selected once.
  2. For the first login experience this can be empty
  3. Post the first login the storeroom is to be saved with the value selected.

(function WriteBackAction(parm_input, parm_variable) {

    var asset_tag = [];
    var reclam_asset = [];
    var reclam_asset_count = 0;
    var processed_assets = [];
    for (var i = 0; i < parm_input.asset_tag.length; i++) {
        asset_tag.push(String(parm_input.asset_tag[i]));
    }
    var almhw = new GlideRecord('alm_hardware');
    almhw.addQuery('install_status', 'IN', '1,5');
    almhw.addQuery('serial_number', 'IN', asset_tag.toString()).addOrCondition('asset_tag', 'IN', asset_tag.toString());
    almhw.query();
    while (almhw.next()) {
        //Update Asset Install Status
        reclam_asset_count++;
        var warrantyEndDate = almhw.warranty_expiration ? new GlideDateTime(almhw.getValue('warranty_expiration')) : null;
        var employeeLastDate = null;
        if (almhw.assigned_to) {
            var employeeProfile = new GlideRecord('sn_employee_profile');
            employeeProfile.addQuery('user', almhw.getValue('assigned_to'));
            employeeProfile.query();

            if (employeeProfile.next()) {
                if (employeeProfile.getValue('employment_end_date')) {
                    employeeLastDate = new GlideDateTime(employeeProfile.getValue('employment_end_date'));
                }
            }
        }
        almhw.install_status = '6'; // Setting to "In Stock"
        almhw.stockroom = parm_input.stock_room;

        // If we have both dates to compare
        if (warrantyEndDate && employeeLastDate) {
            if (warrantyEndDate.compareTo(employeeLastDate) <= 0) {
                // Warranty ends before employee's last date - set to "Pending Disposal"
                almhw.substatus = 'pending_disposal';
            } else {
                almhw.substatus = 'available';
            }
        } else {
            almhw.substatus = 'available';
        }
        //almhw.install_status = '18';
        almhw.update();

        processed_assets.push({
            serial_number: almhw.getValue('serial_number'),
            asset_tag: almhw.getValue('asset_tag')
        });
    }

   
      // --- SAVE STOCKROOM PREFERENCE AFTER SUBMISSION ---
    if (parm_input.stock_room) {
        var pref = new GlideRecord('sys_user_preference');
        pref.addQuery('user', gs.getUserID());
        pref.addQuery('name', 'stockroom');
        pref.query();
        if (pref.next()) {
            pref.value = parm_input.stock_room;
            pref.update();
        } else {
            pref.initialize();
            pref.user  = gs.getUserID();
            pref.name  = 'stockroom';
            pref.value = parm_input.stock_room;
            pref.insert();
        }
    }

    //Close Related Catalog Tasks
    for (var j = 0; j < processed_assets.length; j++) {
        var asset = processed_assets[j];

        var taskData = new GlideRecord('sc_task');
        taskData.addEncodedQuery('request_item.cat_item=' + gs.getProperty('enterprise.asset.reclamation.form.sys.id') +
            '^short_descriptionLIKE' + asset.serial_number +
            '^stateIN-5,1,2,18^active=true');
        taskData.query();

        while (taskData.next()) {
            reclam_asset.push(asset.serial_number.toString());
            gs.log("Sankavi reclamasset" + JSON.stringify(reclam_asset));
            gs.log("Mobile App builder - Processing task for asset: " + asset.serial_number + ", stockroom: " + parm_input.stock_room);
            taskData.setValue('state', '3'); // Closed Complete
            taskData.comments = "The mentioned Asset [ Serial No: " + asset.serial_number + "] Reclamation is done.)";
            taskData.variables.stockroom = parm_input.stock_room;
            taskData.update();
        }
    }
    var tempAsset = [];
    for(var a=0;a<asset_tag.length;a++){
         if(reclam_asset.indexOf(asset_tag[a]) === -1){
        tempAsset.push(asset_tag[a]);
         }
    }
    gs.log("Sankavi tempasset" + tempAsset);
    gs.log("Sankavi" + JSON.stringify(tempAsset));
    //Confirmation Message Details
    var inputJSON = [];
    inputJSON.push(reclam_asset_count + '');
    inputJSON.push(asset_tag.length + '');
    var message = gs.getMessage("{0} out of {1} scanned assets have been reclamation.", inputJSON);
    var errorMessage = gs.getMessage("No termination tasks found for Serial Tag:" + tempAsset);
    gs.log("Sankavi asset tag" + asset_tag);
    gs.log("Sankavi" + asset_tag.length + " reclam_asset_count" + reclam_asset_count);
    var invalidMessage = asset_tag.length - reclam_asset_count;
    gs.log("Sankavi invalidMessage" + invalidMessage);
    if(invalidMessage==1){
       gs.addErrorMessage(errorMessage);
    }
    if (invalidMessage>1) {
        gs.addInfoMessage(message);
        gs.addErrorMessage("Asset not found. Please verify the asset ID:" + tempAsset);
        gs.log("Sankavi inside if");
    }
    if(invalidMessage<=0) {
        gs.addInfoMessage(message);
        //gs.addErrorMessage(errorMessage);
    }
})(parm_input, parm_variable);

I used the below logic in the above server script to store the preferred stockroom value in the sys_user_preference table. However, I’m unable to fetch it to autofill the field with the selected value since client scripts aren’t available here. Is there a way to achieve this, or are there any alternatives?

  // --- SAVE STOCKROOM PREFERENCE AFTER SUBMISSION ---
    if (parm_input.stock_room) {
        var pref = new GlideRecord('sys_user_preference');
        pref.addQuery('user', gs.getUserID());
        pref.addQuery('name', 'stockroom');
        pref.query();
        if (pref.next()) {
            pref.value = parm_input.stock_room;
            pref.update();
        } else {
            pref.initialize();
            pref.user  = gs.getUserID();
            pref.name  = 'stockroom';
            pref.value = parm_input.stock_room;
            pref.insert();
        }
    }
1 REPLY 1

M Iftikhar
Giga Sage

@ShriramS , Can you try to 

 

  • Create a Before Display Business Rule on the table where the Stockroom field exists (e.g., alm_hardware or your form’s table).

  • Add the logic to check for the saved preference and populate it before the form renders.

(function executeRule(current, previous /*null when async*/) {
var pref = new GlideRecord('sys_user_preference');
pref.addQuery('user', gs.getUserID());
pref.addQuery('name', 'stockroom');
pref.query();
if (pref.next()) {
current.stockroom = pref.getValue('value');
}
})(current, previous);

 

This runs before the record is sent to the client (even in Mobile).

 

Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.