Get value of attachment from Record producer in record producer script

Utpal Dutta
Tera Guru

Hi community members,

I'm working on a record producer where I need to get value of "Attachment" in my record producer script.

UtpalDutta_0-1753164768441.png

 

I need this because I'm aboring action of record producer and inserting that record somewhere else. When I'm inserting that record I need the attachment which is added by the user in Record producer to be attached in that record.

 

Any ideas or suggestions are helpful.

 

Thanks

 

1 ACCEPTED SOLUTION

ChiranjeeviR
Kilo Sage

Hi @Utpal Dutta,

 

step1: Open your record producer

specify record producer if you are working on (eg.issue management- Record Producer)

 

step2. add the script to the Record producer

Here is the script to copy attachments in Record Producer

var newRec = new GlideRecord 'incident);//or any other table
newRec.short-description = 'created from Record producer';
newRec.description ='This record was created through a producer';
newRec.insert();


//Now copy attachments from the record producer to the new record

var sa - new GlideSysAttachment();
sa.copy('sc_item_produced_record',current.sys_id,newRec.getTableName(), newRec.sys_id);
//adjust source table name in teh .copy() line if needed

 

use 'sc_item_produced_record' if the attachement is saved there

 

step3: save the Record Producer

 

Testing:

1.Service catalog-> record producer and fill the form and attach a file -> submit

 2. go to the target table (eg.incident in this example )

3. open a new record and check if the attachment is present.

 

Please mark as Correct Answer/Helpful, if applicable.

ServiceNow Developer

Thanks & Regards,
Chiranjeevi
ServiceNow Developer | | ITSM | | ServiceNow Discovery | | Event Management | | Service Mapping | | CMDB

Please mark as Correct Answer/Helpful, if applicable.

View solution in original post

8 REPLIES 8

anshul_goyal
Kilo Sage

Hello @Utpal Dutta,

In which record you want to add that attachment? Selected record producer table or somewhere else?

Thanks

Ankur Bawiskar
Tera Patron
Tera Patron

@Utpal Dutta 

what do you want to do with that attachment?

what's your business requirement?

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

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

Chaitanya ILCR
Kilo Patron

Hi @Utpal Dutta ,

 

when you add an attachment to the record producer it will add it to sys_attachment table with table name as the record producer tableName and sysid = sysid of the task which is not yet generated ( this sysid will be allocated to to the record once you submit the record)

ChaitanyaILCR_0-1753165324523.png

 

it's not recommend to use the setAbortaction method inside record producer script and might not work as intended as per docs 

 

so better create a before insert BR on the table 

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

Shraddha Kadam
Mega Sage

Hello @Utpal Dutta ,

 

In addition to @Chaitanya ILCR point, you can copy the attachment from one table to another table using Glidesysattachment method.

 

// For example

var attachment = new GlideSysAttachment();
var incidentSysID = 'ab1b30031b04ec101363ff37dc4bcbfc';
var incGR = new GlideRecord('incident');
incGR.get(incidentSysID);

var copiedAttachments = attachment.copy('incident', incidentSysID, 'problem', incGR.getValue('problem_id'));
gs.info('Copied attachments: ' + copiedAttachments);
If my response was helpful, please mark it as correct and helpful.
Thank you.