Updating an attachment in a previously created record from a record producer

Mike S3
Giga Expert

In my record producer , I  load values  from a previously created record based on user selection so they can use the record producer to update those values  . I have an attachment field called u_assigned_lead, on submit I would like to update this field on the record to whatever the user attached .  Every other field is updated correctly , except the file attachment . This is the script that I'm currently using: 

 

var record = current.u_select_review_1_strategic_alignment;

var review = new GlideRecord('u_earb');

review.addQuery('sys_id', record);

review.query();

//if we find a record update it

if (review.next()) {

review.u_intake_demand_number = current.u_intake_demand_number;
review.u_assigned_lead = current.u_assigned_lead;
review.update();
1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

It's necessary to query the sys_attachment table to get the sys_id of the attached file.

var grAttach = new GlideRecord('sys_attachment');
grAttach.addQuery('table_name', '<u_table name>');  // table name is producer. 'ZZ_YY' prefix implies attachment field name so don't use to get attachment to the form
grAttach.addQuery('table_sys_id', current.sys_id);
grAttach.query();
if(grAttach.next()){
  review.u_intake_demand_number = current.u_intake_demand_number;
  review.u_assigned_lead = grAttach.sys_id;  // set attachment field to attachment to form
  review.update();
}

View solution in original post

2 REPLIES 2

harshav
Tera Guru

Hi Mike,

Attachments are tracked in different way when we attach to a field in ServiceNow.

Say for example i have an attachment field in ServiceNow Incident table called "attach_here" it will be stored in ServiceNow Attachment table with table name as "ZZ_YYattach_here" and the sys_id of the record you are submitting.

Before you do any sort of linkage just visit the sys_attachment table and see how it was stored, and kindly update the reference in "sys_attachment" table by writing a business rule on the table your Record Producer is used for.

Thanks
Sreeharsha

Hitoshi Ozawa
Giga Sage
Giga Sage

It's necessary to query the sys_attachment table to get the sys_id of the attached file.

var grAttach = new GlideRecord('sys_attachment');
grAttach.addQuery('table_name', '<u_table name>');  // table name is producer. 'ZZ_YY' prefix implies attachment field name so don't use to get attachment to the form
grAttach.addQuery('table_sys_id', current.sys_id);
grAttach.query();
if(grAttach.next()){
  review.u_intake_demand_number = current.u_intake_demand_number;
  review.u_assigned_lead = grAttach.sys_id;  // set attachment field to attachment to form
  review.update();
}