add consumable to user

Shiva Kumar8
Kilo Guru

Hi community,

I'm trying to add consumables to user from user record, for this I create a UI page an calling it using UI action on User record(please find the attached). All I want to achieve is  when I select the user and select the stockroom and add a consumable and fill the quantity, the consumable to get assigned to the user and the count quantity should reduce in the respective stockroom. I have the HTML and Client script ready I only need the processing script so that my task will be completed, 

below is html:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<g:ui_form>
		<html>
			<body onload="setDefaultValue();">
                
                <g:evaluate var="jvar_sysId" expression="RP.getWindowProperties().get('sysId')" />
                <input type="hidden" name="userSysId" id="userSysId" value="${jvar_sysId}"/>
                
                <g:evaluate var="jvar_name" expression="RP.getWindowProperties().get('username')" />
                <input type="hidden" name="userFullName" id="userFullName" value="${jvar_name}"/>
						  
				<input type="hidden" name="hold_or_submit" id="hold_or_submit" value="" />
				<input type="hidden" name="start_error" id="start_error" value="false" />
				<input type="hidden" name="end_error" id="end_error" value="false" />		
				<style>
    select {
        width: 270px;
        margin: 5px;
	    height: 30px;
    }
</style>
				<table style="border-spacing:0 5px">
					<tr> 
				<g:evaluate jelly="true"  object="true">
							var gr = new GlideRecord("sys_user");
                            gr.addQuery('sys_id' current_sys_id);	
							gr.query();
							gr.next();
							gr;
               </g:evaluate>
						<td>User name:</td>
					<j:while test="${jvar_gr.next()}">
                            <td><g:ui_reference name="user" id="user" completer="AJAXTableCompleter" /></td>
						</j:while>
					</tr>
					<tr>
						<td>Stockroom:</td>
						<td> <g:ui_reference name="stockroom" id="stockroom" table="alm_stockroom" completer="AJAXTableCompleter" onchange="fetchModels()"/></td>
					</tr>
					<tr>
						<td>Model:</td>
						<td>
							<g:ui_reference name="model" id="model" table="alm_consumable"/></td>
					</tr>
					<tr>
						<td>Model category:</td>
						<td> <g:ui_reference name="model_category" id="modelCategory" table="cmdb_model_category" query="sys_created_by!=system^name=consumable"/></td>
					</tr>
					<tr>
						<g:evaluate jelly="true" object="true">
						var rec = new GlideRecord('sys_choice');
						rec.addQuery('table', 'alm_consumable');
						rec.addQuery('element', 'u_catalog_category');
						rec.query();
						rec;
						</g:evaluate>
						<td>Catalog category: </td>
							<td> <select name="catalog_category" id="catalogCategory">
							<option value="">-- Select --</option>
								<j:while test="${rec.next()}">
							<option value="${rec.value}">${rec.label}</option>
							</j:while>
								</select></td>
					</tr>
					<tr>
						<td>Installed date:</td>
						<td> <g:ui_date_time name="installDate" id="installDate" table='alm_comsumable' field='install_date'/></td>
					</tr>
					<tr>
						<td>Quantity:</td>
						<td> <input type="text" id="quantity" name="Consumablequantity"
									table="alm_consumable" /></td>
						<!--<g:ui_date_time name="Quantity" id="Quantity" table='alm_comsumable' displayValue="$[quantity]"/>-->
					</tr>													</table>

				<br/>
			<tr>
				<td><g:dialog_button id="cancel" title="${gs.getMessage('Cancel')}" type="${jvar_cancel_type}"
										 style_class="btn btn-default" onclick="return cancelConvert();" style="min-width: 5em;" >${gs.getMessage('Cancel')}</g:dialog_button>

				<g:dialog_button id="submit_for_review" title="${gs.getMessage('submitting')}"
										 type="${jvar_cancel_type}" style_class="btn btn-primary" onclick="return cancelSubmit();"
									 style="min-width: 5em;">&#160;${gs.getMessage('Submit')}</g:dialog_button></td>
					</tr>
			</body>
		</html>
	</g:ui_form>
</j:jelly>

Client script:

function setDefaultValue(){
	gel('user').value = gel('userSysId').value;
	gel('sys_display.user').value = gel('userFullName').value;
	gel('userLINKreplace').style.display = "";
}

function fetchModels(){
  var stockroomValue = gel('stockroom').value;
  var models = gel('lookup.model');
  var modelArray = [];
  var gr = new GlideRecord("alm_consumable");
  gr.addQuery("stockroom", stockroomValue);
  gr.query();
  while (gr.next()) {
  modelArray.push(gr.sys_id.toString());
  }
  models.setAttribute('onclick',"mousePositionSave(event); reflistOpen( 'model', 'not', 'alm_consumable', '', 'false','QUERY:active=true','sys_idIN" + modelArray+ "', '')");
}

function cancelSubmit(){
var c = gel('hold_or_submit');
	c.value = "submit";

	var startError = gel('start_error');
	var endError = gel('end_error');

	if(startError.value == "false" && endError.value == "false")
	{			 
		var submit_for_review = document.getElementById("submit_for_review");
		submit_for_review.style.display = "none";		   
		return true;
	}
	else return false;	
}

function cancelConvert() {
	var c = gel('hold_or_submit');
	c.value = "cancel";
	return true;
}

1 ACCEPTED SOLUTION

Daniel Slocum
ServiceNow Employee
ServiceNow Employee

Hi Shiva,

The standard process for Consumable "consumption" is to navigate to the consumable record, then click the "Consume" button. There you can assign it to a user.

find_real_file.png

 

 

If you are trying to solve for a unique use case, you may be able to leverage the code behind the "Consume" button type UI action as a template to get your processing code started.   However, if you can do so, it is best to stick to the out of box functionality than to build in new features.

View solution in original post

1 REPLY 1

Daniel Slocum
ServiceNow Employee
ServiceNow Employee

Hi Shiva,

The standard process for Consumable "consumption" is to navigate to the consumable record, then click the "Consume" button. There you can assign it to a user.

find_real_file.png

 

 

If you are trying to solve for a unique use case, you may be able to leverage the code behind the "Consume" button type UI action as a template to get your processing code started.   However, if you can do so, it is best to stick to the out of box functionality than to build in new features.