Mandatory Attachments Order Guide and Catalog Items sharing solution

jeff_lord
Giga Contributor

To start I'm pretty new to ServiceNow. Only been working with it for about 3 months.
I had some requests to make attachments mandatory on catalog items.
When I added that request to an order guide it broke and wouldn't check for an attachment or submit.
I tried many different options and variations and finally go one to work and just wanted to share the solution that works.

I used the many articles on the forums for this solution. 🙂 So i have to say thank you to everyone.

The pieces:
On your catalog item (Requests or Record Producers)
After all your other variables create on called "u_order_guide" or whatever you wish (but make sure you modify all to match)
This variable must be a Yes/No and have the default value set to No.
Create a UI Policy that hides this variable onLoad.
Create a client script as follows:



/*
For the Client Script on the request item/Record Producer
Name: Mandatory Attachments
Applies to: A Catalog Item
Type: OnSubmit
*/

function onSubmit() {
if (g_form.getValue('u_order_guide') == 'Yes') {
var ord_id = gel('sysparm_cart_edit').value;
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", ord_id);
gr.query();
if (!gr.next()) {
alert("You must attach a file to submit.");
return false;
}
}
else if (g_form.getValue('u_order_guide') == 'No') {
var cat_id = gel('sysparm_item_guid').value;
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id);
gr.query();
if (!gr.next()) {
alert("You must attach a file to submit.");
return false;
}
}
}


Then on your Order Guide:
Make sure that Cascading Variables is checked or this will not work.
Create the same exact variable after all your other variables called "u_order_guide" and make it a Yes/No but make the default value be 'Yes'

Then create a Catalog Client Script as follows:



/*
For the Catalog Client Script that applies to the Order Guide
Name: Hide Order Guide Variable
Applies to: A Catalog Item
Type: OnLoad
Catalog Item: YOUR ORDER GUIDE
Applies on: check all boxes you need
*/

function onLoad() {
var item = $("current_item");
var guide = $("sysparm_guide");
if (item != null && guide != null && item.value == guide.value)
g_form.setDisplay('u_order_guide', false);
}


If you have any suggestions or comments I am happy to refine my work.

20 REPLIES 20

Jeff Lord
Tera Contributor

Yes the correct answer on that question will work. If it doesn't then I would need to see the code to help further.



Jeff


Hi Jeff,



Could u please help me with your script, it is not working for me.



find_real_file.png


Jeff Lord
Tera Contributor

Hello Shikha,



You need to change the && to be && only for it to work.



Let me know how that works.



Jeff


Hi Jeff,



Thanks for Reply,,, i found that error that day only and forgot to update this comment.



I need some other help if you can,



I am making attachment mandatory on order guide item in Scoped application.


As using Gliderecord client side is not a best practice also it doesnot work in scoped application so I have written a Script Include and a catalog client script but it is not working.



SCRIPT INCLUDE:


var CheckAttachmentMandat   = Class.create();


CheckAttachmentMandat.prototype = {


  initialize: function() {


      },


  checkFile: function() {


  var id = this.getParameter('sysparm_sysid');


  var answer= 'false';



  //var u = gs.getUserID();


  var attachment = new GlideRecord('sys_attachment');


  attachment.addQuery('table_name','sc_cart_item');


  attachment.addQuery('table_sys_id',id);


  //attachment.addQuery('sys_created_by', 'u');


  attachment.query();



  gs.addInfoMessage('shikha:' +attachment.getRowCount());


  /*


  if(attachment.next()){


  answer= 'true';


  }


  */


  if (!attachment.next()) {


  answer='true';


  }


  return answer;


  },


  type: 'CheckAttachmentMandat'


};



Catalog Client Script: On Submit



function onSubmit() {


  alert("Hello");


  var sys_id =   g_form.getUniqueValue();


  //alert(sys_id);


  //var trackchanges = g_form.getValue('track_changes');


  //alert(trackchanges);



  //if(trackchanges == 'Yes'){


  //var ord_id = gel('sysparm_cart_edit').value;


  alert('Order id:'+ sys_id);


  var temp = '123';


  var ga = new GlideAjax('CheckAttachmentMandat');


  ga.addParam('sysparm_name','checkFile');


  ga.addParam("sysparm_sysid",sys_id);


  ga.getXML(function (response) { callback (response, temp); });


  /*


  var answer = ga.getAnswer();


  //alert(answer);


  if(answer == 'true') {


  alert("Please attach atleast one attachment to proceed");


  return false;


  }


  }


  else if(trackchanges == 'No'){


  alert("Please use track changes first.");


  return false;


  }


  */


}


/*function getResponse(response){


  var answer = response.responseXML.documentElement.getAttribute("answer");


  if(answer == 'true') {


  alert("Please attach atleast one attachment to proceed");


  return false;


  }*/


  function callBack (response, temp){


  alert(temp);



  }



I found on wiki that getXMLWait doesnot work in scoped application:


find_real_file.png



So i found an alternative for this function as follows:



Scoped Applications and Client Scripts: A Primer



find_real_file.png



But i am not able to achieve it as may be i am doing some mistake in Script Include.




Now if i talk about Global Application, then i have written a script Include and Catalog client script for the same order guide and it is working fine. I am sharing steps as follows:


1)I create same order guide in Global application which i created in scoped app.


2)Then i created a same item in global application.


3)Created a Script Include as follows:



var CheckAttachment   = Class.create();


CheckAttachment .prototype = Object.extendsObject(AbstractAjaxProcessor, {


  check: function() {


  var id = this.getParameter('sysparm_sysid');


  var answer= 'false';



  //var u = gs.getUserID();


  var attachment = new GlideRecord('sys_attachment');


  attachment.addQuery('table_name','sc_cart_item');


  attachment.addQuery('table_sys_id',id);


  //attachment.addQuery('sys_created_by', 'u');


  attachment.query();


  /*


  gs.addInfoMessage('shikha:' +attachment);


  if(attachment.next()){


  answer= 'true';


  }


  */


  if (!attachment.next()) {


  answer='true';


  }


  return answer;


  },


  type: 'CheckAttachment'


});



4) Created a catalog client script as follows:



function onSubmit() {


  //alert("Hello");


  //var sys_id =   g_form.getUniqueValue();


  //alert(sys_id);


  var trackchanges = g_form.getValue('track_changes');


  alert(trackchanges);



  if(trackchanges == 'Yes'){


  var ord_id = gel('sysparm_cart_edit').value;  


  var ga = new GlideAjax('CheckAttachment');


  ga.addParam('sysparm_name','check');


  ga.addParam("sysparm_sysid",ord_id);


  ga.getXMLWait();



  var answer = ga.getAnswer();


  //alert(answer);


  if(answer == 'true') {


  alert("Please attach atleast one attachment to proceed");


  return false;


  }


  }


  else if(trackchanges == 'No'){


  alert("Please use track changes first.");


  return false;


  }


}



And both script include and catalog client script are working fine in global application.



Can you help me to work same in scoped app??



Regards,


Shikha Khanna


Jeff Lord
Tera Contributor

Hello Shikha,



I will work it out in my Dev instance and get back to you. I have not done it in a scoped app.



Jeff