Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Trying to get sys id from Attachemnt table sys id

sanjeet1245
Tera Guru
(function executeRule(current, previous) {
    try {
      


gs.info("-------------------------111111111111111111");
            var attachment = new GlideSysAttachment();
            var attachmentContent;
            gs.info("---------------------222222");
           var agr = new GlideRecord('sys_attachment');
		   agr.addQuery('table_name','incident');
		   agr.addQuery('table_sys_id',current.sys_id);
		   agr.query();
           gs.info("-----------------333");

            while (agr.next()) { 
			gs.info('Attachment sys_id:' + agr.getValue('sys_id'));
			
                gs.info('-------' + agr.getValue('file_name')); //print file name of attachment
                attachmentContent = attachment.getContent(agr);
                gs.info('------Attachment content: ' + attachmentContent); //print attachment content
			
            }
        }catch(e){gs.info('------------error in xml attachment '+e);}
})(current, previous);

  Hi, i am having an issue with my business rule. in my business rule on sys_attachment table on condition filename is anything . when i add and save any attachment in the incident form ,i want to see the details of the attachment table sys id in system log 

1 ACCEPTED SOLUTION

You can use the below code to get attachment details in system log whenever any new attachment is attached to any table:-

        var attachment = new GlideSysAttachment();
        var agr = attachment.getAttachments(current.table_name, current.table_sys_id);
        while (agr.next()) {
            gs.info(agr.getValue('file_name')); //print file name of attachment
            var attachmentContent = attachment.getContent(agr);
            gs.info('Attachment content: ' + attachmentContent); //print attachment content
            gs.info('Attachment content type' + agr.getValue('content_type'));
        }

View solution in original post

20 REPLIES 20

Uday Soni05
Tera Guru

Hi @sanjeet1245 ,

Are you running BR on sys_attachment Table
If yes
You will need to run it on incident table as sys_attachment record have field table sys_id which will store the sys id of incident and running on sys_attachment table will not give right sysid

Hope this helps you

Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Uday

can you explain more about this ? what changes i have to do on target table incident or attachment ?

@sanjeet1245  ,

Can you tell me a bit more about your requirement?

my business rule is  on sys_attachment table . my gs.log(-----------3) is getting printed on system log but after the while (agr.next())  loop part in not getting printed in system log .

my requirement is while loop part printed in the system log which i want to print but instead of printing output its show undefined or not print any message.

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


gs.info("-------------------------111111111111111111");
            var attachment = new GlideSysAttachment();
            var attachmentContent;
            gs.info("------------22222222222222222222222");
            var agr = attachment.getAttachments('incident',  current.sys_id); //create attachment GlideRecord
gs.info("---333333333333333333333333333333333333333333");

            while (agr.next()) { //for each attachment on the incident record
            gs.info('------------44444');
                gs.info(agr.getValue('file_name')); //print file name of attachment
             var attachmentContent = attachment.getContent(agr);
                gs.info('Attachment content: ' + attachmentContent); //print attachment content

		   
		    }

        }catch(e){gs.info('------------error in xml attachment '+e);}


        })(current, previous);