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

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

You cannot update asset in this way, you have to write another glide record to get to that asset record, and then update install status there.

Update on dot walk doesnt work.

-Anurag

-Anurag

Brian Lancaster
Tera Sage

Something like this should work.

var gr = new GlideRecord('clm_m2m_contract_asset');
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();
	}
}

 

 

Hi Brian,

 

I had something similar to this originally:

//var exContract = new GlideRecord('ast_contract');
//exContract.addQuery('state','Expired');
//exContract.query();
//while (exContract.next()){
//var assets = new GlideRecord('alm_asset');
//assets.addQuery('lease_id',exContract.number);
//assets.query();
//while (assets.next());{
//	assets.install_status = '7';
//}}

I don't think it was this version but one of them resulted in 2000 new duplicate assets being created. I assumed it was the nested GlideRecord. 

I will give your code a go and let you know. 😉

Thank you.

 

you should add asset.update() in the script above.

-Anurag