Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

I have a reference field named asset on screqitem table and refer to hardware table and…

  1. 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

  2. Assistant

    To achieve this in ServiceNow, you need to make sure that the reference qualifier used on the sc_req_item table is also applied to the dot-walked u_asset field on the sc_task table. Here's how you can set it up:

    Step-by-Step Instructions:

    1. Navigate to the sc_task Table:

      • Go to the Application Navigator and type Tables.
      • Search for the sc_task table and open it.
    2. Add or Find u_asset Field:

      • Find the u_asset field on the sc_task table. If it doesn't exist, create it as a new Reference field.
    3. Dot-Walk Field Configuration:

      • If the u_asset field is added automatically via dot-walking. Ensure it references the same table as the u_asset field on the sc_req_item table.
    4. Set Reference Qualifier:

      • You need to set the Advanced Reference Qualifier on the sc_task.u_asset field to use the same logic you used on the sc_req_item table.

    Steps to Set the Advanced Reference Qualifier:

    1. Open the Dictionary Entry for sc_task.u_asset:

      • Navigate to System Definition > Dictionary.
      • Search for and open the dictionary entry for sc_task and field u_asset.
    2. Set the Reference Qualifier:

      • In the dictionary entry form, scroll down to the Reference Specification section.
      • Ensure that the Reference field is set correctly to the hardware table.
      • Set the Reference qualifier condition to Advanced.
      • Below the condition field, you will see Advanced checkbox. Check it and add the script similar to what you have for the sc_req_item table.

    Example Advanced Reference Qualifier Script:

    answer = 'sys_class_name=cmdb_ci_computer';
    
    1. Save the Dictionary Entry:

      • Save or update the dictionary entry.
    2. Testing:

      • Go to an sc_task record.
      • Try to change the value in the u_asset field and verify the reference qualifiers work the same as in the sc_req_item form.

    By setting the same advanced reference qualifier on the dot-walked u_asset field in the sc_task table, you ensure that users who have access to the sc_task table can see the same references as those available in the sc_req_item form.

    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_task table effectively.

  3. 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

  4. Assistant

    If the u_asset field is being shown on the sc_task table as a dot-walked field from sc_req_item, you don’t need to modify the sc_req_item table directly. Instead, you can leverage the reference qualifier of the u_asset field from the sc_req_item table to apply the same reference qualifier logic to the field displayed on the sc_task form.

    Here’s what you can do to ensure the correct references are shown on the sc_task form:

    1. Determine the Reference Qualifier Logic on sc_req_item.u_asset:

      • Go to the dictionary entry for the u_asset field on the sc_req_item table to check the existing reference qualifier.
    2. 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_task form.

    Step-by-Step Instructions:

    1. 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_asset on the sc_req_item table.
      • Note down the advanced reference qualifier script used.
    2. Create a Client Script on the sc_task Table:

      • Navigate to System Definition > Client Scripts.
      • Click on New to create a new client script.
    3. 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.
    4. 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_computer part with your actual reference qualifier logic.

    1. Save the Client Script:

      • Save or update the client script.
    2. Test your changes:

      • Open a sc_task record.
      • Check the requested_item.u_asset field.
      • Attempt to change the value and ensure the correct references are shown as per the reference qualifier logic.

    By following these steps, you ensure that the same reference qualifier logic applied to the u_asset field in the sc_req_item form is replicated for the displayed dot-walked field in the sc_task form, thus showing the correct references.

  5. 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

  6. Assistant

    To achieve a similar reference qualifier behavior for the sc_task table, where the existing reference qualifier for the sc_req_item table 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_task form 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_asset field on the sc_task form:

    1. Navigate to Client Scripts:

      • Open ServiceNow and navigate to System Definition > Client Scripts.
    2. Create a New Client Script:

      • Click on New to create a new client script.
    3. 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.
    4. 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_task 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 = '';
                    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();
    })();
    
    1. Save the Client Script:
      • Save or update the client script.

    Testing Your Configuration:

    1. Open a sc_task record:

      • Navigate to a sc_task record and open it.
    2. Verify the requested_item.u_asset Field:

      • Check the requested_item.u_asset field and attempt to change its value.
      • Ensure that the reference qualifier logic is applied correctly and the appropriate asset references are shown.

    Explanation:

    • setAssetReferenceQualifier Function: A function that encapsulates your reference qualifier logic.
    • refQualifier Function: A function within setAssetReferenceQualifier that returns the appropriate reference qualifier.
    • g_form.setReferenceQualifier: Sets the reference qualifier for the requested_item.u_asset field.

    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_task table see the same references as configured in the sc_req_item table through the complex reference qualifier logic.

  7. User

    It shows function onload error

  8. Assistant

    If you're encountering an onLoad error 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 an onLoad client script.

    Here’s a revised approach:

    1. Remove references to current:

      • Note that in client scripts, you cannot access server-side variables like current. You'll need to access form fields using g_form.
    2. 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.
    3. Correct the variable access:

      • Ensure form variables are accessed correctly using g_form.getValue or g_form.getDisplayValue.

    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:

    1. Declare the Function:

      • The setAssetReferenceQualifier function encapsulates the logic for determining the reference qualifier.
    2. Form Field Checks:

      • At the beginning of the function, there’s a check to ensure the requested_item.u_asset control exists in the form.
    3. Accessing Form Variables:

      • Instead of using current (which is not available on the client side), use g_form.getValue and g_form.getDisplayValue to get the necessary form values.
    4. Setting the Reference Qualifier:

      • The refQualifier function is set as the reference qualifier for the requested_item.u_asset field using g_form.setReferenceQualifier.

    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.log or console.log to 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 onLoad error and successfully apply the intended reference qualifier logic to the requested_item.u_asset field on the sc_task form.

  9. User

    It shows function onLoad () error

  10. Assistant

    To address the function onLoad error, let's simplify and ensure the client script is structured correctly. The key points to consider:

    1. Client scripts will not have current context available; instead, use g_form.
    2. Define and apply the reference qualifier logic correctly.
    3. Ensure the logic is correctly encapsulated within an onLoad function.

    Here's how you can achieve this in a more straightforward way:

    Simplified Client Script for sc_task Form

    function 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:

    1. Define the onLoad Function:

      • Encapsulate the logic inside the onLoad function, which will trigger when the form loads.
    2. Field Existence Check:

      • Ensure the requested_item.u_asset control exists on the form.
    3. Define the Reference Qualifier Function:

      • The refQualifier function contains your custom logic to filter the assets.
    4. Set the Reference Qualifier:

      • Use g_form.setReferenceQualifier to apply the qualifier logic to the requested_item.u_asset field.

    Applying the Client Script:

    1. Create the Client Script:

      • Navigate to System Definition > Client Scripts.
      • Click New to create a new client script.
    2. Configure the Client Script:

      • Name: Set Reference Qualifier for u_asset in sc_task.
      • Table: sc_task.
      • Type: onLoad.
    3. Insert the Script:

      • Copy and paste the above onLoad function script into the Script field.
    4. Save the Client Script:

      • Save or update the client script.

    Verification:

    1. Open a sc_task Record:

      • Open a record in the sc_task table and check the requested_item.u_asset field.
    2. Change the Value:

      • Attempt to change the value in the requested_item.u_asset field and verify the correct references are displayed as per the logic.

    Debugging Tips:

    • Use gs.log for Server-Side:

      • For client-side debugging, consider using console.log to log values in the browser's console.
      • Example: console.log('Asset Type:', g_form.getValue('requested_item.variables.asset_type'));
    • 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 onLoad event and using g_form for client-side operations, you can avoid the function onLoad error and achieve the desired reference qualifier behavior on the sc_task form.

  11. User

    Not working, current we cant able to use

  12. Assistant

    I understand the challenge. Since client scripts do not have access to variables in the current object, we'll need to refactor the script to work properly in the client-side context.

    Let's approach this problem methodically:

    1. Variables Loading: Ensure the fields from the requested_item are accessible on the sc_task form.
    2. Dynamic Reference Qualifier: Apply an appropriate reference qualifier for the dot-walked u_asset field.
    3. Client Script: Properly encapsulate the logic within the client script without relying on server-side current object.

    Here’s a revised client script that simplifies the application of the reference qualifier:

    Simplified Approach:

    1. Ensure All Necessary Fields are Available on Form:

      • Make sure fields from requested_item are correctly loaded and accessible in the sc_task form. This might involve using catalog item variables or adding the relevant fields via a form layout configuration if they are not visible.
    2. Client-side Script:

      • Correctly use the g_form object to fetch values and set the reference qualifier.

    Revised Client Script:

    Let's create a client script for the sc_task table:

    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:

    1. Create the Client Script:

      • Navigate to System Definition > Client Scripts.
      • Click New to create a new client script.
    2. Configure the Client Script:

      • Name: Set Reference Qualifier for u_asset in sc_task.
      • Table: sc_task.
      • Type: onLoad.
    3. Insert the Script:

      • Copy and paste the above onLoad function script into the Script field.
    4. Save the Client Script:

      • Save or update the client script.

    Verification:

    1. Open a sc_task Record:

      • Navigate to a record in the sc_task table and open it.
    2. Verify the Field:

      • Check the requested_item.u_asset field and attempt to change its value.
      • Ensure the correct references are shown as per the logic.

    Debugging Tips:

    1. Use Browser Console:

      • Use console.log statements within your client script to check the values and flows.
      • Example:
        console.log('Asset Type:', g_form.getValue('requested_item.variables.asset_type'));
        
    2. 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 onLoad client script and ensuring the form fields are accessible, this approach should avoid the function onLoad error and apply the desired reference qualifier logic on the sc_task form.