How to write fix script?

Shashi K
Tera Contributor

I have a requirement when ever an Change is closed configuration item filed must not be empty.

 

How to write in fix script? I want only fix script 

1 ACCEPTED SOLUTION

Chetan Mahajan
Kilo Sage
Kilo Sage

Hi Shashi,

                   If I correct you want to update configuration item field for closed change. once you run fix script whatever changes has empty CI it will get update with some value ? so you can refer below script

updateCI();
function updateCI(){
	try{
		var gr = new GlideRecord('change_request');
		gr.addEncodedQuery('state=3^cmdb_ciISEMPTY'); // state is closed and ci is empty
		gr.query();
		while(gr.next()){
			// your logic of updating here
			gr.setValue('cmdb_ci','b0cb50c3c0a8000900893e69d3c5885e');
			gr.update();
		}
	}
	catch(ex){
		gs.info(ex);
	}
}

Kindly mark response correct and helpful if applicable

View solution in original post

10 REPLIES 10

Chetan Mahajan
Kilo Sage
Kilo Sage

Hi Shashi,

                   If I correct you want to update configuration item field for closed change. once you run fix script whatever changes has empty CI it will get update with some value ? so you can refer below script

updateCI();
function updateCI(){
	try{
		var gr = new GlideRecord('change_request');
		gr.addEncodedQuery('state=3^cmdb_ciISEMPTY'); // state is closed and ci is empty
		gr.query();
		while(gr.next()){
			// your logic of updating here
			gr.setValue('cmdb_ci','b0cb50c3c0a8000900893e69d3c5885e');
			gr.update();
		}
	}
	catch(ex){
		gs.info(ex);
	}
}

Kindly mark response correct and helpful if applicable

Hi Chetan, 

If CI field is empty then the change (Change request) should not close

But for this requirement you don't need fix script,  you can do it by client script. Fix script is not use for Validation purpose. you can use below onChange Client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

  var ci = g_form.getValue('cmdb_ci');   
  var state = g_form.getValue('state');
	if(state == 3 && ci == ''){
		alert("Please select Configuration item");
		return false;
	}

}

Sai Kumar B
Mega Sage
Mega Sage

@Shashi K 

Can you elaborate?

Do you want to apply the patch for existing closed change request records? If yes, you can try the code shared by @Chetan Mahajan, If you want to apply new validation to restrict the users from closing the change without ci then you have to write client scripts/Business rules to validate.