How can we set attachment flag for existing HR Cases?

Community Alums
Not applicable

 

@Mark Roethof Thanks! Your below post helps me solving BR for the new Cases.

 

https://www.servicenow.com/community/hrsd-forum/how-to-check-if-hr-case-has-attachment/m-p/1336697/p...

 

For existing, Cases I am trying a Fix script by modifying the code but it not working. 

 

var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addEncodedQuery('table_nameLIKEsn_hr');
grAttachment.query();
while(grAttachment.next()) {
var grRecord = new GlideRecord(grAttachment.getValue('table_name'));
grRecord.addQuery('sys_id', grAttachment.getValue('table_sys_id'));
grRecord.addEncodedQuery('state=<different states>');
grRecord._query();
if (grRecord._next()) {
grRecord.setWorkflow(false);
grRecord.u_has_attachment = true; <custom column in sn_hr_case_core table>
grRecord.update();
}
}

I am running the Fix script in global scope.

Thoughts?

 

Thanks,

MK

1 ACCEPTED SOLUTION

Community Alums
Not applicable

@Anil Lande - I did add the gs.info() for testing prospective and it was going into the code too. Some how it was not executing the code.

I took a different approach, since I need to run the fix script for handful of records with the same HR Service and some specific states so I took the sys_id from attachment table and executed directly at the HR table level. In this way, I didn't face any RCA issue too. Moreover, in this way I know which records it is touching.

 

Here's the simplify code:

var grRecord = new GlideRecord('<HR workflow admin table>');
grRecord.addEncodedQuery('sys_idIN<bunch of sys_id>^state=10');
grRecord.query();
while(grRecord.next()) {
grRecord.setWorkflow(false);
grRecord.u_has_attachment = true;
grRecord.update();
}

View solution in original post

5 REPLIES 5

Anil Lande
Kilo Patron

Hi,

Have you tried adding some logs?

var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addEncodedQuery('table_nameLIKEsn_hr');
grAttachment.query();
gs.info('AAAAA Found Attachments : '+grAttachment.getRowCount());
while(grAttachment.next()) {
var grRecord = new GlideRecord(grAttachment.getValue('table_name'));
grRecord.addQuery('sys_id', grAttachment.getValue('table_sys_id'));
grRecord.addEncodedQuery('state=2');//<use different states values>
grRecord._query();
if (grRecord._next()) {
gs.info('AAAAA Updating Has Attachments on case : '+grAttachment.getRowCount());
grRecord.setWorkflow(false);
grRecord.u_has_attachment = true; //<custom column in sn_hr_case_core table>
grRecord.update();
}
}

See if these logs coming in, also try running this script in Application scope.

 

 

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Community Alums
Not applicable

@Anil Lande - I did add the gs.info() for testing prospective and it was going into the code too. Some how it was not executing the code.

I took a different approach, since I need to run the fix script for handful of records with the same HR Service and some specific states so I took the sys_id from attachment table and executed directly at the HR table level. In this way, I didn't face any RCA issue too. Moreover, in this way I know which records it is touching.

 

Here's the simplify code:

var grRecord = new GlideRecord('<HR workflow admin table>');
grRecord.addEncodedQuery('sys_idIN<bunch of sys_id>^state=10');
grRecord.query();
while(grRecord.next()) {
grRecord.setWorkflow(false);
grRecord.u_has_attachment = true;
grRecord.update();
}

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @Community Alums ,

                                       There are some typo in your script 
grRecord.
query(); // Use 'query()' instead of '_query()'
if (grRecord.next()) { // Use 'next()' instead of '_next()'

Also you can try to change encoded query as below

 

// Make sure to replace 'sn_hr_case_core' with the actual table name of your HR cases table
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addEncodedQuery('table_name=sn_hr_case_core'); // Use '=' instead of 'LIKE' to match exact table name
grAttachment.query();

while (grAttachment.next()) {
  var grRecord = new GlideRecord(grAttachment.getValue('table_name'));
  grRecord.addQuery('sys_id', grAttachment.getValue('table_sys_id'));
  grRecord.query(); 

  if (grRecord.next()) { 
    grRecord.u_has_attachment = true; 
    grRecord.setWorkflow(false);
    grRecord.update();
  }
}

 

Kindly mark correct and helpful If applicable

@Chetan Mahajan 

FYI, _query() and _next() are alternative methods to query() and next().

 

https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/c_GlideRecordAPI#r_GlideR...

https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/c_GlideRecordAPI#r_GlideR...

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande