I want to upload attachments through transform maps. Can anybody help??

geetikagarg
Tera Contributor

I want to upload data in contract table and the records contain attachment  also. Can any body help how to upload the attachment data.I am getting the data through excel sheet.

1 ACCEPTED SOLUTION

Then check this blog where I have recently shared the approach

is it possible to send attachment through transform map?

Tell customer to send all these details in the excel file

1) the base64encoded data of the attachment

2) file name

3) content type

once you are sure these all data is present in excel you can use onAfter transform script so that it adds attachment to that record which got created/updated

Like this

find_real_file.png

OnAfter script would be like this

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
	// Add your code here
	var fileName = source.u_file_name;
	var fileContentType = source.u_content_type; // such as image/jpeg or application/pdf
	var fileData = source.u_base64_data;

	var ecc = new GlideRecord('ecc_queue');
	ecc.initialize();
	ecc.agent = "AttachmentCreator";
	ecc.topic = "AttachmentCreator";
	ecc.name = fileName + ":" + fileContentType;
	ecc.source = target.sys_class_name + ":" + target.sys_id;
	ecc.payload = fileData;
	ecc.insert();

})(source, map, log, target);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

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

View solution in original post

9 REPLIES 9

How does mapping work? I have the same problem but I am confused on what fields to map it with. 
find_real_file.png

Should I map the fields like this? 
Or is it like this?

find_real_file.png

 

Direct mapping won't work.

You will have to use onBefore transform script.

You should have all the other details as well like filename, content type

Regards
Ankur

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

Hi Ankur, how are you? I was having the same problem and decided to use your script but for some reason the attachment (image, excel, etc...) is created in servicenow but when I download it the attachment is corrupted. Do you wonder what it could be? Here are the inputs that I used in excel

Jo_oPedro_0-1669831217150.png

And here is the attachment in servicenow 

Jo_oPedro_1-1669831391195.png

 

 

Hi Ankur,

 

Thanks for the above. 

 

For update scenario - where I have a set of numbers / sys ids already in the excel as a column.

In the script of transform map, I check if that number/sys id already exists then I update a particular field., which is happening properly but the attachment is sitting on a new insertion (that onafter script as suggested by you) instead of updating on the existing record that I checked in the transform script.

 

What am i missing here, need your help. Thanks in advance.

Harikrishna4
Tera Contributor

Hi @Ankur Bawiskar ,

 

When I'm trying to do this activity. I'm getting the below error when I try to preview the attached attachment.

Can you help me to overcome this?

Harikrishna4_0-1724223130941.png

 

 

Thanks,
Hari