中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

How to fix onSubmit data validation once and for a

發布時間:2020-06-19 21:00:24 來源:網絡 閱讀:506 作者:ServiceNow 欄目:開發技術

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;  
}


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

盐亭县| 眉山市| 禹城市| 邯郸市| 军事| 巴彦淖尔市| 盘山县| 右玉县| 蒲城县| 上饶市| 英吉沙县| 台湾省| 泰来县| 西乡县| 福清市| 白银市| 黄龙县| 天津市| 邵东县| 镇沅| 林甸县| 九寨沟县| 磐石市| 微山县| 安阳市| 黔江区| 衡山县| 峨眉山市| 新民市| 宜春市| 松原市| 克拉玛依市| 通城县| 嫩江县| 咸丰县| 北宁市| 连山| 兰州市| 丹凤县| 张家港市| 伊通|