Copy Managed Document

Christine35
Tera Expert

Hello:

 

We would like to  copy many records from one table to another, including managed documents on each record's  Documents related list. The procedures below work for individual records, and we would like to duplicate this using a script, eventually as part of a transform map.  (GlidesysAttachment works for attachments, but doesn't seem to work for managed documents). We'd appreciate any pointers or advice on how to create a script to copy managed documents from one record to another. Thank you.

 

https://docs.servicenow.com/en-US/bundle/utah-servicenow-platform/page/product/document-services/tas....

 

 

1 ACCEPTED SOLUTION

Christine35
Tera Expert

We did manage to do this, here is the onComplete transform map script:

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

              var grSource = new GlideRecord(<’source table>');

              grSource.addQuery('sys_id', source.u_sys_id);

              grSource.query();           

              grSource.next());

 

              grTarget = new GlideRecord(‘<target table>');

              grTarget.addQuery('sys_id', target.sys_id);

              grTarget.query();

              grTarget.next();

             

              grRefSource = new GlideRecord('ds_document_references');

              grRefSource.addQuery('table_name',  ‘<source table>’);

              grRefSource.addQuery('target_record', grSource.sys_id);

              grRefSource.query();

              while (grRefSource.next()) {

                                           create();

              }

             

              function create() {          

             

                             var gr = new GlideRecord('ds_document_references');

                             gr.initialize();

 

                             var gr2 = new GlideRecord('ds_document');

                             gr2.addQuery('sys_id', grRefSource.document); 

                             gr2.query();

                             if (gr2.next()) {  

                                           gr.document = gr2.sys_id;

                                           gr.table_name = ‘<target table>’;                                        

                                           gr.target_record =  grTarget.sys_id;

                                           gr.insert();

                             }

              }

})(source, map, log, target);

View solution in original post

1 REPLY 1

Christine35
Tera Expert

We did manage to do this, here is the onComplete transform map script:

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

              var grSource = new GlideRecord(<’source table>');

              grSource.addQuery('sys_id', source.u_sys_id);

              grSource.query();           

              grSource.next());

 

              grTarget = new GlideRecord(‘<target table>');

              grTarget.addQuery('sys_id', target.sys_id);

              grTarget.query();

              grTarget.next();

             

              grRefSource = new GlideRecord('ds_document_references');

              grRefSource.addQuery('table_name',  ‘<source table>’);

              grRefSource.addQuery('target_record', grSource.sys_id);

              grRefSource.query();

              while (grRefSource.next()) {

                                           create();

              }

             

              function create() {          

             

                             var gr = new GlideRecord('ds_document_references');

                             gr.initialize();

 

                             var gr2 = new GlideRecord('ds_document');

                             gr2.addQuery('sys_id', grRefSource.document); 

                             gr2.query();

                             if (gr2.next()) {  

                                           gr.document = gr2.sys_id;

                                           gr.table_name = ‘<target table>’;                                        

                                           gr.target_record =  grTarget.sys_id;

                                           gr.insert();

                             }

              }

})(source, map, log, target);