유용한 첨부 파일 스크립트
유용한 첨부 파일 스크립트의 검색 가능한 버전입니다.
경고:
여기서 설명하는 사용자 지정은 특정 인스턴스에서 사용하도록 개발되었으며, Now Support에서 지원되지 않습니다. 이 메서드는 있는 그대로 제공되며 구현 전에 철저히 테스트되어야 합니다. 이 사용자 지정에 대한 모든 질문과 의견을 커뮤니티 포럼에 게시합니다.
기록에서 기록으로 첨부 파일 복사
한 기록에서 다른 기록으로 첨부 파일을 복사하려면 다음 스크립트를 사용하십시오.
GlideSysAttachment.copy('sourcetable','sys_id','destinationtable','sys_id');
주:
GlideSysAttachment.copy는 모든 첨부 파일을 복사합니다. 특정 첨부 파일을 선택할 수 없습니다.
중복 첨부 파일 삭제
비즈니스 규칙, 예약된 작업 또는 백그라운드 스크립트에서 다음 스크립트를 사용하여 첨부 파일 [sys_attachment] 테이블에 있는 중복 첨부 파일을 삭제합니다.
function fixDuplicateImages(){
var now_GR = new GlideRecord('sys_attachment');
now_GR.addQuery('table_name','LIKE','ZZ_YY%');
now_GR.orderBy('table_sys_id');
now_GR.orderByDesc('sys_created_on');
now_GR.query();
var lastID ='not_a_match';
var lastFile ='not_a_match';
while(now_GR.next()){
var isDuplicate = (lastID == now_GR.table_sys_id)&&(lastFile == now_GR.file_name);
lastID = now_GR.table_sys_id;
lastFile = now_GR.file_name;
gs.print(now_GR.table_sys_id + ' ' + now_GR.table_name + ' ' + now_GR.file_name + ' ' + now_GR.sys_created_on + ' ' + isDuplicate);
if(isDuplicate)
now_GR.deleteRecord();
}
}목록 뷰에 작업에 첨부 파일이 있는지 여부 표시
비즈니스 규칙에서 다음 스크립트를 사용하여 기록 목록 뷰에서 작업을 볼 때 작업에 첨부 파일이 있는지 여부를 표시합니다. 스크립트에는 첨부 파일 있음[u_has_attachments] 테이블에 사용자 지정 필드가 필요합니다.
checkAttachment();
function checkAttachment(){
// if inserting then the task has an attachment
if(current.operation()=='insert') {
hasAttachment('true'); // if deleting attachment check for other attachments
if(current.operation()=='delete') {
var timeNow3 =new GlideDateTime();
gs.log('has_attachment br: gliderecord query start date time is: '+ timeNow3.getNumericValue(),'jwtest');
var attachCount = new GlideAggregate('sys_attachment');
attachCount.addQuery('table_sys_id',current.sys_id);
attachCount.addAggregate('COUNT');
attachCount.query();
var numAttachments ='0';
// if no other attachments task does not have attachment
if(attachCount.next()){
numAttachments = attachCount.getAggregate("COUNT");
if(numAttachments >0){
hasAttachment ='true';
} else {
hasAttachment('false');
}
var timeNow4=new GlideDateTime();
gs.log('has_attachment br: gliderecord query start date time is: '+ timeNow4.getNumericValue(),'jwtest');
}
function hasAttachment(answer){
var task = new GlideRecord('task');
task.addQuery('sys_id',current.table_sys_id);
task.query();
if(task.next()){
task.u_has_attachment= answer;
task.autoSysFields(false); //Don't set the lastUpdatedTime or the Simultaneous Update Alert will likely get triggered
task.setWorkflow(false); //Don't allow other business rules to run, otherwise multiple notifications will likely be sent
task.update();
}
}
주:
삽입/삭제 후 실행될 비즈니스 규칙을 예약합니다.
이메일 알림의 첨부 파일에 링크
이메일 알림 또는 템플릿에 다음 스크립트를 사용하여 첨부 파일에 대한 링크를 포함합니다.
printattachments();
function printattachments(){
var now_GR =new GlideRecord('sys_attachment');
now_GR.addQuery('table_sys_id',current.sys_id);
now_GR.query();
while(now_GR.next()){
template.print('Attachment: <a href="http://'+gs.getProperty("instance_name")+'.service-now.com/sys_attachment.do?sys_id='+ now_GR.sys_id+'">'+ now_GR.file_name+'</a>');
}
}
주:
"instance_name"를 인스턴스 이름으로 바꿉니다.
첨부 파일 로깅
사용자가 첨부 파일을 다운로드할 때마다 이 작업은 attachment.read 이벤트 기록을 이벤트 로그에 기록합니다. 원하는 경우 스크립트 작업 또는 이메일 알림으로 이러한 이벤트를 처리할 수 있습니다. 이 기능은 첨부 파일을 읽을 때 어떤 작업을 수행하려는 경우에 유용할 수 있습니다. 예를 들어 특정 첨부 파일을 다운로드한 시기와 사용자를 기록할 수 있습니다. 이 기능의 경우 현재 변수는 sys_attachment 기록을 가리켜야 하며 이벤트 기록은 다음 매개변수를 사용해야 합니다.
- parm1 : 파일 이름
- parm2: 테이블 이름