Script behind attachment ui page

xiaix
Tera Guru

I'm a bit baffled as to where the code-behind is for the attachment ui page when initiated.

I found the Script Include, "AttachmentAjax", that handles the actual updating and processing of an attachment.   I've scoured through and tested each function of the Client Script section of the attachment UI page, but I just don't see where the call is to first query the sys_attachment table.

Visual Example:

I have a link on a UI page:

find_real_file.png

The link calls this function:

find_real_file.png

The "attachment" UI Page came built in with ServiceNow (Eureka).   Like I've mentioned though, I've gone over the attachment ui page line by line and I don't see any ajax calls or any functions that, upon loading, go and get current attachments, but it does.   Where is this code?

1 ACCEPTED SOLUTION

coryseering
ServiceNow Employee
ServiceNow Employee

Hi David,



Far down in the code you will find a line like this:


<g2:attachment_list sys_id="${jvar_target_sys_id}" table="${jvar_target_table}">



That calls an extension to Jelly which takes as input the sys_id and table of the record that you're on when you open the dialog. It calls   new SysAttachment();


sa.getAttachments(table, sys_id);



That gets Attachments for all of the records for that given table, and puts it into a variable that is used in the next line of that page:


<g2:for_each_record file_variable="sys_attachment" var="attachment">



This loops over all of those attachments and outputs the HTML you see below *that*- an input button, and then a UI Macro (attachment_entry).



I hope that's helpful


Cory


View solution in original post

10 REPLIES 10

coryseering
ServiceNow Employee
ServiceNow Employee

You're welcome. I wish I could think of an easy way to accomplish your goal.


Well..., when it comes to code, I never accept defeat.   I've figured out a solution that works quite well.



On my custom UI page where a user uploads the images, I have the following HTML <a> links:


<a id="attachmentLink1" style="display:none;" onclick="saveMyAttachments('${bcp.sys_id}', '1')">


  <img width="16" height="16" border="0" src="images/icons/attachment.gifx" />


      Upload/Edit image of Floor Map


</a>


<a id="attachmentLink2" style="display:none;" onclick="saveMyAttachments('${bcp.sys_id}', '2')">


  <img width="16" height="16" border="0" src="images/icons/attachment.gifx" />


      Upload/Edit image of External Designated Meeting Area


</a>



The saveMyAttachments() function in the Client Script section is as follows:


saveMyAttachments


function saveMyAttachments(sys_id, whichImg)


{


  var newSysID = sys_id;


  newSysID = newSysID.slice(0, -2);



  if (whichImg == "1")


  {


        newSysID = newSysID + "_A";


  }


  else if (whichImg == "2")


  {


        newSysID = newSysID + "_B";


  }



  var g_dialog = new GlideDialogWindow('bcp_attachment');


  g_dialog.setTitle(getMessage('Attachments'));


  g_dialog.setPreference('target_table', 'sys_attachment');


  g_dialog.setPreference('target_sys_id', newSysID);


  g_dialog.setPreference('target_whichMap', whichImg);


  g_dialog.setPreference('attachment_disabled',


  (window["AttachmentUploader"] ?   AttachmentUploader.isAttachmentDisabled() : "false"));


  g_dialog.on("closeconfirm", _saveAttachmentConfirm);


  g_dialog.render();


}




Now... to display the images in a controlled manner, i have the following code:


<g:evaluate>


  gs.getSession().setStrictQuery(true);


  var bcp = new GlideRecord('u_bcp_sites');


  bcp.addQuery('u_erc_author', '${jvar_userID}');


  bcp.query();


</g:evaluate>




<j:if test="${bcp.next()}">




  <g:evaluate>


        var img1SysID = '${bcp.sys_id}';


        img1SysID = img1SysID.slice(0, -2);


        img1SysID = img1SysID + "_A";


 


        var image1 = new GlideRecord('sys_attachment');


        image1.addQuery('table_sys_id', img1SysID);


        image1.query();


  </g:evaluate>


  <g:evaluate>


        var img2SysID = '${bcp.sys_id}';


        img2SysID = img2SysID.slice(0, -2);


        img2SysID = img2SysID + "_B";



        var image2 = new GlideRecord('sys_attachment');


        image2.addQuery('table_sys_id', img2SysID);


        image2.query();


  </g:evaluate>




  <j:if test="${image1.next()}">


  <j:set var="jvar_f_map_SysID" value="${image1.sys_id}" />


  </j:if>


  <j:if test="${image2.next()}">


  <j:set var="jvar_ma_map_SysID" value="${image2.sys_id}" />


  </j:if>



</j:if>




<table>


  <tr>


  <td><img src="https://mydomain.service-now.com/ess/sys_attachment.do?view=true&amp;sys_id=${jvar_f_map_SysID}" /></td>


  </tr>


  <tr>


  <td><img src="https://mydomain.service-now.com/ess/sys_attachment.do?view=true&amp;sys_id=${jvar_ma_map_SysID}" /></td>


  </tr>


</table>












This has been tested and works just like I needed it to.


arnabdash
Mega Contributor

Hey David,



Great work again.


I have a similar requirement. I need to upload an excel from a UI page and attach it to a form. Moreover, the uploaded excel has to be parsed. The data obtained from the parsed excel will then be used to fill the variables of the form.



I was able to parse the Excel using your method from this page: Parse Excel (xlsx) file.. with ease!



Now please help me to find a way to get this attachment from UI page to be available in form.



Any suggestion is appreciated.



Regards,


Arnab


arnabdash
Mega Contributor

In the example you have shown in the link, can you point me to the part where you have the attchments moved from ui page to ui form?