Use GlideRecord setvalue to replace part of a string

David77
Giga Guru

Excuse my ignorance here, but this is not my area of expertise. 

Need to remove a hardcoded prefix value in the field alm_hardware.asset_tag from "ABC-" to null

Here's my code, but it's not working and need help

var gr = new GlideRecord('alm_hardware');
gr.addQuery('asset_tag' , 'STARTSWITH' , 'ABC-');
gr.setLimit(1);
gr.query();

while (gr.next()) {

gr.setvalue('asset_tag',gr.getvalue('asset_tag').replace('ABC-',''));
gr.update();
}

3 ACCEPTED SOLUTIONS

Rajesh Chopade1
Mega Sage

Hi @David77 

 

Try following script once:

var gr = new GlideRecord('alm_hardware');
gr.addQuery('asset_tag', 'STARTSWITH', 'ABC-');
gr.query();

while (gr.next()) {
    var updatedAssetTag = gr.getValue('asset_tag').replace('ABC-', '');
    gr.setValue('asset_tag', updatedAssetTag);
    gr.update();
}

I hope my answer helps you to resolve your issue, if yes mark my answer helpful and correct.

THANK YOU

rajesh chopade.

View solution in original post

@David77 Your almost there. Try assigning gr.getvalue('asset_tag').replace('ABC-','') to a variable and pass that variable in your set value.

 

please mark my response is helpful and accept the solution if it help you

View solution in original post

David77
Giga Guru

@Satishkumar B  thanks! only thing i still had to do was code it as: getValue
Did not realize case mattered in these commands. Thanks!!!

View solution in original post

3 REPLIES 3

Rajesh Chopade1
Mega Sage

Hi @David77 

 

Try following script once:

var gr = new GlideRecord('alm_hardware');
gr.addQuery('asset_tag', 'STARTSWITH', 'ABC-');
gr.query();

while (gr.next()) {
    var updatedAssetTag = gr.getValue('asset_tag').replace('ABC-', '');
    gr.setValue('asset_tag', updatedAssetTag);
    gr.update();
}

I hope my answer helps you to resolve your issue, if yes mark my answer helpful and correct.

THANK YOU

rajesh chopade.

@David77 Your almost there. Try assigning gr.getvalue('asset_tag').replace('ABC-','') to a variable and pass that variable in your set value.

 

please mark my response is helpful and accept the solution if it help you

David77
Giga Guru

@Satishkumar B  thanks! only thing i still had to do was code it as: getValue
Did not realize case mattered in these commands. Thanks!!!