Create change task from fix script

Enkhdalai
Tera Expert

Hi everyone, 

I need to create change task on following condition matched change requests:find_real_file.png

I have already made changes in workflow, so new created change requests will have desired change tasks.

Now I have to create change task on already created change request which have old workflow so change task needed to created with fix script. 

Here is fix script I used:

var ct = new GlideRecord('change_task');  
ct.addActiveQuery();  
ct.addQuery('change_request', current.sys_id);  
ct.setLimit(1);  
ct.priority = '3';
ct.state = '1';
ct.short_description = "Short Desc 1";
ct.description = "description";
ct.assignment_group = '4fcc8c9bdb2be010af1fa2ddd39619be';

ct.query();  

          Here is the result.  

valuator: com.glide.script.RhinoEcmaError: Cannot read property "sys_id" from null
   script : Line(3) column(0)
      1: var ct = new GlideRecord('change_task');  
      2: ct.addActiveQuery();  
==>   3: ct.addQuery('change_request', current.sys_id);  
      4: ct.setLimit(1);  
      5: ct.priority = '3';
      6: ct.state = '1';

[0:00:00.042] Total Time
1 ACCEPTED SOLUTION

SumanthDosapati
Mega Sage

Hi @Enkhdalai 

Your script is not correct. You can try below

var gr = new GlideRecord("change_request");
gr.addEncodedQuery("Software^active=true^start_date<javascript:gs.dateGenerate('2022-08-29','00:00:00')"); //you can update your encoded query as required
gr.query();
while (gr.next()) {
    var ct = new GlideRecord('change_task');
    ct.initialize();
    ct.change_request = gr.sys_id;
    ct.priority = '3';
    ct.state = '1';
    ct.short_description = "This change task created via fix script"; //you need to update as required
    ct.description = "Give description of change task here";
    ct.assignment_group = '4fcc8c9bdb2be010af1fa2ddd39619be';
    ct.insert();
}

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

View solution in original post

3 REPLIES 3

Vasantharajan N
Tera Sage

current object is not defined or not recognized in the fix script due to that reason you are getting the error.

Please try the below code.

Note: PLEASE UPDATE THE VARIABLE "encodedFilter" in the below script then test it in the NON-PROD instance first

var encodedFilter = ""; // Please add the encoded query then execute.

var gr = new GlideRecord("change_request");
gr.addEncodedQuery(encodedFilter);
gr.query();

gs.info("No of Change request that will be add the change task " + gr.getRowCount());

while (gr.next()) {
    //Create the change task
    var ct = new GlideRecord('change_task');
    ct.initialize();
    ct.change_request = gr.sys_id;
    ct.priority = '3';
    ct.state = '1';
    ct.short_description = "Short Desc 1";
    ct.description = "description";
    ct.assignment_group = '4fcc8c9bdb2be010af1fa2ddd39619be';
    ct.insert();
}

Thanks & Regards,
Vasanth

SumanthDosapati
Mega Sage

Hi @Enkhdalai 

Your script is not correct. You can try below

var gr = new GlideRecord("change_request");
gr.addEncodedQuery("Software^active=true^start_date<javascript:gs.dateGenerate('2022-08-29','00:00:00')"); //you can update your encoded query as required
gr.query();
while (gr.next()) {
    var ct = new GlideRecord('change_task');
    ct.initialize();
    ct.change_request = gr.sys_id;
    ct.priority = '3';
    ct.state = '1';
    ct.short_description = "This change task created via fix script"; //you need to update as required
    ct.description = "Give description of change task here";
    ct.assignment_group = '4fcc8c9bdb2be010af1fa2ddd39619be';
    ct.insert();
}

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

Thank you, this script is working well.