I have a reference field named asset on screqitem table and refer to hardware table and…
- User
I have a reference field named asset on sc_req_item table and refer to hardware table and we are using advance reference qualifier to show the references, my requirement is that , this u_asset field is on sc_task table also through dot walk, requested_item.u_asset name.. and if any user who have access to sc_task table wants to change the value in this field it is showing blank in popup. I want to show the same references that showing in ritm form for the sc_task users also. How to do it in servicenow, please provide complete steps
- Assistant
To achieve this in ServiceNow, you need to make sure that the reference qualifier used on the
sc_req_itemtable is also applied to the dot-walkedu_assetfield on thesc_tasktable. Here's how you can set it up:Step-by-Step Instructions:
-
Navigate to the
sc_taskTable:- Go to the Application Navigator and type
Tables. - Search for the
sc_tasktable and open it.
- Go to the Application Navigator and type
-
Add or Find u_asset Field:
- Find the
u_assetfield on thesc_tasktable. If it doesn't exist, create it as a new Reference field.
- Find the
-
Dot-Walk Field Configuration:
- If the
u_assetfield is added automatically via dot-walking. Ensure it references the same table as theu_assetfield on thesc_req_itemtable.
- If the
-
Set Reference Qualifier:
- You need to set the Advanced Reference Qualifier on the
sc_task.u_assetfield to use the same logic you used on thesc_req_itemtable.
- You need to set the Advanced Reference Qualifier on the
Steps to Set the Advanced Reference Qualifier:
-
Open the Dictionary Entry for sc_task.u_asset:
- Navigate to System Definition > Dictionary.
- Search for and open the dictionary entry for
sc_taskand fieldu_asset.
-
Set the Reference Qualifier:
- In the dictionary entry form, scroll down to the
Reference Specificationsection. - Ensure that the
Referencefield is set correctly to the hardware table. - Set the
Reference qualifier conditionto Advanced. - Below the condition field, you will see
Advancedcheckbox. Check it and add the script similar to what you have for thesc_req_itemtable.
- In the dictionary entry form, scroll down to the
Example Advanced Reference Qualifier Script:
answer = 'sys_class_name=cmdb_ci_computer';-
Save the Dictionary Entry:
- Save or update the dictionary entry.
-
Testing:
- Go to an
sc_taskrecord. - Try to change the value in the
u_assetfield and verify the reference qualifiers work the same as in thesc_req_itemform.
- Go to an
By setting the same advanced reference qualifier on the dot-walked
u_assetfield in thesc_tasktable, you ensure that users who have access to thesc_tasktable can see the same references as those available in thesc_req_itemform.Note:
If the reference qualifier involves complex logic or involves querying other tables, ensure that it is crafted correctly in the script to handle all necessary conditions.
That's it! These steps should help you apply the same references to the dot-walked field on the
sc_tasktable effectively. -
- User
But we do not have any field in sc_task. It is showing the ritm table asset field. Open dictionary will also showing requested_item field to change
- Assistant
If the
u_assetfield is being shown on thesc_tasktable as a dot-walked field fromsc_req_item, you don’t need to modify thesc_req_itemtable directly. Instead, you can leverage the reference qualifier of theu_assetfield from thesc_req_itemtable to apply the same reference qualifier logic to the field displayed on thesc_taskform.Here’s what you can do to ensure the correct references are shown on the
sc_taskform:-
Determine the Reference Qualifier Logic on
sc_req_item.u_asset:- Go to the dictionary entry for the
u_assetfield on thesc_req_itemtable to check the existing reference qualifier.
- Go to the dictionary entry for the
-
Apply a Similar Logic in a Client Script for
sc_task:- Create a client script to apply the same reference qualifier logic for the dot-walked field on the
sc_taskform.
- Create a client script to apply the same reference qualifier logic for the dot-walked field on the
Step-by-Step Instructions:
-
Check the Reference Qualifier Logic on
sc_req_item.u_asset:- Navigate to System Definition > Dictionary.
- Search for and open the dictionary entry for the field
u_asseton thesc_req_itemtable. - Note down the advanced reference qualifier script used.
-
Create a Client Script on the
sc_taskTable:- Navigate to System Definition > Client Scripts.
- Click on New to create a new client script.
-
Configure the Client Script:
- Name: Provide a descriptive name (e.g., "Set Reference Qualifier for u_asset in sc_task").
- Table: Select
sc_task. - Type: Select
onLoad. - Condition: Leave it blank or set conditions as required.
-
Client Script Logic:
- Write a script to set the reference qualifier for the dot-walked field. Here’s an example script:
(function() { // Ensure the form field exists before setting the reference qualifier if (g_form.getControl('requested_item.u_asset')) { // Set the reference qualifier for the dot-walked field g_form.setReferenceQualifier('requested_item.u_asset', 'sys_class_name=cmdb_ci_computer'); } })();Replace the
sys_class_name=cmdb_ci_computerpart with your actual reference qualifier logic.-
Save the Client Script:
- Save or update the client script.
-
Test your changes:
- Open a
sc_taskrecord. - Check the
requested_item.u_assetfield. - Attempt to change the value and ensure the correct references are shown as per the reference qualifier logic.
- Open a
By following these steps, you ensure that the same reference qualifier logic applied to the
u_assetfield in thesc_req_itemform is replicated for the displayed dot-walked field in thesc_taskform, thus showing the correct references. -
- User
javascript: if(current.variables.asset_type == 'eus')( ifcurrent.variables.consume_from_local_stock=='Yes')f new HamProGlobalClientCallableAssetRefresh(.updateAssetConsume(current.variables.collection_location.toString0. current.variables.sub_asset_type.getDisplayValue().toString0); else new HamProGlobalClientCallableAssetRefresh().updateAssetTransfer (current.u_stockroom.toString(, current.variables.sub_asset_type.getDisplayValue(); if(currentvariables.asset_type =="network') if(currentvariables.consume_from_local stock 'Yes') t if(currentvariables.location_type== 'bl') new HamProGlobalClientCallableAssetRefresh0).modelConsume(current.variables.branch_location.toString0. current.variables.model.toString0); if(fcurrentvariables.location_type=='dc') new HamProGiobalClientCallableAssetRefresh0.modeiConsume(current.variables.dc_location.toString(), I have this reference qualifier shown , how to modify it to show same on sc_task
- Assistant
To achieve a similar reference qualifier behavior for the
sc_tasktable, where the existing reference qualifier for thesc_req_itemtable involves a complex script, you can utilize a client script in ServiceNow to dynamically set the reference qualifier for the dot-walked field.Since your reference qualifier includes complex JavaScript logic, you'll need to encapsulate this logic within a client script that will run when the
sc_taskform loads.Here’s a step-by-step approach to creating a client script that applies the same reference qualifier logic to the
requested_item.u_assetfield on thesc_taskform:-
Navigate to Client Scripts:
- Open ServiceNow and navigate to
System Definition > Client Scripts.
- Open ServiceNow and navigate to
-
Create a New Client Script:
- Click on
Newto create a new client script.
- Click on
-
Fill in the Client Script Form:
- Name: Provide a descriptive name, such as
Set Reference Qualifier for u_asset in sc_task. - Table: Select
sc_task. - Type: Select
onLoad. - Condition: Leave it blank or set conditions as needed.
- Name: Provide a descriptive name, such as
-
Client Script Logic:
- Insert the logic from your reference qualifier into the client script.
Here’s a modified version of your reference qualifier logic for use in a
sc_taskclient script:(function() { function setAssetReferenceQualifier() { // Ensure the form field exists before setting the reference qualifier if (g_form.getControl('requested_item.u_asset')) { // Define the reference qualifier function with the custom logic var refQualifier = function() { var answer = ''; if (current.variables.asset_type == 'eus') { if (current.variables.consume_from_local_stock === 'Yes') { new HamProGlobalClientCallableAssetRefresh().updateAssetConsume(current.variables.collection_location.toString(), current.variables.sub_asset_type.getDisplayValue().toString()); } else { new HamProGlobalClientCallableAssetRefresh().updateAssetTransfer(current.u_stockroom.toString(), current.variables.sub_asset_type.getDisplayValue()); } } if (current.variables.asset_type === 'network') { if (current.variables.consume_from_local_stock === 'Yes') { if (current.variables.location_type === 'bl') { new HamProGlobalClientCallableAssetRefresh().modelConsume(current.variables.branch_location.toString(), current.variables.model.toString()); } if (current.variables.location_type === 'dc') { new HamProGlobalClientCallableAssetRefresh().modelConsume(current.variables.dc_location.toString(), current.variables.model.toString()); } } } return answer; }; // Set the reference qualifier for the dot-walked field g_form.setReferenceQualifier('requested_item.u_asset', refQualifier); } } // Call the function to set the reference qualifier setAssetReferenceQualifier(); })();- Save the Client Script:
- Save or update the client script.
Testing Your Configuration:
-
Open a
sc_taskrecord:- Navigate to a
sc_taskrecord and open it.
- Navigate to a
-
Verify the
requested_item.u_assetField:- Check the
requested_item.u_assetfield and attempt to change its value. - Ensure that the reference qualifier logic is applied correctly and the appropriate asset references are shown.
- Check the
Explanation:
- setAssetReferenceQualifier Function: A function that encapsulates your reference qualifier logic.
- refQualifier Function: A function within
setAssetReferenceQualifierthat returns the appropriate reference qualifier. - g_form.setReferenceQualifier: Sets the reference qualifier for the
requested_item.u_assetfield.
Important Notes:
- Adjust the script as necessary to ensure compatibility with your specific implementation.
- Ensure that all used fields and variables (like
current.variables.asset_type) are properly scoped and available in the client script context. - The client script runs on form load, ensuring the reference qualifier is updated dynamically.
By following these steps, you'll ensure that users with access to the
sc_tasktable see the same references as configured in thesc_req_itemtable through the complex reference qualifier logic. -
- User
It shows function onload error
- Assistant
If you're encountering an
onLoaderror for the function, it indicates that there might be an issue with how the client script is structured or how the reference qualifier function is defined. To address this, we'll carefully handle the form context and make sure that the script logic is correctly formatted for anonLoadclient script.Here’s a revised approach:
-
Remove references to
current:- Note that in client scripts, you cannot access server-side variables like
current. You'll need to access form fields usingg_form.
- Note that in client scripts, you cannot access server-side variables like
-
Define the logic within a function:
- Encapsulate the reference qualifier logic as a function and then set it as the reference qualifier using
g_form.
- Encapsulate the reference qualifier logic as a function and then set it as the reference qualifier using
-
Correct the variable access:
- Ensure form variables are accessed correctly using
g_form.getValueorg_form.getDisplayValue.
- Ensure form variables are accessed correctly using
Revised Client Script:
(function() { function setAssetReferenceQualifier() { // Ensure the form field exists before setting the reference qualifier if (g_form.getControl('requested_item.u_asset')) { // Define the reference qualifier function with the custom logic var refQualifier = function() { var answer = ''; var assetType = g_form.getValue('requested_item.variables.asset_type'); var consumeFromLocalStock = g_form.getValue('requested_item.variables.consume_from_local_stock'); var subAssetType = g_form.getDisplayValue('requested_item.variables.sub_asset_type'); var collectionLocation = g_form.getValue('requested_item.variables.collection_location'); var uStockroom = g_form.getValue('requested_item.u_stockroom'); var locationType = g_form.getValue('requested_item.variables.location_type'); var branchLocation = g_form.getValue('requested_item.variables.branch_location'); var model = g_form.getValue('requested_item.variables.model'); var dcLocation = g_form.getValue('requested_item.variables.dc_location'); if (assetType === 'eus') { if (consumeFromLocalStock === 'Yes') { new HamProGlobalClientCallableAssetRefresh().updateAssetConsume(collectionLocation, subAssetType); } else { new HamProGlobalClientCallableAssetRefresh().updateAssetTransfer(uStockroom, subAssetType); } } if (assetType === 'network') { if (consumeFromLocalStock === 'Yes') { if (locationType === 'bl') { new HamProGlobalClientCallableAssetRefresh().modelConsume(branchLocation, model); } else if (locationType === 'dc') { new HamProGlobalClientCallableAssetRefresh().modelConsume(dcLocation, model); } } } return answer; }; // Set the reference qualifier for the dot-walked field g_form.setReferenceQualifier('requested_item.u_asset', refQualifier); } } // Call the function to set the reference qualifier setAssetReferenceQualifier(); })();Step-by-Step Explanation:
-
Declare the Function:
- The
setAssetReferenceQualifierfunction encapsulates the logic for determining the reference qualifier.
- The
-
Form Field Checks:
- At the beginning of the function, there’s a check to ensure the
requested_item.u_assetcontrol exists in the form.
- At the beginning of the function, there’s a check to ensure the
-
Accessing Form Variables:
- Instead of using
current(which is not available on the client side), useg_form.getValueandg_form.getDisplayValueto get the necessary form values.
- Instead of using
-
Setting the Reference Qualifier:
- The
refQualifierfunction is set as the reference qualifier for therequested_item.u_assetfield usingg_form.setReferenceQualifier.
- The
Important Notes:
- Client-Side Execution: The client script runs on the client side, so ensure all variable accesses are compatible with client-side practices.
- Debugging: Use
gs.logorconsole.logto debug if the values are being correctly fetched and the functions are executing as expected in the browser console. - Form Fields Naming: Verify the field names as per your configuration (
g_form.getValue('requested_item.variables.<field_name>')).
By following this revised script and ensuring correct client-side practices, you should be able to avoid the
onLoaderror and successfully apply the intended reference qualifier logic to therequested_item.u_assetfield on thesc_taskform. -
- User
It shows function onLoad () error
- Assistant
To address the
function onLoaderror, let's simplify and ensure the client script is structured correctly. The key points to consider:- Client scripts will not have
currentcontext available; instead, useg_form. - Define and apply the reference qualifier logic correctly.
- Ensure the logic is correctly encapsulated within an
onLoadfunction.
Here's how you can achieve this in a more straightforward way:
Simplified Client Script for
sc_taskFormfunction onLoad() { // Ensure the form field exists before setting the reference qualifier if (g_form.getControl('requested_item.u_asset')) { var refQualifier = function() { var answer = ''; var assetType = g_form.getValue('requested_item.variables.asset_type'); var consumeFromLocalStock = g_form.getValue('requested_item.variables.consume_from_local_stock'); var collectionLocation = g_form.getValue('requested_item.variables.collection_location'); var subAssetType = g_form.getDisplayValue('requested_item.variables.sub_asset_type'); var uStockroom = g_form.getValue('requested_item.u_stockroom'); var locationType = g_form.getValue('requested_item.variables.location_type'); var branchLocation = g_form.getValue('requested_item.variables.branch_location'); var model = g_form.getDisplayValue('requested_item.variables.model'); var dcLocation = g_form.getValue('requested_item.variables.dc_location'); if (assetType === 'eus') { if (consumeFromLocalStock === 'Yes') { new HamProGlobalClientCallableAssetRefresh().updateAssetConsume(collectionLocation, subAssetType); } else { new HamProGlobalClientCallableAssetRefresh().updateAssetTransfer(uStockroom, subAssetType); } } if (assetType === 'network') { if (consumeFromLocalStock === 'Yes') { if (locationType === 'bl') { new HamProGlobalClientCallableAssetRefresh().modelConsume(branchLocation, model); } else if (locationType === 'dc') { new HamProGlobalClientCallableAssetRefresh().modelConsume(dcLocation, model); } } } return answer; }; // Apply the reference qualifier to the dot-walked field g_form.setReferenceQualifier('requested_item.u_asset', refQualifier); } }Step-by-Step Explanation:
-
Define the
onLoadFunction:- Encapsulate the logic inside the
onLoadfunction, which will trigger when the form loads.
- Encapsulate the logic inside the
-
Field Existence Check:
- Ensure the
requested_item.u_assetcontrol exists on the form.
- Ensure the
-
Define the Reference Qualifier Function:
- The
refQualifierfunction contains your custom logic to filter the assets.
- The
-
Set the Reference Qualifier:
- Use
g_form.setReferenceQualifierto apply the qualifier logic to therequested_item.u_assetfield.
- Use
Applying the Client Script:
-
Create the Client Script:
- Navigate to
System Definition > Client Scripts. - Click
Newto create a new client script.
- Navigate to
-
Configure the Client Script:
- Name:
Set Reference Qualifier for u_asset in sc_task. - Table:
sc_task. - Type:
onLoad.
- Name:
-
Insert the Script:
- Copy and paste the above
onLoadfunction script into the Script field.
- Copy and paste the above
-
Save the Client Script:
- Save or update the client script.
Verification:
-
Open a
sc_taskRecord:- Open a record in the
sc_tasktable and check therequested_item.u_assetfield.
- Open a record in the
-
Change the Value:
- Attempt to change the value in the
requested_item.u_assetfield and verify the correct references are displayed as per the logic.
- Attempt to change the value in the
Debugging Tips:
-
Use
gs.logfor Server-Side:- For client-side debugging, consider using
console.logto log values in the browser's console. - Example:
console.log('Asset Type:', g_form.getValue('requested_item.variables.asset_type'));
- For client-side debugging, consider using
-
Browser Console:
- Open the browser's developer tools (usually F12 or right-click and select "Inspect") and navigate to the "Console" tab to see log outputs.
By ensuring the function is correctly encapsulated within an
onLoadevent and usingg_formfor client-side operations, you can avoid thefunction onLoaderror and achieve the desired reference qualifier behavior on thesc_taskform. - Client scripts will not have
- User
Not working, current we cant able to use
- Assistant
I understand the challenge. Since client scripts do not have access to variables in the
currentobject, we'll need to refactor the script to work properly in the client-side context.Let's approach this problem methodically:
- Variables Loading: Ensure the fields from the
requested_itemare accessible on thesc_taskform. - Dynamic Reference Qualifier: Apply an appropriate reference qualifier for the dot-walked
u_assetfield. - Client Script: Properly encapsulate the logic within the client script without relying on server-side
currentobject.
Here’s a revised client script that simplifies the application of the reference qualifier:
Simplified Approach:
-
Ensure All Necessary Fields are Available on Form:
- Make sure fields from
requested_itemare correctly loaded and accessible in thesc_taskform. This might involve using catalog item variables or adding the relevant fields via a form layout configuration if they are not visible.
- Make sure fields from
-
Client-side Script:
- Correctly use the
g_formobject to fetch values and set the reference qualifier.
- Correctly use the
Revised Client Script:
Let's create a client script for the
sc_tasktable:function onLoad() { // Check if the requested_item.u_asset control is available on the form if (g_form.getControl('requested_item.u_asset')) { // Define the reference qualifier function with custom logic var refQualifier = function() { var answer = ''; var assetType = g_form.getValue('requested_item.variables.asset_type'); var consumeFromLocalStock = g_form.getValue('requested_item.variables.consume_from_local_stock'); var collectionLocation = g_form.getValue('requested_item.variables.collection_location'); var subAssetType = g_form.getDisplayValue('requested_item.variables.sub_asset_type'); var uStockroom = g_form.getValue('requested_item.u_stockroom'); var locationType = g_form.getValue('requested_item.variables.location_type'); var branchLocation = g_form.getValue('requested_item.variables.branch_location'); var model = g_form.getDisplayValue('requested_item.variables.model'); var dcLocation = g_form.getValue('requested_item.variables.dc_location'); // Adjusted logic as per fields data if (assetType === 'eus') { if (consumeFromLocalStock === 'Yes') { answer = 'sub_asset_type=' + subAssetType + '^collection_location=' + collectionLocation; } else { answer = 'u_stockroom=' + uStockroom + '^sub_asset_type=' + subAssetType; } } if (assetType === 'network') { if (consumeFromLocalStock === 'Yes') { if (locationType === 'bl') { answer = 'branch_location=' + branchLocation + '^model=' + model; } else if (locationType === 'dc') { answer = 'dc_location=' + dcLocation + '^model=' + model; } } } return answer; }; // Apply the reference qualifier to the dot-walked field g_form.setReferenceQualifier('requested_item.u_asset', refQualifier); } }Applying the Client Script:
-
Create the Client Script:
- Navigate to
System Definition > Client Scripts. - Click
Newto create a new client script.
- Navigate to
-
Configure the Client Script:
- Name:
Set Reference Qualifier for u_asset in sc_task. - Table:
sc_task. - Type:
onLoad.
- Name:
-
Insert the Script:
- Copy and paste the above
onLoadfunction script into the Script field.
- Copy and paste the above
-
Save the Client Script:
- Save or update the client script.
Verification:
-
Open a
sc_taskRecord:- Navigate to a record in the
sc_tasktable and open it.
- Navigate to a record in the
-
Verify the Field:
- Check the
requested_item.u_assetfield and attempt to change its value. - Ensure the correct references are shown as per the logic.
- Check the
Debugging Tips:
-
Use Browser Console:
- Use
console.logstatements within your client script to check the values and flows. - Example:
console.log('Asset Type:', g_form.getValue('requested_item.variables.asset_type'));
- Use
-
Field Names:
- Ensure the field names exactly match the names used in your environment.
- Validate if the fields are available and correctly named in the form layout.
By correctly encapsulating the logic within the
onLoadclient script and ensuring the form fields are accessible, this approach should avoid thefunction onLoad errorand apply the desired reference qualifier logic on thesc_taskform. - Variables Loading: Ensure the fields from the