…thoughts on ServiceNow and digital transformation

Post

Attachments in ServiceNow Record Producer Mapped Attachment Variable Duplicated in Activity Log


If you have a record producer with a mapped attachment field, you (or perhaps your users) may have noticed that attachments uploaded to this variable appear in twice in the activity formatter. According to this knowledge article this is expected behavior. The attachment is uploaded first to the variable and then copied to the field to which the variable is mapped. In the sys_attachment table, there are actually two attachments.

Needless to say, this is very confusing for users and needlessly clogs up the Activity formatter and the database. Here’s a work around to get rid of the attachment on the variable, leaving the attachment in the field.

On the question_answer table, create a business rule to delete the attachment associated with the variable

Condition:

(current.question.map_to_field && (current.question.type == 33)) 

Script:

(function executeRule(current, previous /*null when async*/ ) {
    var oldAttachSysId = current.value;

    //delete the attachment 
    var attGR = new GlideRecord('sys_attachment');
    attGR.get(oldAttachSysId);
    attGR.deleteRecord();

})(current, previous);

Optionally, you can create another business rule to put the sys_id of the attachment into the variable. Note that this business rule is async and yes, it does current.update which is normally a no no, however I also do current.setWorkflow(false).

Condition:

(current.question.map_to_field && (current.question.type == 33)) 

Script:

(function executeRule(current, previous /*null when async*/ ) {


    //update the variable value with the sysid of the attachment in the field 
    var taskGR = new GlideRecord(current.question.record_producer_table);
    taskGR.get(current.table_sys_id);
    var newAttachSysId = taskGR.getValue(current.question.field);
    if (newAttachSysId) {
        current.setValue('value',newAttachSysId);
        current.setWorkflow(false);
        current.update();
    }


})(current, previous);