Add a Prompt to the Consume a Consumable UI

Adam Wood
Tera Contributor

Hi 

 

 

We have a requirement to enter extra information when we click the consume button.

 

I would like to the add the option to the UI Page (consume_a_consumable) to enter a number and add the results to a field on the consumable

 

I have managed to add this to the UI page but have been unable to update the consumable with this number

 

AdamWood_0-1743097636486.png

 

Any help would be appreciated 

 

Thank you 

 

 

6 REPLIES 6

Kieran Anson
Kilo Patron

Hi Adam,

Have you modified the processing script at all?

If not, could you share your modified HTML highlighting what addition you've made, and also what field you're wanting to update on the consumables record and we can guide you through this change 

 

Thanks!

HTML SCRIPT ROW 36 - 39 
 
<g:ui_form>
<input type="hidden" id="cancelled" name="cancelled" value="false"/>
<input type="hidden" id="consumable" name="consumable" value="${sysparm_sys_id}"/>
<input type="hidden" id="total_qty" name="total_qty" value="${sysparm_total_qty}"/>
<input type="hidden" id="from_workspace" name="from_workspace" value="false"/>
<j2:if test="$[!empty(sysparm_domain)]">
<input type="hidden" id="sysparm_domain" name="sysparm_domain" value="${sysparm_domain}"/>
</j2:if>
<j2:if test="$[!empty(sysparm_domain_scope)]">
<input type="hidden" id="sysparm_domain_scope" name="sysparm_domain_scope" value="${sysparm_domain_scope}" />
</j2:if>
 
<table width="100%" style="border-spacing:10px; height:100px;">
<tr>
<td rowspan="5" nowrap="true" width="110px" style="vertical-align:top;">
  <img src="images/plugin_activate.gifx" alt="${gs.getMessage('Activate/Upgrade')}" style="vertical-align:top;" />
</td>
</tr>
<tr>
<td style="text-align:right;"><label>${gs.getMessage('From stockroom:')}$[SP]</label></td>
<td style="padding:10px;"><label>${sysparm_stockroom}</label></td>
</tr>
<tr >
<td style="text-align:right;"><label>${gs.getMessage('Quantity:')}$[SP]</label></td>
<td style="padding:10px;"><input class="form-control" id="qty" name="qty" value="1" label="" type="text"/></td>
</tr>
    <tr>
        <td style="text-align:right;"><label for="${jvar_asset_query}" onclick="" dir="">${gs.getMessage('Asset:')}$[SP]</label></td>
<td id="td_reference_hardware"><g:ui_reference name="alm_hardware" table="alm_hardware"/></td> 
    </tr>
    <tr>
        <td style="text-align:right;"><label for="${jvar_user_query}" onclick="" dir="">${gs.getMessage('User:')}$[SP]</label></td>
<td id="td_reference_user"><g:ui_reference name="sys_user" table="sys_user"/></td> 
    </tr>
<tr>
<td style="text-align:right;"><label>${gs.getMessage('Samanage Ticket:')}$[SP]</label></td>
<td style="padding:10px;"><input class="form-control" id="samticket" name="samticket" value="" label="" type="text"/></td>
</tr>
<script>
var tdReferenceHardware = document.getElementById('td_reference_hardware');
var tdReferenceUser = document.getElementById('td_reference_user');
var isPolarisEnabled = window.NOW.isPolarisEnabled;
if (isPolarisEnabled === 'true') {
tdReferenceUser.style.padding = '10px';
tdReferenceHardware.style.padding = '10px';
} else {
tdReferenceUser.setAttribute('style','');
tdReferenceHardware.setAttribute('style','');
}
</script>
 
    <tr id="dialogbuttons">
        <td colspan="3" align="right" style="padding-right:10px;">
            <g:dialog_buttons_ok_cancel ok="return actionOK();" cancel="cancel();"/>
        </td>
    </tr>
 
CLIENT SCRIPT Row 15
 
function cancel() {
    var c = gel('cancelled');
    c.value = "true";

    // Only destroy the window when user is on platform view
    if (!isActionFromWorkspace())
        GlideDialogWindow.get().destroy();
}

function actionOK() {
    var total_quantity = gel('total_qty').value;
    var quantity = gel('qty').value;
    var asset_id = gel('alm_hardware').value;
    var user_id = gel('sys_user').value;
    var u_samanage_ticket_number = gel('samticket').value;
    isActionFromWorkspace();
   
    quantity = quantity.replaceAll(',', '');
    var regex = /^\d+$/;
    if (!regex.test(quantity) || isNaN(quantity)) {
        alert(getMessage("Please enter a non-zero number for quantity"));
        return false;
    }

    total_quantity = parseInt(total_quantity, 10);
    quantity = parseInt(quantity, 10);

    if (quantity > total_quantity) {
        alert(getMessage("Please enter a number less than or equal to the number in stock"));
        return false;
    } else if (quantity < 1) {
        alert(getMessage("Please enter a non-zero number for the quantity"));
        return false;
    } else if (asset_id == '' && user_id == '') {
        alert(getMessage("Please select an asset or a user to attach the consumed items to"));
        return false;
    } else {
        var form = document.forms['form.' + '${sys_id}'];
        addInput(form, "HIDDEN", "asset", asset_id);
        addInput(form, "HIDDEN", "user", user_id);
        return true;
    }
}

/**
 * Determine if the action performed was from platform or on workspace
 */
function isActionFromWorkspace() {
    var fromWorkspace = getParmVal('from_workspace') === "true";
   
    // Set the hidden input field that determined whether or not the
    // submission was from platform or workspace
    var c = gel('from_workspace');

    if (fromWorkspace)
        c.value = "true";
    else
        c.value = "false";
   
    return fromWorkspace;
}

/**
 * Helper function to get the parameter values from the URL
 *
 * @Param {string} name of the url parameter to fetch
 */
function getParmVal(name) {
    var url = document.URL.parseQuery();
    if (url[name]) {
        return decodeURI(url[name]);
    }
    return;
}
 
PROCESSING SCRIPT - Row 10 & 13
 
if(cancelled == "false" && asset == "" && user != ""){
    var gr = new GlideRecord('sys_user');
    gr.get(user);
    var location = gr.getValue('location');
    var userDetails= {
    cost_center: gr.getValue('cost_center'),
    department: gr.getValue('department'),
    company: gr.getValue('company'),
    };
    new Consumables().splitForeground(consumable, qty, '10', '', asset, '', location, user, userDetails, samticket);
}
else if (cancelled == "false" && asset != "") {
    var new_sys_id = new Consumables().splitForeground(consumable, qty, '10', '', asset, '', '', user, samticket);
}

if (from_workspace !== "true")
    response.sendRedirect("alm_consumable.do?sys_id=" + consumable);

@Adam Wood 

so till which point did you reach?

were you able to send the samticket to that script include function?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I have amended the UI Page to request the value but I am unable to add the value to the field on the consumable table