- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2018 12:37 AM
Hi All,
I want to auto populate "asset_tag" value from asset table(alm_hardware) for selected user "requested_for" in service request. Could you please provide me the sample code to achieve this.
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2018 04:01 AM
Hi Josmad,
The field you are trying to set is a reference field right? or it is a string field.
If it is a reference field it can hold only 1 value
if it is a string field then do you want to populate all the asset tags with comma separated in that field.
If yes then update script as below
getDetails: function(){
var arr = [];
var sysId =this.getParameter('sysparm_sysId');
var gr = new GlideRecord('alm_hardware');
gr.addQuery('assigned_to', sysId);
gr.query();
while(gr.next()){
arr.push(gr.asset_tag.toString());
}
if(arr.length > 0)
return arr.toString();
else
return '';
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 08:38 PM
@Dr Atul G- LNG i tried to use this but it is not populating
// Client Script on the incident table
function onLoad() {
// Check if the 'opened_by' field has a value
var openedBy = g_form.getValue('opened_by');
// If 'opened_by' has a value, perform further actions
if (openedBy) {
// Query the alm_hardware table to find the user asset (replace 'alm_hardware' with your actual table name)
var hardware = new GlideRecord('alm_hardware');
hardware.addQuery('user', openedBy);
hardware.query();
// If a user asset is found, set the value of the 'userasset' field on the form
if (hardware.next()) {
g_form.setValue('userasset', hardware.userasset);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 10:01 PM
Kindly check this link i have similar requirement and i achieved it with the help of scripting. If user has more than 2 asset then i have populate the serial number with comma , seperated.
regards
shubham
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2023 03:15 PM
@shubhamdubey thanks for the solution, i tried to convert it to onload for the walkup experience
function onLoad() {
g_form.addLoadCompleteCallback(function () {
var assetField = g_form.getControl('your_asset_field'); // Replace 'your_asset_field' with the actual name of your asset field
var serialNumberField = g_form.getControl('serial_number'); // Replace 'serial_number' with the actual name of your serial number field
if (assetField && serialNumberField) {
var assetValue = assetField.value;
if (assetValue) {
g_form.clearValue('serial_number'); // Clear the serial number field initially
var assetList = assetValue.split(',');
var temp = '';
function processAsset(index) {
if (index < assetList.length) {
var ga = new GlideAjax('MyScriptInclude');
ga.addParam('sysparm_name', 'getSerialNumber');
ga.addParam('sysparm_ast', assetList[index]);
ga.getXMLAnswer(function (answer) {
if (answer !== ' ') {
temp += answer + ',';
serialNumberField.value = temp;
}
// Process the next asset
processAsset(index + 1);
});
}
}
// Start processing assets from the first index
processAsset(0);
}
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2024 11:12 PM
Hi Ankur,
If i want to fetch the user name in other custom field "user_to_be_released", then how i will fetch the value of the field using client script. Can you please help me since i am new learner of scripting.
thanks,
Aruna
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2024 11:34 PM
Can you post a new question and tag me there as this is an old thread?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader