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 01:25 PM
Images a treated a little differently. The best way to copy the image from one record another is
GlideSysAttachment.copy('source_table', 'source_sys_id', 'target_table', 'target_sys_id');
The key to note with image fields, is that the table is ZZ_YYtablename. For example the image field on my compamy record would look like this in sys_attachment:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2017 01:59 PM
Sheesh... so maybe im over complicating it now. i did try sticking that in the script include, though, to begin with and i got nothing in return. I also did notice the zz_yy preface. I guess I'm not quite back at square one but somewhere in the square 13 zone...
Should i be changing my script include to look something like?:
var GetImage = Class.create();
GetImage.prototype = {
initialize: function() {
},
getImage : function(sysparm_product,sysparm_record) {
GlideSysAttachment.copy('ZZ_YYu_products', sysparm_product, 'u_order_table', sysparm_record);
},
type: 'GetImage'
};
And should i leave my client script the same?
I did give it a go with no result.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2017 08:28 PM
This doesn't look like a client callable Ajax script. When you start a new script include and check the Client Callable checkbox, you'll get a different template in the script field something like this:
var MyScriptInclude = Class.create();
MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
type: 'MyScriptInclude'
});
Your method will get parameters from the client script, not directly as passed to the method (see the example in the GlideAjax wiki page.)
Your target table will also need to start with ZZ_YY since it's also an image field, right?
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
‎08-01-2017 06:02 AM
Ugh, you're right. I must have scripted before I marked client callable as true. This is what it looks like now...
var GetImage2 = Class.create();
GetImage2.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getImage2:function(sysparm_product,sysparm_record){
GlideSysAttachment.copy('ZZ_YYu_products', sysparm_product, 'u_order_table', sysparm_record);
},
type: 'GetImage2'
});
And yes, it is an image so my table begins with ZZ_YY and is pushing it to the record on u_order_table.
I did catch a mistake in my client script so I changed that line too. This is where I am getting the values for sysparm_product and sysparm_record.
This still isn't working but I feel like it should now. I also made sure that product and record were returning the right values. Its like they aren't being pushed to the script include correctly, or its not being called back correctly. What am I missing?
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var product = newValue;
var record = g_form.getUniqueValue();
var ga = new GlideAjax('GetImage2');
ga.addParam('sysparm_name', 'getImage2');
ga.addParam('sysparm_product', product);
ga.addParam('sysparm_record', record);
ga.getXML(GetImageParse);
function GetImageParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
}
}
Again, thank you for your assistance so far, it really has been helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2017 06:44 AM
I have also tried this for my client script and it didn't work:
var GetImage2 = Class.create();
GetImage2.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getImage2:function(){
var product = this.getParameter('sysparm_product');
var record = this.getParameter('sysparm_record');
GlideSysAttachment.copy('ZZ_YYu_product', product, 'u_order_table', record);
},
type: 'GetImage2'
});