GlideSysAttachment().copy Not copying the attachment - HR Scoped application

Mrman
Tera Guru

Hi Team,

I have created a after insert BR to insert a HR task and then copy a specific attachment from a Case to HR task .

The attachment is not getting copied to HR task. Please suggest 

All the log messages are getting triggered correctly , but the attachment is not found on target record.

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

    gs.info("the BR triggerred for status");

    var grCattch = new GlideRecord('sys_attachment');
    grCattch.addQuery('table_name', 'sn_hr_core_case_workforce_admin');
    grCattch.addQuery('table_sys_id', current.sys_id);
	grCattch.addEncodedQuery('file_nameSTARTSWITHStatus Record Certificate');
    grCattch.query();

    if (grCattch.next()) {
		

        var grCmnl = new GlideRecord('sn_hr_core_task');
        grCmnl.initialize();
        grCmnl.parent = current.sys_id;
        grCmnl.assignment_group = '3506330cdbc92054e803f3551d9619b0';
        grCmnl.state = '10';
        grCmnl.short_description = "Validate Status Record";
        grCmnl.insert();
		

gs.info("the record status is found " + 'attchsysid----' + grCattch.getUniqueValue() + 'attchtable---' + grCattch.getTableName() + 'tasktable----' + grCmnl.getTableName() + 'tasksysid---' + grCmnl.getUniqueValue());

		new GlideSysAttachment().copy(grCattch.getTableName(), grCattch.getUniqueValue(), grCmnl.getTableName(), grCmnl.getUniqueValue());
    }

 

22 REPLIES 22

Hi,

check the other link I mentioned

Regards
Ankur

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

@Ankur Bawiskar  Please let me know if I need to use below line , could you guide me where exactly i need to make changes

glideSysAttachmentRef.writeContentStream

Hi,

here is your updated script

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

	var grCattch = new GlideRecord('sys_attachment');
	grCattch.addQuery('table_name', 'sn_hr_core_case_workforce_admin');
	grCattch.addQuery('table_sys_id', current.sys_id);
	grCattch.addEncodedQuery('file_nameSTARTSWITHStatus Record Certificate');
	grCattch.query();
	if (grCattch.next()) {
		var grCmnl = new GlideRecord('sn_hr_core_task');
		grCmnl.initialize();
		grCmnl.parent = current.sys_id;
		grCmnl.assignment_group = '3506330cdbc92054e803f3551d9619b0';
		grCmnl.state = '10';
		grCmnl.short_description = "Validate Status Record";
		grCmnl.insert();

		createAttachment(current, grCmnl, grCattch);
	}

	function createAttachment(source, targetRef, sysAttRef) {

		try {
			var glideSysAttachmentRef = new GlideSysAttachment();
			var guid = "";
			guid = glideSysAttachmentRef.writeContentStream(targetRef, sysAttRef.getValue("file_name"), sysAttRef.getValue("content_type"), glideSysAttachmentRef.getContentStream(sysAttRef.getUniqueValue()));
			gs.info("createAttachment --> guid = " + guid, source);
		} catch (error) {
			gs.info("createAttachment --> error = " + error, source);
		}
	}

})(current, previous);

Regards
Ankur

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

@Ankur Bawiskar  i have tried this code but there is no attachment present on the target record . 

UUpon checking logs , below is found. Please suggest

find_real_file.png

B

BBelow is the script used .

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

   	var grCattch = new GlideRecord('sys_attachment');
	grCattch.addQuery('table_name', 'sn_hr_core_case_workforce_admin');
	grCattch.addQuery('table_sys_id', current.sys_id);
	grCattch.addEncodedQuery('file_nameSTARTSWITHStatus Record Certificate');
	grCattch.query();
	if (grCattch.next()) {
		var grCmnl = new GlideRecord('sn_hr_core_task');
		grCmnl.initialize();
		grCmnl.parent = current.sys_id;
		grCmnl.assignment_group = ''3506330cdbc92054e803f3551d9619b0';
		grCmnl.state = '10';
		grCmnl.short_description = "Validate Status Record";
		grCmnl.insert();

		createAttachment(current, grCmnl, grCattch);
	}

	function createAttachment(source, targetRef, sysAttRef) {

		try {
			var glideSysAttachmentRef = new GlideSysAttachment();
			var guid = "";
			guid = glideSysAttachmentRef.writeContentStream(targetRef, sysAttRef.getValue("file_name"), sysAttRef.getValue("content_type"), glideSysAttachmentRef.getContentStream(sysAttRef.getUniqueValue()));
			gs.info("createAttachment --> guid = " + guid, source);
		} catch (error) {
			gs.info("createAttachment --> error = " + error, source);
		}
	}



})(current, previous);

Hi,

I think this function only works in scope -> writeContentStream

Did you try to debug by adding gs.info() to check you are getting correct values

Another way is

1) get the bas64Encoded data and then use writeBase64() method

Regards
Ankur

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