SetValue not working

Andrew Bettcher
Kilo Sage

Hello,

I'm trying to create a script that can run early in the morning to set all of the assets that are associated with expired contracts to "Retired" status (we get our contract and asset details from a 3rd party system that insists on adding a new unique reference even though it's the same actual asset and so this is to avoid duplicates appearing on searches - search fields are set to exclude retired assets).

I'm banging my head against the wall here. It should be nice and easy.

I started defining the contracts via a glideRecord and then grabbing the associated assets but that didn't work so I change it to use the clm_m2m_contract_asset records instead. It finds the records OK but I just can't get it to set the related assets install status to retired.

Here is the latest iteration:

 

var gr = new GlideRecord('clm_m2m_contract_asset');
gr.addQuery('contract.state','expired');
gr.query();

while (gr.next()){
	gr.setValue('asset.install_status', 7);
	gr.update();
}

 

I've tried every permutation of the "setValue" line that I can think of including simply asset.install_status = 7. No dice.

(7 is the value for "Retired").

What's going wrong here?

1 ACCEPTED SOLUTION

I'm not sure. I just modified the script so I could run it against one that is expired in my PDI demo data and it worked.  Below is the code ran in the script background.

var gr = new GlideRecord('clm_m2m_contract_asset');
gr.addQuery('contract.number', 'CNTR0009009')
//gr.addQuery('contract.state','expired');
gr.query();

while (gr.next()){
	var asset = new GlideRecord('alm_asset');
	asset.addQuery('sys_id', gr.asset);
	asset.query();
	if (asset.next()){
		asset.setValue('install_status', 7);
		asset.update();
	}
}

 

View solution in original post

10 REPLIES 10

Hi Brian,

It's quite bizarre. Your code looked to me to be as valid as anything else I've tried but the text asset that I'm using isn't retired.

The only thing I can think of is that the install_status is actually a reference from the asset table itself:

find_real_file.png

That shouldn't matter though right, because we're saying, wherever it is, update it.

Any ideas?

I'm not sure. I just modified the script so I could run it against one that is expired in my PDI demo data and it worked.  Below is the code ran in the script background.

var gr = new GlideRecord('clm_m2m_contract_asset');
gr.addQuery('contract.number', 'CNTR0009009')
//gr.addQuery('contract.state','expired');
gr.query();

while (gr.next()){
	var asset = new GlideRecord('alm_asset');
	asset.addQuery('sys_id', gr.asset);
	asset.query();
	if (asset.next()){
		asset.setValue('install_status', 7);
		asset.update();
	}
}

 

Curiouser and curiouser. It doesn't completely surprise me. Our instance was administrated by people who weren't trained back in the day. They were coders but not ServiceNow coders. I come across things like this all the time. 

All common sense says it should work but some weird config prevents it.

 

Thank you for you help. I'll try debug and also look through the business rules to see if I can work out what's happening. If I crack it (WHEN I crack it), I'll post. 

I can't explain this but I started with a blank page again and pasted your script in, amended it back to the original for all contracts and it worked.

Let's assume that this was something I did wrong at some point BUT, I did actually copy and paste your original script when you posted it so I'm very confused. I'm also relieved and a little frustrated that it took me all day to do something relatively simple.

 

Anyway, thank you again.

Rajesh Kannan G
ServiceNow Employee
ServiceNow Employee

How about this query

select * from alm_asset 
where sys_id in (
         select m2m.asset 
         from clm_m2m_contract_asset m2m, ast_contract cnt 
         where m2m.contract = cnt.sys_id and 
         cnt.state = 'expired')

and the script is

var gr = new GlideRecord('alm_asset');
var joinCond = gr.addJoinQuery('clm_m2m_contract_asset', 'sys_id', 'asset');
joinCond.addCondition('contract.state','expired');
gr.setValue('install_status', 7);
gr.updateMultiple();

 

Regards,

Rajesh