Copy variables data to a string field

si21
Tera Guru

Hi experts, can we copy the below variables 'name: value' to the string field.

Like we do copy variables data to HR case description.

cbvd.png

TIA

1 ACCEPTED SOLUTION

AakashG2703
Mega Guru

Hi @si21,

Hope you are doing well.

 

Proposed Solution

In order to achieve this task, I personally tried it on my Personal Developer Instance so that you can get the solution as soon as possible. For this, you just need to create an "On-Change Catalog Client Script" and configure it to get all the values of 3 fields that you mentioned in your ask or question and set the value of "String" Field with the help of the script mentioned below: -

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var name = g_form.getDisplayValue('requested_for'); // Replace requested_for with New manager name
    var email = g_form.getDisplayValue('email'); // // Replace email with Location
    var company = g_form.getDisplayValue('company'); // No need to change anything over here
    if (name == '' || email == '') {
        alert('Kindly fill above two fields first');
        g_form.setValue('company', '');
    } else {
        var val = 'Name: ' + name + '\nEmail: ' + email + '\nCompany ' + company;
        g_form.setValue('string', val);
    }
}

 

 

For your reference, also attaching screenshots of the outputs that will give you better insights of how the script is working or the best thing will be to follow the solution and execute the scripts on your instance.

 

AakashG2703_0-1712329271721.png

 

If Any Field is Missing: -

AakashG2703_1-1712329387461.png

 

Alerting User to fill above two fields First.

AakashG2703_2-1712329415904.png

 

Populate String Field if all the above 3 Fields are filled.

AakashG2703_3-1712329456419.png

 

If you find this information/knowledge/solution helpful, please don't forget to mark my solution and reply as helpful and accepted.

 

Thanks ‌‌:)

Aakash Garg

ServiceNow Developer

View solution in original post

2 REPLIES 2

Deepak Shaerma
Kilo Sage

Hi @si21 

just create a onChange Catalog client script for your requirements:
Type: Onchange
Field : New manager name (Note: you need to create 3 different catalog client scripts for each field-->company, location)

 var user = g_form.getValue('new_manager_name');
    var company = g_form.getValue('company');
    
    if (new_manager_name && company) {
        // Fetching display values instead of sys_ids for user-friendly names
        var userName = g_form.getDisplayBox('new_manager_name').value;
        var companyName = g_form.getDisplayBox('company').value;
        
        // Constructing the string
        var combinedValue = 'new_manager_name' + userName + 'Company ' + companyName;
        
        // Setting the constructed string to the target field
        g_form.setValue('string', combinedValue);
    }



Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma

AakashG2703
Mega Guru

Hi @si21,

Hope you are doing well.

 

Proposed Solution

In order to achieve this task, I personally tried it on my Personal Developer Instance so that you can get the solution as soon as possible. For this, you just need to create an "On-Change Catalog Client Script" and configure it to get all the values of 3 fields that you mentioned in your ask or question and set the value of "String" Field with the help of the script mentioned below: -

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var name = g_form.getDisplayValue('requested_for'); // Replace requested_for with New manager name
    var email = g_form.getDisplayValue('email'); // // Replace email with Location
    var company = g_form.getDisplayValue('company'); // No need to change anything over here
    if (name == '' || email == '') {
        alert('Kindly fill above two fields first');
        g_form.setValue('company', '');
    } else {
        var val = 'Name: ' + name + '\nEmail: ' + email + '\nCompany ' + company;
        g_form.setValue('string', val);
    }
}

 

 

For your reference, also attaching screenshots of the outputs that will give you better insights of how the script is working or the best thing will be to follow the solution and execute the scripts on your instance.

 

AakashG2703_0-1712329271721.png

 

If Any Field is Missing: -

AakashG2703_1-1712329387461.png

 

Alerting User to fill above two fields First.

AakashG2703_2-1712329415904.png

 

Populate String Field if all the above 3 Fields are filled.

AakashG2703_3-1712329456419.png

 

If you find this information/knowledge/solution helpful, please don't forget to mark my solution and reply as helpful and accepted.

 

Thanks ‌‌:)

Aakash Garg

ServiceNow Developer