How to copy the template value to the string field

Samiksha2
Mega Sage

Hi All,

I have created some templates for a custom field. 
In the custom field, there is a reference field (Template)referencing to the sys_template table (I have added the filter to show only templates related to the table).

 

On change of Template field, values of that template should be copied to the string fields of the table.

 

Samiksha2_0-1742985207184.pngSamiksha2_1-1742985245641.png

 



Please help me.

Thanks,
Sam

1 ACCEPTED SOLUTION

@Samiksha2 

this worked for me

Template:

AnkurBawiskar_3-1742993203150.png

 

onChange client script: on that template field

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var values = g_form.getReference('u_template').template; // give here the field name

    var fieldValuePairs = values.split('^');

    // Loop through each pair and set the value on the form
    fieldValuePairs.forEach(function(pair) {
        var [field, value] = pair.split('=');
        if (field && value) {
            g_form.setValue(field, value);
        }
    });

    //Type appropriate comment here, and begin script below

}

AnkurBawiskar_0-1742992948000.png

 

Output:

Before: when template is not selected

AnkurBawiskar_1-1742993144366.png

 

After selecting template fields populated

AnkurBawiskar_2-1742993175794.png

 

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

View solution in original post

9 REPLIES 9

@Samiksha2 

then you should be able to achieve using the approach I shared above?

sharing it again here

Now on your table you have a field which refers to sys_template and want to bring the value

Use this in onChange with getReference() callback

1) get the values as encoded query

2) split and then set each field value

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

Hi @Samiksha2 

What will happen, or what is the next action, if a template is available in the dropdown? If the client wants that when they choose a template from the dropdown, it should be automatically applied, then this may not be a good use case. It would be helpful to educate the client on the OOTB (Out-of-the-Box) way of working.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

@Samiksha2 

this worked for me

Template:

AnkurBawiskar_3-1742993203150.png

 

onChange client script: on that template field

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var values = g_form.getReference('u_template').template; // give here the field name

    var fieldValuePairs = values.split('^');

    // Loop through each pair and set the value on the form
    fieldValuePairs.forEach(function(pair) {
        var [field, value] = pair.split('=');
        if (field && value) {
            g_form.setValue(field, value);
        }
    });

    //Type appropriate comment here, and begin script below

}

AnkurBawiskar_0-1742992948000.png

 

Output:

Before: when template is not selected

AnkurBawiskar_1-1742993144366.png

 

After selecting template fields populated

AnkurBawiskar_2-1742993175794.png

 

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

Hi @Ankur Bawiskar ,
It is working. You are awesome!!!


Thanks,
Sam

@Samiksha2 

Glad to help.

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