How to update system set (using script)

Takumi_
Tera Contributor

Hi All.

 

I'm trying to update system set (sc_item_option)

 

I want to know that if you know something.

 

--------------------------------------------------

sample

-------------------------------------------------

Takumi__0-1715948360911.png

// RITMNumber -> RITM + some number
// query table sc_item_option_mtom, get sc_item_option
var mtomGR = new GlideRecord('sc_item_option_mtom');
mtomGR.query();
var targetItemOptionSysID;
while (mtomGR.next()) {
if (mtomGR.getDisplayValue('request_item') == RITMNumber) {
targetItemOptionSysID = mtomGR.getValue('sc_item_option');
break;
}
}
// query table sc_item_option
var itemOptionGR = new GlideRecord('sc_item_option');
itemOptionGR.addQuery('sys_id', targetItemOptionSysID);
itemOptionGR.query();
while(itemOptionGR.next()){
if(itemOptionGR.getValue(value) == 'thisIsTarget'){
itemOptionGR.setValue('value', 'testValue');
itemOptionGR.update();
}
}

-------------------------------------------------

I'm not good at English.

You don't know what I said, Please ask me.

1 ACCEPTED SOLUTION

Abhit
Tera Guru

Hi @Takumi_ 

Add a query condition, while you perform GlideRecord operation.

try the below code. this code will update the values where the values are "testValue" to test.

if you want to update any particular variables value to something then you have add a check condition in the GildeRecord of mtom table within the if loop under while loop.

 

var RITMNumber = 'RITM0010001'; // -> RITM + some number
// query table sc_item_option_mtom, get sc_item_option
var mtomGR = new GlideRecord('sc_item_option_mtom');
//mtomGR.addEncodedQuery("request_item.numberSTARTSWITH"+RITMNumber); // Always add a filter to out the unwanted response.
mtomGR.query();
var targetItemOptionSysID;
while (mtomGR.next()) {
    if (mtomGR.getDisplayValue('request_item') == RITMNumber) {
        targetItemOptionSysID = mtomGR.getValue('sc_item_option');
        updateOptionValue(targetItemOptionSysID);
    }
}

function updateOptionValue(sysid) {
    // query table sc_item_option
    var itemOptionGR = new GlideRecord('sc_item_option');
    itemOptionGR.addQuery('sys_id', sysid);
    itemOptionGR.query();
    while (itemOptionGR.next()) {
        if (itemOptionGR.getValue('value') == 'testValue') {
            itemOptionGR.setValue('value', 'test');
            itemOptionGR.update();
        }
    }
}


Cheers!!

Abhit

View solution in original post

6 REPLIES 6

Abhit
Tera Guru

Hi @Takumi_ 

Add a query condition, while you perform GlideRecord operation.

try the below code. this code will update the values where the values are "testValue" to test.

if you want to update any particular variables value to something then you have add a check condition in the GildeRecord of mtom table within the if loop under while loop.

 

var RITMNumber = 'RITM0010001'; // -> RITM + some number
// query table sc_item_option_mtom, get sc_item_option
var mtomGR = new GlideRecord('sc_item_option_mtom');
//mtomGR.addEncodedQuery("request_item.numberSTARTSWITH"+RITMNumber); // Always add a filter to out the unwanted response.
mtomGR.query();
var targetItemOptionSysID;
while (mtomGR.next()) {
    if (mtomGR.getDisplayValue('request_item') == RITMNumber) {
        targetItemOptionSysID = mtomGR.getValue('sc_item_option');
        updateOptionValue(targetItemOptionSysID);
    }
}

function updateOptionValue(sysid) {
    // query table sc_item_option
    var itemOptionGR = new GlideRecord('sc_item_option');
    itemOptionGR.addQuery('sys_id', sysid);
    itemOptionGR.query();
    while (itemOptionGR.next()) {
        if (itemOptionGR.getValue('value') == 'testValue') {
            itemOptionGR.setValue('value', 'test');
            itemOptionGR.update();
        }
    }
}


Cheers!!

Abhit

Takumi_
Tera Contributor

Thank you Abhit.

I can update system set.

Takumi_
Tera Contributor

Thank you for your answer.

 

It's almost perfect.

 

But I have one more question.

 

What I should do , if query question or name.

 

I added a query but it doesn't work.

Takumi__0-1716194081323.png

Hi @Takumi_ 

 

try the below line.

 itemOptionGR.addQuery('item_option_new.question_text', 'hostname_1');

 
Abhit