检查表单是否已保存在 CRM 2011 Javascript 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9991080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:25:55  来源:igfitidea点击:

Check the form has saved or not in CRM 2011 Javascript

javascriptdynamics-crmdynamics-crm-2011crmdynamics-crm-online

提问by Charan Raju C R

I'm writing a Javascript to call external link on click of custom ribbon button in CRM 2011 entity form. In javascript I'm checking the form is dirty or not. If the form is dirty,(means some fields are modified by user) then JScript will save the form forcefully using Xrm.Page.data.entity.save(). But, when the mandatory fields have not filled, force save will not be happened and I have to show some custom message to fill those fields, terminate the flow of control and should not open the external link. How to get whether the form has saved or not..?

我正在编写一个 Javascript 来调用外部链接,点击 CRM 2011 实体表单中的自定义功能区按钮。在 javascript 中,我正在检查表单是否脏。如果表单很脏,(意味着用户修改了某些字段),则 JScript 将使用Xrm.Page.data.entity.save()强制保存表单。但是,当未填写必填字段时,不会发生强制保存,我必须显示一些自定义消息来填充这些字段,终止控制流并且不应打开外部链接。如何获取表单是否已保存..?

Piece of code as below:

一段代码如下:

function buttonOnClick() {
    if (Xrm.Page.data.entity.getIsDirty()) 
    {
        Xrm.Page.data.entity.save();
    }
    else 
    {
        window.open('http://www.google.com', 'name', 'width=900,height=800');
    }
}

回答by Philip Rich

When you say 'form has been saved' do you mean for the first time? If so you can query the form type:-

当您说“表单已保存”时,您的意思是第一次吗?如果是这样,您可以查询表单类型:-

Xrm.Page.ui.getFormType();

(Is it in Create or Update for example). If the form is already in Update mode then you can check if the form is dirty as you say. If you want to know which mandatory fields have not been completed you can also potentially loop over the attributes on the form and query whether they are Business Required or not:-

(例如是在创建还是更新中)。如果表单已经处于更新模式,那么您可以检查表单是否如您所说的那样脏。如果您想知道哪些必填字段尚未完成,您还可以遍历表单上的属性并查询它们是否是业务必需的:-

Xrm.Page.data.entity.attributes.get("myAttribute").getRequiredLevel();

and add this to a warning message to the user.

并将其添加到给用户的警告消息中。

回答by glosrob

You could add your own OnSave method to validate the fields and return a value based on whether they are valid or not.

您可以添加自己的 OnSave 方法来验证字段并根据它们是否有效返回一个值。

e.g.

例如

Xrm.Page.data.entity.addOnSave(function() {
    var isValid = VerifyOnSave();
    if (isValid) {
        //continue
    }
    else {
        //show errors, cancel save
    }
);

function VerifyOnSave()
{ 
    //<insert validation logic here>
    return true;
}

That doesn't explicitly tell you the form saved, but lets you know whether the form is valid, which may or may not be close enough.

这并没有明确地告诉您保存的表单,而是让您知道表单是否有效,这可能不够接近,也可能不够接近。

回答by Manuel Roldan

You could try this way:

你可以试试这样:

var entitySaved;

function OnLoad(){
   entitySaved=false;
 }
function OnSave(){
   entitySaved=true;
 }
function myFunction(){
  if(entitySaved){
   //do your logic here
  }
}

Of course, you will have to add the form events from your CRM solution, by clicking in form properties.

当然,您必须通过单击表单属性从 CRM 解决方案添加表单事件。