Query to update column data based on a condition

Chrisww2
Kilo Contributor

I want to use something like this below but have some sort of condition as to only update/delete certain columns. I am new at coding so learning as I go.

 

What I am trying to do is just delete the columnn data from 

table x_nu_ea_devices column discovery_source where third_party_id is not null or empty

I am new to writing code so forgive me if I butchered this

 

var gr = new GlideRecord('u_table');

gr.query();


while(gr.next())

{

gr.u_column='';

gr.update();

7 REPLIES 7

Muhammad Khan
Mega Sage

Hi, 

You want to modify values of some records ?

It would be better if you provide details.

Added update

Prateek kumar
Mega Sage

Check this out;

https://thesnowball.co/how-to-use-addencodedquery-with-gliderecord-in-servicenow/


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Harneet Sital
Mega Sage
Mega Sage

Hi @Chrisww2 

var grUpdateColumn = new GlideRecord('x_nu_ea_devices');
grUpdateColumn.addNotNullQuery('third_party_id'); //Makes sure the record does not have third party id
grUpdateColumn.query();
while(grUpdateColumn.next())
{
 grUpdateColumn.discovery_source =''; //Sets to empty
 grUpdateColumn.update();
}

Hope this helps, try this code and let us know if there are any issues and we can try and resolve the issue. 

 

Thanks
-Harneet Sital
Request you to please mark my answer as helpful or correct based on the impact

Find all my ServiceNow articles here