Copy attachment with Client Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2017 06:36 AM
Hi!
I have a need to be able to populate an image field when a reference field is populated/changed. I looked at this How to add at default image to an image field on the Incident form. and that works awesome as a business rule, but I need this in a client script. If u_product (a reference field) changes, I want it to pull the image attached to that value in the reference field and populate on this new record. The records on this table will only have that one attachment.
Anyone have any ideas?
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2017 06:43 AM
Hi Alex,
Your client script is going to leverage the server at some point to make the copy happen. Your client can only do things related to the browser and the information (HTML, CSS, Javascript) that was passed to it. In order to make an image field populated, you need to have an entry in the sys_attachment table that corresponds to that record (using the table and table_sys_id fields from sys_attachment.) This means database access, which means server side code.
Take a look at GlideAjax and you'll start to see how you can incorporate some of the elements of that business rule you had in a script include to get the job done.
Docs: Client Scripts
Docs: GlideForm
Docs: GlideAjax
Client Script Best Practices - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2017 07:18 AM
That makes complete sense. Any ideas on what the script for both the script include and client script would look like? I have only ever written one, and I'm still not convinced it's functioning like it should be.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2017 07:21 AM
Take a look at Episodes 5, 6, and 33 of TechNow. We mention or demonstrate all these bits in those videos.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2017 10:29 AM
z
Hey Chuck,
So those episodes were extremely helpful and made me understand this a lot more. I'm also going to have to go back and watch a few more episodes that I saw in there.
I think that I understand the logic now, I'm just having a hard time pulling the scripts altogether to make one cohesive unit. I think my main issue is that its trying to get that attachment and not just a variable on the form.
Do you mind taking a look at what I have thus far and seeing where I'm going wrong?
The method that I am using right now is instead of querying my u_products table (which is the reference table for the reference variable), I am querying the attachment table and filtering by the table sys id (the record on my u_products table that contains the one attachment). Then I am returning the sys_id. On the client side I am pushing it to u_image... which I don't think you can do, but I'm not really sure how to grab that attachment and put it on this new record...
---------------------------------------------------------------------------------------------------------------------------
Client Script
onChange of u_product
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}//Type appropriate comment here, and begin script below
var product = newValue;
var ga = new GlideAjax('GetImage');
ga.addParam('sysparm_name', 'getImage');
ga.addParam('sysparm_product', product);
ga.getXML(stateCallback);
function stateCallback(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_image', answer);
}
}
------------------------------------------------------------------------------------------------------------------------
Script Include
client callable = true
var GetImage = Class.create();
GetImage.prototype = {
initialize: function() {
},
getImage : function() {
var product = this.getParameter('sysparm_product');
var tableName = 'sys_attachment';
var image = new GlideRecord(tableName);
image.addQuery('table_sys_id', product);
image.query();
if (image.next()){
return image.getValue();
}
},
type: 'GetImage'
};