Copy value from one field to another field based on a condition using Fix Script

Owais3
Tera Expert

Hello Devs,

 

I've got a Fix Script which only updates the limit amount once, and then when I re-run it nothing updates. Below is the script which partially works or so to say only works once.

 

(function() {
    // Creating a GlideRecord for the 'cmdb_ci' table
    var gr = new GlideRecord('cmdb_ci');
    // Querying CMDB CI records where 'owned_by' is not empty
    gr.addNotNullQuery('owned_by');
    // Limiting the query to 10 records
    gr.setLimit(10);
    // Executing the query
    gr.query();
    // Looping through the records and updating 'assignment_group' based on 'owned_by'
    while (gr.next()) {
        gr.assignment_group = gr.owned_by;
        gr.update();
    }
    gs.info('Fix script completed successfully. Updated 10 records.');
})();

 

Any idea?... Has to be a Fix Script.

Thanks

1 ACCEPTED SOLUTION

OlaN
Giga Sage
Giga Sage

Hi,

Try something like this (worked fine for me).

 

var copyCmdbResult = copyValues();
if (copyCmdbResult == 0){
	gs.info('No records updated');
}
else{
	gs.info('Update finished, Updated ' + copyCmdbResult + ' records');
}

function copyValues() {
	var cmdbGR = new GlideRecord('cmdb_ci');
	cmdbGR.setLimit(3);
	cmdbGR.addNotNullQuery('owned_by');
	cmdbGR.addNullQuery('assigned_to'); // added to make sure we don't overwrite existing value
	cmdbGR.query();
	var counter = 0;
	while (cmdbGR.next()){
	//    gs.info('id: ' + cmdbGR.getUniqueValue());
		cmdbGR.setValue('assigned_to', cmdbGR.getValue('owned_by')); // used getters and setters
		cmdbGR.update();
		counter++;
	}

	return counter;
}

 

View solution in original post

5 REPLIES 5

Astik Thombare
Tera Sage

Hi @Owais3 ,

 

The issue with the provided Fix Script is that it only updates the first 10 records retrieved by the query. This is because the setLimit(10) statement restricts the query to only returning the first 10 records that match the specified criteria. Once the script has processed these 10 records, it terminates and does not attempt to retrieve any further records.

To update all records that meet the criteria, you can remove the setLimit(10) statement from the script. This will allow the query to retrieve all matching records, and the script will process and update them all accordingly.

 

Here is the modified script

 

(function() {
  // Creating a GlideRecord for the 'cmdb_ci' table
  var gr = new GlideRecord('cmdb_ci');

  // Querying CMDB CI records where 'owned_by' is not empty
  gr.addNotNullQuery('owned_by');

  // Executing the query
  gr.query();

  // Looping through the records and updating 'assignment_group' based on 'owned_by'
  while (gr.next()) {
    gr.assignment_group = gr.owned_by;
    gr.update();
  }

  gs.info('Fix script completed successfully. Updated all records.');
})();

 

 With this modification, the script will update all records that have a non-empty owned_by field.

 

Please mark  Correct if this resolves your issue, and also mark 👍 Helpful if you find my response valuable based on the impact.

 

Regards,

Astik Thombare

Hey @Astik Thombare 

I appriciate your response, but don't think you understood the question. I am aware there's a limit, and I mentioned that in my question. The script runs fine if I remove the limit, the question was taking into account the limit and that the script runs once, and IF/WHEN ran again the next set of ci's do not update so what would the solution be.

 

Thanks for contributing anyway.

Hello @Owais3 ,

 

I apologize for misunderstanding your question.

 

As you are using the setLimit(10) statement, the script is fetching the same records every time it runs. This may be the reason why it is not updating records when you run it multiple times.

If you want to continue using the setLimit(10) statement, you can use the following script:

 

 

 

(function() {
    // Creating a GlideRecord for the 'cmdb_ci' table
       var gr = new GlideRecord('cmdb_ci');
    // Querying CMDB CI records where 'owned_by' is not empty
       gr.addNotNullQuery('owned_by');
    // Limiting the query to 10 records
       gr.setLimit(10);
    // Executing the query
       gr.query();
    // Looping through the records and updating 'assignment_group' based on 'owned_by'
    while (gr.next()) {
        gs.info(gr.name);
        gr.assignment_group = gr.owned_by;
        gr.setWorkflow(false); //Do not run business rules
        gr.autoSysFields(false); //Do not update system fields
        gr.setForceUpdate(true); 
        gr.update();
    }
    gs.info('Fix script completed successfully. Updated 10 records.');
})();

 

 

It is working fine. The script will update the records every time you run the fix script.

 

If you want to fetch different records every time and update them, you need to add one more condition to the script, such as fetching records that have not been updated in the last 5 minutes.etc 

 

Please mark Correct if this resolves your issue, and also mark 👍 Helpful if you find my response valuable based on its impact.

 

Regards,

Astik Thombare

Hi @Owais3 ,

 

Please mark  Correct if this resolves your issue, and also mark 👍 Helpful if you find my response valuable based on its impact.

 

Regards,

Astik Thombare