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

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Samiksha2 

No, it is not possible unless you have that field in the template and under the table. You can't copy the template file directly to the table.

*************************************************************************************************************
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]

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Samiksha2 

you created template for your custom table.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Samiksha2 

when user opens form, he/she can apply template from bottom.

why not use that to set the values?

AnkurBawiskar_1-1742988161868.png

 

when user clicks on that template, the field values are set

AnkurBawiskar_0-1742988137301.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,
Client wants to be that template should be in the drop down.