Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Copy tags from (sysapproval_approver)table to (sys_journal_field) table whenever the approval create

Akshay Pithore
Tera Contributor
Hello Team,

I am using the below Query Business Rule to copy tags from the sysapproval_approver table to the sys_journal_field table whenever an approval is created.

Currently, the tags are being copied, but they are not accurate and there is a mismatch issue. Please find the scenarios below:

When 3 approval requests are created, only 2 tags are copied initially. The 3rd approval’s tags are not copied at all.
When the approval state changes later, the tags are still not copied again for that record.
In some cases, after a state change, previous tags are copied instead of the correct/latest ones, which results in incorrect tag mapping.

Could you please suggest the required changes to fix this behavior?

Thank you.
(function executeRule(current, previous) {

    var taskSysId = current.element_id;
    gs.info("Task Sys ID: " + taskSysId);

    var appr = new GlideRecord('sysapproval_approver');
    appr.addQuery('sysapproval', taskSysId);
    appr.orderByDesc('sys_created_on');
   // appr.setLimit(1);
    appr.query();

    while (appr.next()) {

        gs.info("Approval found: " + appr.sys_id);
        gs.info("Approval state: " + appr.state);
        gs.info("Approval for task: " + appr.sysapproval);

        // Copy tags from approval to journal
        var tag = new GlideRecord('label_entry');
        tag.addQuery('table', 'sysapproval_approver');
        tag.addQuery('table_key', appr.sys_id);
        tag.query();

        if (!tag.hasNext()) {
            gs.info("No tags found for approval: " + appr.sys_id);
        }

        while (tag.next()) {

            gs.info("Processing tag: " + tag.label);

            var newTag = new GlideRecord('label_entry');
            newTag.initialize();
            newTag.label = tag.label.sys_id;
            newTag.table = current.getTableName();
            newTag.table_key = current.sys_id;
            newTag.insert();

            gs.info("Tag copied: " + tag.label + "  New Sys ID: " + newSysId);
        }

    }

    gs.info("BR COMPLETED for record: " + current.sys_id);

})(current, previous);

Please mark this response as correct or helpful if it assisted you with your question.
3 REPLIES 3

Tanushree Maiti
Kilo Patron

Hi @Akshay Pithore ,

Just try once with After insert BR with same script . 

 

 

 

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Not working for Atfer insert BR.


Please mark this response as correct or helpful if it assisted you with your question.

Ankur Bawiskar
Tera Patron

@Akshay Pithore 

what's the business requirement to copy tags?

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader