Please tell me the script to realize below function

Kei Ogose
Tera Contributor

Hi, I'm Kei.

I would like to implement the below function.

I'm not good at Java Script.

That's why I would appreciate it if you could let me know the script.

 

<Function>

If the field of "Kitting Price" (api name:k_price) is 8500, the field of "Kitting" (api name:kitting ) can be value "standard"

If the field of "Kitting Price" (api name:k_price) is 11000, the field of "Kitting" (api name:kitting ) can be value "choice"

 

I would like to set the above script in the calculated Value of "Kitting Price" field.

And I have already created two records(Value 8500 record & Value 11000 record) in system_property table.

It seems to use the method gs.getProperty, but I'm not sure...

2 REPLIES 2

Chris McDevitt
ServiceNow Employee
ServiceNow Employee

Hi, 

Not a lot to go on but, if assume a Business Rule, then the code should be something like this:

 
var choice = gs.getProperty('choice');
var standard = gs.getProperty('standard');

if(current.k_price == 1100){
current.kitting = choice;
}else if(current.k_price == 8500){
current.kitting = standard;
}

Andrew_TND
Mega Sage
Mega Sage

Hi @Kei Ogose 

Really depends how its updated. If its being updated from another table I'd say go with a business rule like below.
Run: Before on Insert/Update 

(function executeRule(current, previous /*null when async*/ ) {
    var k_price = current.k_price;
    var knitting = current.knitting;
    if (k_price == 8500) {
        current.knitting = 'standard';
    } else if (k_price == 11000) {
        current.knitting = 'choice';
    }
})(current, previous);


Otherwise if its being updated in the client, go with this on change client script.

Run when k_price Changes

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    var k_price = g_form.getValue('k_price');
    var knittingf = g_form.getControl('knitting');
    if (k_price == 8500) {
        knittingf.value = 'standard';
    } else if (k_price == 11000) {
        knittingf.value = 'choice';
    }
}

 Please mark as helpful or if its resolved the issue, CORRECT!