diff --git a/Client-Side Components/Client Scripts/Hide RelatedList based on Record/Readme.md b/Client-Side Components/Client Scripts/Hide RelatedList based on Record/Readme.md new file mode 100644 index 0000000000..1423a51fdb --- /dev/null +++ b/Client-Side Components/Client Scripts/Hide RelatedList based on Record/Readme.md @@ -0,0 +1,12 @@ +Hide Related List if there is no Record + +Script Type : Client Script, Type : OnLoad, Table: incident + +Script Type : Script Include, Glide AJAX enabled : True, Role : admin + +Goal : Hide the Related list on the form based on the record + +Walk through of code : So the Client Script will run on the Onload form so this will check whether the mentioned related list has record or not so based on that this will send the data to the Script include through Glide Ajax and then this Script include will Glide Record to the particular table name and then check whether record/records it there or not based on that this will return 'true' if record is no record else return 'false' means there is a record. + +![alt text](clientScript.png) +![alt text](scriptInclude.png) \ No newline at end of file diff --git a/Client-Side Components/Client Scripts/Hide RelatedList based on Record/clientScript.js b/Client-Side Components/Client Scripts/Hide RelatedList based on Record/clientScript.js new file mode 100644 index 0000000000..7415966499 --- /dev/null +++ b/Client-Side Components/Client Scripts/Hide RelatedList based on Record/clientScript.js @@ -0,0 +1,53 @@ +function onLoad() { + + /* + Inputs + 1. Related list name + 2. Table name which has been related to + 3. Field name through which relationship made + + E.g., + 1. Related list name - incident [Child Incidents] + 2. Table name which has been related to - incident [Incident] + 3. Field name through which relationship made - parent_incident [Parent Incident] + + + Output + If true = Hide the Related List + Else = there is record won't hide the Related List + + */ + + // Related list that want to hide when there is no record in the related list records + // Can add other related list as well this object is define as + // {related_list_name :{table : table_related_to, ref: field_reference_to_the_table}} + + var relatedList = { + 'task_sla': { table: 'task_sla', ref: 'task' }, + 'incident' : {table : 'incident', ref: 'parent_incident'}, + 'incident_task': { table: 'incident_task', ref: 'parent' } + }; + + + // Called the function based on the list of related list in the object., + for (var listName in relatedList) { + checkAndHideRelatedList(listName, relatedList[listName]); + } + + // function which is used to called the Script include through GlideAjax and check whether record is there are not and get the response based on that it will hide the related list. + function checkAndHideRelatedList(listName, config) { + var ga = new GlideAjax('CheckRelatedListRecord'); + ga.addParam('sysparm_name', 'hasRecord'); + ga.addParam('sysparm_related_table', config.table); + ga.addParam('sysparm_reference_field', config.ref); + ga.addParam('sysparm_record_sys_id', g_form.getUniqueValue()); + + ga.getXMLAnswer(function(answer) { + if (answer == 'false') { + g_form.hideRelatedList(listName); + + } + }); + } + +} \ No newline at end of file diff --git a/Client-Side Components/Client Scripts/Hide RelatedList based on Record/clientScript.png b/Client-Side Components/Client Scripts/Hide RelatedList based on Record/clientScript.png new file mode 100644 index 0000000000..bf48b9647b Binary files /dev/null and b/Client-Side Components/Client Scripts/Hide RelatedList based on Record/clientScript.png differ diff --git a/Client-Side Components/Client Scripts/Hide RelatedList based on Record/scriptInclude.js b/Client-Side Components/Client Scripts/Hide RelatedList based on Record/scriptInclude.js new file mode 100644 index 0000000000..484c7bd14d --- /dev/null +++ b/Client-Side Components/Client Scripts/Hide RelatedList based on Record/scriptInclude.js @@ -0,0 +1,19 @@ +var CheckRelatedListRecord = Class.create(); +CheckRelatedListRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, { + + // Glide Record to the particular table and then query and get the record and setlimit only for one record to avoid unnecessary querytime. + hasRecord: function() { + + var relatedTable = this.getParameter('sysparm_related_table'); + var referenceField = this.getParameter('sysparm_reference_field'); + var recordSysId = this.getParameter('sysparm_record_sys_id'); + + var gr = new GlideRecord(relatedTable); + gr.addQuery(referenceField, recordSysId); + gr.setLimit(1); + gr.query(); + return gr.hasNext() ? 'true' : 'false'; + + }, + type: 'CheckRelatedListRecord' +}); \ No newline at end of file diff --git a/Client-Side Components/Client Scripts/Hide RelatedList based on Record/scriptInclude.png b/Client-Side Components/Client Scripts/Hide RelatedList based on Record/scriptInclude.png new file mode 100644 index 0000000000..fd1cb80db4 Binary files /dev/null and b/Client-Side Components/Client Scripts/Hide RelatedList based on Record/scriptInclude.png differ