how to perform lookup when the walkup interaction form is loaded ?

chercm
Mega Sage

i am new in scripting . need some help i have set the asset field as a reference to alm_hardware table but how to populate the asset field based on the opened by user ?

 

chercm_0-1702166515459.png

 

4 REPLIES 4

Tony Chatfield1
Kilo Patron

Hi, if you have a single asset mapped to each user, you could use an onChange client script for the Opened For field.
When 'Opened For' changes\is populated, use GlideAjax to look up your asset record returning the users Asset and setting it in the 'asset' field.
Client Side API | ServiceNow Developers

 

If the user has multiple Assets in their name, then you would probably need to use a reference qualifier on the 'asset' field so that the assets available for selection are limited to those associated to your 'Opened For' user.

Reference qualifiers (servicenow.com)

@Tony Chatfield1  

 

chercm_0-1702259556617.png

 

 

is this the one that you are referring to ? 

 

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 '';

chercm
Mega Sage

@Tony Chatfield1 i used chat gpt and it gave me this. i tried doing it but i cannot amend the walkup experience in the studio. any other ways of doing it ?

 

To create a field on a ServiceNow Walk-up Interaction form that looks up user assets from the alm_hardware table based on the opened_by field, you can use a combination of reference fields, client scripts, and possibly a Script Include for server-side processing. Below are the steps to achieve this:

1. Create a Reference Field on Walk-up Interaction Table:

  1. Navigate to ServiceNow Studio > Tables.
  2. Open the Walk-up Interaction table.
  3. Create a new reference field, let's call it user_asset.
  4. Set the Reference field to reference the alm_hardware table.
  5. Configure the Reference Qualifier if necessary to filter assets based on the opened_by field.

2. Create a Client Script:

  1. Navigate to ServiceNow Studio > Scripts - Client Scripts.
  2. Create a new Client Script and name it something like WalkupInteractionClientScript.
  3. In the script, use the onLoad function to trigger the lookup when the form loads:

 

javascriptCopy code
function onLoad() { // Get the value of the 'opened_by' field var openedBy = g_form.getValue('opened_by'); // Check if the 'opened_by' field is not empty if (openedBy) { // Make an asynchronous request to the server to get user assets getUserAssets(openedBy); } } function getUserAssets(openedBy) { // Create a GlideAjax instance to make a server-side script include call var ga = new GlideAjax('YourScriptIncludeName'); // Replace with the actual name of your Script Include ga.addParam('sysparm_name', 'getUserAssets'); ga.addParam('sysparm_opened_by', openedBy); // Make the AJAX call ga.getXML(handleResponse); } function handleResponse(response) { if (response) { // Parse the JSON response var result = JSON.parse(response.responseText); // Check if the response was successful if (result && result.success) { // Set the value of the 'user_asset' field with the received assets g_form.setValue('user_asset', result.userAssets); } else { // Handle errors alert('Error fetching user assets: ' + result.error); } } }

 

 

3. Create a Server-Side Script Include:

  1. Navigate to ServiceNow Studio > Scripts - Script Includes.
  2. Create a new Script Include and name it something like YourScriptIncludeName.
  3. In the script include, create a function to process the server-side request:

 

javascriptCopy code
var YourScriptIncludeName = Class.create(); YourScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, { getUserAssets: function () { var openedBy = this.getParameter('sysparm_opened_by'); // Query the 'alm_hardware' table based on 'opened_by' field var userAssets = new GlideRecord('alm_hardware'); userAssets.addQuery('assigned_to', openedBy); userAssets.query(); // Process the results var result = []; while (userAssets.next()) { result.push({ name: userAssets.getValue('name'), // Add other fields as needed }); } // Return the result as JSON return new JSON().encode({ success: true, userAssets: result }); }, type: 'YourScriptIncludeName' });
 

4. Add Client Script to the Form Layout:

  1. Go to ServiceNow Studio > Forms.
  2. Open the Walk-up Interaction form.
  3. Add the WalkupInteractionClientScript to the form layout.

5. Test the Walk-up Interaction Form:

  1. Create or open a Walk-up Interaction record.
  2. Select a user in the opened_by field.
  3. The user_asset field should automatically populate with the assets associated with the selected user.

chercm
Mega Sage

i tried to use this script but when the walkup interaction loads, i am not getting any data in the serial_number box 

 

 

function onLoad() {
var openedForValue = g_form.getValue('opened_for'); // Replace 'opened_for' with the actual field name

if (openedForValue !== '') {
var ga = new GlideAjax('MyScriptInclude');
ga.addParam('sysparm_name', 'getAssignedTo');
ga.addParam('sysparm_opened_for', openedForValue);

ga.getXMLAnswer(function(answer) {
if (answer !== ' ') {
g_form.setValue('serial_number', answer); // Replace 'assigned_to' with the actual field name
}
});
}
}

script include: 

 

var MyScriptInclude = Class.create();

MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

    getSerialNumber: function() {

        var openedForSysId = this.getParameter('sysparm_ast'); // Assuming opened_for is a reference field to sys_user

 

        var gr = new GlideRecord('alm_hardware');

        gr.addQuery('opened_for', openedForSysId);

 

        if (gr.next()) {

            var assignedToSysId = gr.assigned_to; // Assuming assigned_to is a reference field to sys_user

            var assignedToGR = new GlideRecord('sys_user');

 

            if (assignedToGR.get(assignedToSysId)) {

                var userName = assignedToGR.user_name;

                return userName;

            }

        }

 

        return ''; // Return an appropriate value if no matching record is found or any other error

    }

});