Dynamic attachment on Catalog Item from a record

Patrik Z
Giga Guru

Hello Community,

a client has a specific requirement to attach a file from a record to a catalog item.

The way it's supposed to work is that there is a UI Action that redirects you to the catalog item and when you get there, the attachment from the record is supposed to be there. 

The cat. item has a reference field to the record but I just don't know how to do it since the cat. item is not a created record yet so GlideSysAttachment.copy will probably not work.

Is there a way to do it dynamically like that?

Thanks
Patrik

 

3 REPLIES 3

Sagar Pagar
Tera Patron

Hi Patrik,

Would you please explain a bit from which records you want to copy attachment?

 

Thanks,

Sagar Pagar

The world works with ServiceNow

David36
Tera Contributor

Was any solution found for this request?

simonezini
Mega Sage

Hi @Patrik Z ,

from the UI Action you could redirect to the catalog item page and add to the url a parameter, for example the sys_id of the attachment you want to be in the catalog item.

Then, with an onLoad script you could retrieve that parameter from the URL and set it to a variable of type Attachment (it will take the sys_id of an attachment as value). In this way you could set a dynamic attachment on the catalog item.

 

Here's an example of the Catalog Item onLoad script:

function onLoad() {

	//Pre-load values from URL if available
	var url = top.location.href;
	var attachmentSysId = new URLSearchParams(url).get("sysparm_att");

	if (attachmentSysId) {
		g_form.setValue('var_myattachment', attachmentSysId);
	}

}

 

Here, var_myattachment is your attachment type variable on catalog item, and sysparm_att is the parameter you need to pass in the URL; so the URL would be like this: "https://myinstance.service-now.com/sp?id=sc_cat_item&sys_id=XXXXXXXXXX&sysparm_att=abcdef1234567890".

 

Let me know if this helped!

 

Regards

Simone