Display values only for specific categories and fix input values otherwise

matsuo akari
Tera Contributor

Categories currently exist: Test, Software, Hardware and Network.

Category: When selecting a test, display "This is a test" in the short description.

However, when a category other than test is selected, the short description "This is a test" is deleted and the user writes an arbitrary description.

Here, when the category is selected from non-test to non-test, the description entered in the short description will not be deleted and will continue to be used.

For example, when selecting Category: Software, enter "I am a company employee" in the short description. Next, when Category: Network is selected, the value of short description will continue to be displayed as "I am a company employee" that was entered when Category: Software was selected.

To achieve this, I wrote the script below. but it didn't work. Can someone please help me? Thank you.スクリーンショット (51).png

 

1 ACCEPTED SOLUTION

Sonam Tiwari
Tera Guru

@matsuo akari ,

 

I see what you are saying. I tested it again and saw the issue. 

Figured out that oldValue is not something that we can catch until the record is saved. So I tried to find something to fetch the previous value and came across this post:

https://www.servicenow.com/community/developer-forum/onchange-client-script-previous-value/m-p/21614...

With the help of this, I could rewrite the script and it worked fine for me.

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


    var previousValue;
    if (g_scratchpad.previousValue == undefined) {
        previousValue = oldValue;
    } else {
        previousValue = g_scratchpad.previousValue;
    }


    if (previousValue == 'test') {
       // g_form.addInfoMessage(previousValue);

        g_form.clearValue('short_description');
    }

    if (newValue == 'test') {
        g_form.setValue('short_description', "This is a test short description");
    } else {
        
        g_form.setValue('short_description', g_form.getValue('short_description'));

    }
    g_scratchpad.previousValue = newValue;
}

 Please test once with required changes from your end.

Thanks 

View solution in original post

5 REPLIES 5

Sonam Tiwari
Tera Guru

Hi @matsuo akari ,

Can you please try below once

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    
    if (oldValue == 'test') {
      
        g_form.clearValue('short_description');
    }

    if (newValue == 'test') {
        g_form.setValue('short_description'"This is a test short description");
    } else {
        g_form.setValue('short_description', g_form.getValue('short_description'));

    }
}

Thank you, @Sonam Tiwari !

 

I tried implementing the script you taught me once, but when I selected another category from Category: Test, the value of shortdescription at Category: Test did not disappear and was used as it was. I want to erase this. Thank you for your help!

Thank you @Sonam Tiwari !

I tried implementing the script you taught me once, but when I selected another category from Category:Test, the value of shortdescription at Category: Test did not disappear and was used as it was. I want to erase this. Thank you for your help!

Sonam Tiwari
Tera Guru

@matsuo akari ,

 

I see what you are saying. I tested it again and saw the issue. 

Figured out that oldValue is not something that we can catch until the record is saved. So I tried to find something to fetch the previous value and came across this post:

https://www.servicenow.com/community/developer-forum/onchange-client-script-previous-value/m-p/21614...

With the help of this, I could rewrite the script and it worked fine for me.

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


    var previousValue;
    if (g_scratchpad.previousValue == undefined) {
        previousValue = oldValue;
    } else {
        previousValue = g_scratchpad.previousValue;
    }


    if (previousValue == 'test') {
       // g_form.addInfoMessage(previousValue);

        g_form.clearValue('short_description');
    }

    if (newValue == 'test') {
        g_form.setValue('short_description', "This is a test short description");
    } else {
        
        g_form.setValue('short_description', g_form.getValue('short_description'));

    }
    g_scratchpad.previousValue = newValue;
}

 Please test once with required changes from your end.

Thanks