Update Image to Table using Record Producer

natchen
Tera Contributor

Hi all, I’m having trouble with a Record Producer in ServiceNow. I'm trying to update an existing record in the table A with both a string field and a image field. However, the string gets updated but the the image is not updating in the existing record. A new record is created with both the string and image being created.

Here’s what I’m trying to do:

  • Prevent a new record from being created by using setAbortAction(current).

  • Update the existing record with the string and image fields from the Record Producer

 

this is the function in script include

 updateJoinerWithIntroAndPhoto: function(userSysId, selfIntro, sourceTable, sourceSysId) {
        var joinerGR = new GlideRecord('new_joiner');
        joinerGR.addQuery('user', userSysId); 
        joinerGR.query();

        if (joinerGR.next()) {
            if (selfIntro)
                joinerGR.setValue('self_introduction', selfIntro);
 
            var attachmentGR = new GlideRecord('sys_attachment');
            attachmentGR.addQuery('table_sys_id', sourceSysId);
            attachmentGR.addQuery('table_name', sourceTable);
            attachmentGR.orderByDesc('sys_created_on');
            attachmentGR.query();

if (attachmentGR.next()) {
    var newAttachmentSysId = GlideSysAttachment.copy(
        sourceTable,
        sourceSysId,
        'new_joiner',
        joinerGR.getUniqueValue()
    );

    if (newAttachmentSysId) {
        joinerGR.setValue('photo', newAttachmentSysId);
    }
}
            joinerGR.update();
            return true;
        }
    },

this is the record producer script
var utils = new new_joine_0.NewJoinerOnboardingUtils();
var selectedUserSysId = producer.new_joiner.toString(); 
var selfIntro = producer.self_introduction.toString();   
var result = utils.updateJoinerWithIntroAndPhoto(
    selectedUserSysId,
    selfIntro,
    current.getTableName(),  
    current.getUniqueValue() 
);
utils.setAbortAction(current);
 
4 REPLIES 4

Murthy Ch
Giga Sage

@natchen 

If you are using "image" type field you need to understand few things before updating the image via script:
When you create it for the first time via producer that image will never be deleted until you deletes which means if you update it a new image will be inserted but old image will stays in the same record.

So, think and write the logic, lmk if you need help to building this.

Thanks,
Murthy

Hi @Murthy Ch, the image variable will be originally empty. User will upload the the image through record producer and the image field will only be updated by then.

Ankur Bawiskar
Tera Patron
Tera Patron

@natchen 

the 2nd variable is of type "Attachment"?

Share screenshots of your variable config

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

yes the 2nd variable in the record producer is type Attachment. In the table, the 2nd variable will be updated as Image type.