How to fix onSubmit data validation once and for a
admin
2023-07-26 06:00:30
0

How to fix onSubmit data validation once and for all in Scoped Applica

So a little background. I have a Scope Application which has a catalog item. I wanted the ability to verify a file was attached prior to submission. The problem is, most all solutions proposed do not work in scoped applications...

 

Here are the current solutions you could use if you were not in a scoped application:

 

1) You could use GlideAjax and do a getXMLWait(), then check the answer and return false, example:

var ga = new GlideAjax('MyAttachmentUtils');  
ga.addParam('sysparm_name', 'hasAttachment');  
ga.addParam('sysparm_data', g_form.getValue('sysparm_item_guid')); // you can't use getUniqueValue, it doesn't map to what is in the attachment table  
ga.getXMLWait();  
if (ga.getAnswer() == 'false') {  
    g_form.addErrorMessage('You must attach something');  
    return false;  
}

All fine and dandy (if you are ok with using getXMLWait)...but, of course, getXMLWait is NOT available in scoped applications (makes sense, they don't want apps locking up the browser...

So...on to option 2

 

2) You could move your code from your script include directly into the client script...Then use GlideRecord and do the search in the client script!

var gr = new GlideRecord('sys_attachment');  
gr.addQuery('table', 'my_table');  
gr.addQuery('table_sys_id', g_form.getValue('sysparm_item_guid'));  
gr.query();  
if (!gr.next) {  
   g_form.addErrorMessage('You must attach something');  
   return false;  
}

All fine and dandy (if you are fine with calling server side code in a client...yuck!)...But, of course, GlideRecord is NOT available in a scoped application client script! Adding global. doesn't fix it either. But wait, there is a workaround for this.. option 2a...

 

2a) Take the above script and move it to the 'global' application, then it works. The major downside is that this is NOT included in your application and won't promote properly. You'll have to install a global update set with the scoped app...this kinda breaks the whole idea of having a scoped app....so not a good solution, but it technically works...there has to be something better....

 

So how do we do it?

 

I love async calls and I love using script includes to handle server code. So I decided to always return false on the onSubmit. Then, in the ajax callback function, I submit the form again if the validation is successful. However, this is easier said than done...

 

First, g_form.submit() doesn't work on catalog items (too easy, darn it!). It'll give you an error saying use g_form.orderNow()...which doesn't work either!

Second, use good old DOM manipulation (even though SN doesn't recommend it) document.getElementById('submit_button').submit() ...oops, Scoped Apps don't allow access to the DOM! Thwarted again!

 

Now what? Well, it can be done, here's how:

 

1) Create a variable on your form called 'submitted', I just made it a single line text with '' by default. This will prevent recursion.

2) Hide that variable using a ui policy

3) Create your onSubmit client script, but instead of the normal ways mentioned above, get the HTMLElement and use .click()...

function onSubmit() {  
  if (g_form.getValue('submitted') == 'yes') // this prevents recursion  
    return true;  
  
  var ga = new GlideAjax('MyAttachmentUtils');  
  ga.addParam('sysparm_name', 'hasAttachment');  
  ga.addParam('sysparm_table', 'x_your_scoped_table_name');  
  ga.addParam('sysparm_id', g_form.getValue('sysparm_item_guid')); //don't use getUniqueValue, it's bogus if there is an attachment and this hasn't been submitted  
  ga.getXMLAnswer(function(response) {  
    g_form.clearMessages();  
    if (response == 'false')   
      g_form.addErrorMessage('You must attach a Statement of Work'); // no need to do anything else, false was already returned  
    else {  
      g_form.setValue('submitted', 'yes'); // set the hidden value to prevent recursion  
      if(g_form.getControl('submit_button')){
          g_form.getControl('submit_button').click(); // use HTMLElement to grab the button, then use click()  
       }else{
           g_form.submit();   //portal submit form
       }      
    }  
  });  
  
  return false;  
}


相关内容

热门资讯

韩国队无缘世界杯32强,韩足球... 【环球网报道 记者 张江平】综合韩国《朝鲜日报》等媒体6月29日报道,对于韩国队无缘美加墨世界杯32...
蓝营提案采购无人机,卢秀燕力挺 据凤凰卫视报道,在台湾,国民党立法机构党团提案,要规划上限为2400亿新台币的预算,用于采购并发展台...
MWC上海2026·6G产业生... 6月24日至26日,2026上海世界移动通信大会(MWC)在上海新国际博览中心召开。本届MWC上海首...
贵州贵材新研发大楼落成 加速向... 6月28日,贵州贵材创新科技股份有限公司(简称“贵州贵材”)新研发大楼前红绸飘落,掌声雷动。这座大楼...
杭州探索设立OPC“AI产品体... 新华社杭州6月29日电(记者张璇、徐卓宇)记者29日从首届“AI+OPC”创新发展大会(杭州)上获悉...
2026年影像旗舰横评体验,视... 在2026年的今天,选择一款旗舰手机,强大的影像能力早已成为核心考量。尤其是对于热爱记录生活、追求创...
数智普惠,一步到位:当AI诊疗... 在医院的日常运转中,时间的价值被放大到极致。影像加载延迟、系统响应卡顿,都可能影响诊疗节奏,甚至贻误...
机器人造机器人:卡诺普如何成为... 封面新闻记者 陈彦霏 走进成都卡诺普机器人技术股份有限公司的智能工厂,AGV小车正驮着百公斤的机器...
劲松塑料取得袋体折叠定型支撑装... 国家知识产权局信息显示,江苏劲松塑料科技有限公司取得一项名为“一种袋体折叠定型支撑装置”的专利,授权...
刚刚,姚班传奇陈立杰苦思7年的... 新智元报道 【新智元导读】GPT-5.5 Pro 生成了一个数学证明,解决了计算几何中一个 陈立杰...