The CreatorCon Call for Content is officially open! Get started here.

Copy attachment with Client Script

alexcharleswort
Tera Expert

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?

12 REPLIES 12

Hi Alex,



Not to be pedantic, but did you mean this is your script include in the previous comment? Did you check sys_attachment for any new records copied/created? If you're using an image field, you need to make the target table start with ZZ_YY also. Something like this.



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, 'ZZ_YYu_order_table', record);  


},  


 


      type: 'GetImage2'  


});  



To ensure it's working, place some gs.log() statements in the getImage2 function and inspect the log. One at the beginning to indicate it's started. One just before the copy to print out the product and record variables would be helpful as well. Those should be sys_id values.


Ah okay, so this is kind of working now. I changed the table name so now when I change the value in products field the new image won't actually display until I click save on the record. Is this how it is supposed to be working or is there a way to get it to change immediately after the value changes??



Also put the logs in and watch and they were running correctly and returning the right sys_ids and it looks like they were changing at the time that I edited the field, so before I clicked save.


I tried adding g_form.save(); after the callback function which made it work about 50% of the time. I assume that means that the query and copy wasn't finished running before it was saved for those that didn't work. I did try to manipulate the client script to make it more of a synchronous process but that didn't seem to help much:


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.getXMLWait();
ga.getXMLAnswer();


}


I guess I would have figured with asynchronous, the script would have caught up while the user was filling out the form or it wouldn't take that long to complete, not that you actually had to click save on the record for the changes to take effect?