Split the value from look up select box field and auto populate in single line text variable

satya30
Tera Contributor

I have a look up select field where I am populating the values of u_code, u_label by seperator (u_code|u_label)
then I have a single text variable which is read only where I need to auto populate the u_code that is getting displayed in the look up select box field.

Here, I cannot change the Look up select box to Reference field.

Thanks in advance.

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@satya30 

either change the variable to Reference

OR

you can write onChange catalog client script and get the value and then split.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Can you please help me with the scripting as I tried different ways but still this is storing the sys_id of the record in the code field.

what needs to be done here. please help me. thank you.

function onChange(control, oldValue, newValue, isLoading) {
    // If the form is loading or the new value is empty, exit
    if (isLoading) {
        return;
    }

 

    // Show an info message indicating that the value is being processed
   /* g_form.addInfoMessage('Processing the selected value... Please wait.');

 

    // Get the selected value in the lookup field (this will be the Sys ID)
    var selectedOption = g_form.getValue('look_up');

 

    // Log the selected option to see what value is returned from the lookup field
    g_form.addInfoMessage('Selected Option: ' + selectedOption);

 

    // If a valid option is selected, fetch the corresponding record and get u_code
    if (selectedOption) {
        // Log to confirm we're entering the block of code
        g_form.addInfoMessage('Processing selected option...');

 

        // Query the reference record using GlideRecord
        var gr = new GlideRecord('table'); // Your reference table
        if (gr.get(selectedOption)) { 
            // Log the u_code value for debugging
            g_form.addInfoMessage('Retrieved u_code: ' + gr.u_code);

 

            // Set the value of uCode field
            g_form.setValue('uCode', gr.u_code);

 

            // Confirm that the field was updated
            g_form.addInfoMessage('uCode has been set to: ' + gr.u_code);
        } else {
            // If no matching record was found, log this as well
            g_form.addInfoMessage('No matching record found for Sys ID: ' + selectedOption);
        }
    } else {
        // If no valid selection was made, log this as well
        g_form.addInfoMessage('No option selected.');
    }
}
*/

 

       
        var name = g_form.getValue('look_up');
        var Val = name.split('|')[0];
        g_form.setValue('uCode', Val);
            g_form.addInfoMessage('No option selected.'+Val);
        }

@satya30 

are you getting sysId when you added the info message?

If yes then you can GlideRecord and get the value of field u_code from that table

If you are allowed to change the variable type then please make it Reference type

Then you can use Auto populate feature and populate the u_code in your next single line text variable

Auto-populate a variable based on a reference type variable (Utah) 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader