Hardware asset, auto populate field values.

Simran Kumari1
Tera Contributor

Hardware asset table: when we select an RITM id from the field 'request line' in Alm hardware asset table, then auto populate the state field as In Use, and assigned to field value same as requested_for of the selected RITM.

 

can anybody help me with this if I can achieve it using client script? and if I can then how?

3 REPLIES 3

Anil Lande
Kilo Patron

Hi,

I would suggest few links and try the Client Script and GlideAjax to get this done.

https://www.servicenow.com/community/developer-forum/get-manager-name-based-an-selected-user/m-p/145...

https://www.servicenow.com/community/developer-articles/client-side-scripting-go-for-glideajax-with-...

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

newhand
Mega Sage

@Simran Kumari1 

answer from chatGPT。。。😀

 

 

  1. Create a script include and define a function that will perform the query, for example:

 

 

var MyScriptInclude = Class.create();

MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getHardwareAsset: function(rmNumber) {
    var assetGr = new GlideRecord('alm_hardware');
    assetGr.addQuery('request_line', rmNumber);
    assetGr.query();
    
    if (assetGr.next()) {
      return {
        state: 'In Use',
        assigned_to: assetGr.getValue('requested_for')
      };
    }
    
    return {};
  }
});

 

  1. Save the script include and go to the form where you want to perform the query.
  2. Create a client-side script that calls the script include function using GlideAjax. For example:

 

function setHardwareAssetFields() {
  var rmNumber = g_form.getValue('request_line');
  
  var ga = new GlideAjax('MyScriptInclude');
  ga.addParam('sysparm_name', 'getHardwareAsset');
  ga.addParam('rm_number', rmNumber);
  ga.getXML(onSuccess, onFailure);

  function onSuccess(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');
    
    if (answer) {
      g_form.setValue('state', answer.state);
      g_form.setValue('assigned_to', answer.assigned_to);
    }
  }

  function onFailure(response) {
    alert('Error: ' + response);
  }
}

 

  1. Add a client script to the form that calls the setHardwareAssetFields() function when the request_line field is changed.

Note that the getHardwareAsset() function returns an object with the state and assigned_to values, which are then set on the form using g_form.setValue(). Also, the rm_number parameter is passed to the script include function using ga.addParam().

 

 

Please mark my answer as correct and helpful based on Impact.

can u tell me if i can configure this using onChange?