1

I currently have a development environment for CRM 2013 On-Premise. In my custom entity, the following JavaScript code runs:

function UpdateBPF() {
    var requests = [];
    requests.push(Xrm.Page.getAttribute('description').setRequiredLevel('none'));
    requests.push(Xrm.Page.getAttribute('categoryid').setRequiredLevel('none'));
    requests.push(Xrm.Page.getAttribute('priority').setRequiredLevel('none'));
    requests.push(Xrm.Page.getAttribute('initialtype').setValue(1));
    requests.push(Xrm.Page.data.save());
    requests.push(event.returnValue = false);

    $.when.apply(undefined, requests).always(function () {
        Xrm.Page.data.setFormDirty(false);
        RefreshForm();
    });
};

function RefreshForm() {
    Mscrm.ReadFormUtilities.openInSameFrame(window._etc, Xrm.Page.data.entity.getId());
};

What I'm trying to do:

  • Change a business process flow on change of an option set value

What my code is doing after switching the option set:

  • Launching a custom workflow to do the actual switching (Works fine, I require the refresh to actually display the selected workflow to the user)
  • Not requiring certain fields (prevented refresh from occurring if not filled)
  • Saving the form (After refreshing to display the correct business process flow, values were never saved so the option set value was never set)
  • Preventing the "You have not saved yet" popup from occurring. (Good possibility I no longer need this)
  • I set the form to not dirty after not saving

Why am I pushing most of the steps into the requests variable?

  • I wanted those values to definitely occur before refreshing the form. Very likely I'm doing this incorrectly or not efficiently.

What worked:

  • Everything worked in my CRM 2013 On-Premise Test environment as expected.
  • Save
  • Refresh
  • Correct Business Process Flow & Stage

The Issue for CRM 2013 Online:

  • Saving and Refreshing do not work
  • IE 9,10,11 and FireFox Completely crash. Chrome is fine.
  • Turning off Auto-Save didn't make a difference

What I've tried for save:

  • Xrm.Page.data.save()
  • Xrm.Page.data.entity.save()

What I've tried for refresh:

  • Xrm.Page.data.refresh()
  • Mscrm.ReadFormUtilities.openInSameFrame(window._etc, Xrm.Page.data.entity.getId())
  • Xrm.Utility.openEntityForm("service_ticket", Xrm.Page.data.entity.getId(), null)
  • var url = Xrm.Page.context.getClientUrl(); window.open(url + "/main.aspx?etn=ticket&pagetype=entityrecord&id=" + Xrm.Page.data.entity.getId() + "&newWindow=false", "_blank", null, false);

When I debug, step by step through the code posted at the top, it gets the then .when, .apply, and.always but SKIPS everything inside. I would think that always means that it always runs...

When I try:

Xrm.Page.data.save().then(
    function () {
        alert('Save worked, refresh');
        Xrm.Page.data.refresh();
    },
    function () {
        alert('Save failed!');
    }
);

Stepping through the code has it hit save. But after hitting then, it SKIPS everything inside and then IE(Which I've been mainly testing in because Chrome seems to work...), completely crashes. An error doesn't exactly popup, it's more of an alert that say "IE has stopped working".

I've seen 1 similar issue in StackOverflow without an accepted answer or an answer that did not work for me at all. The Xrm.Page.data.save() function just before the previous 2 paragraphs was a sample from one of the answers. Super simple sample that was easy to follow and step through for testing purposes with no change in result.

I've also Googled (my best friend) and even Binged my issue. The one other post on another site, I believe on a Microsoft Site, had no responses at all. Based on what was written, I assume it was the same person as for the StackOverflow post.

Any insight into the problem, whether it be my code or something else, is greatly appreciated.

tl;dr Save and Refresh JavaScript does not work for CRM 2013 Online

4

2 回答 2

5

我最终做的是在 Xrm.Page.data.save 之前有一个 setTimeout。似乎在字段更改之后和我的 JavaScript 之前直接发生了一些事情。这可能是业务规则(但我最初将它们停用,所以有可能不是它)。

无论哪种方式,我为解决我的问题所做的就是在短暂的超时内附上保存功能,然后使用 Xrm.Utility.openEntityForm 来“刷新”整个页面,因为我宁愿不使用不受支持的

Mscrm.ReadFormUtilities.openInSameFrame(window._etc, Xrm.Page.data.entity.getId())

下面是我使用的代码片段:

setTimeout(function () {
    Xrm.Page.data.save().then(
        function () {
            var id = Xrm.Page.data.entity.getId();
            var entityLogicalName = Xrm.Page.data.entity.getEntityName();
            Xrm.Utility.openEntityForm(entityLogicalName, id);
        },
        function () {
            alert('Save failed! Please refresh the form.');
        });
}, 500);

在保存新创建的记录后,我得到了记录的 ID。在超时或保存之前将其设置为打开新记录(本质上是保存 + 新建)。

如果有更有效的方法来解决这个问题,请让我知道,我会选择你的答案(如果有效)作为正确的答案。

编辑:
下面的代码实际上是一个更好的版本。我之前的代码保存和保存&关闭,导致保存发生两次,这弄乱了我对不同项目的一些逻辑

setTimeout(function () {
    var id = Xrm.Page.data.entity.getId();
    var entityLogicalName = Xrm.Page.data.entity.getEntityName();
    Xrm.Utility.openEntityForm(entityLogicalName, id);
}, 500);

在这里,openEntityForm 在关闭并再次打开之前保存记录,这是我最接近保存和刷新的方法。

于 2014-04-28T20:42:43.257 回答
0

以下代码应与“modifiedon”字段的“onchange”事件相关联。保存表单后,“modifiedon”字段总是更新。因此,单击“保存”后,表单将使用以下函数重新加载。

function ReloadFormAfterSave(eventArgs) {
var formId = Xrm.Page.ui.getFormType();
alert(formId);
if (formId == "2") { // in my case, I only want the reload to occur when updating
    setTimeout(function () {
        var id = Xrm.Page.data.entity.getId();
        var entityLogicalName = Xrm.Page.data.entity.getEntityName();
        Xrm.Utility.openEntityForm(entityLogicalName, id);
    }, 500);
}
}

有关“modifiedon”的说明,请参阅http://dynamicscrmtouch.wordpress.com/2014/07/05/onload-event-not-triggered-after-save-on-crm-2013/

我忘了提到您需要在表单上添加“modifiedon”作为隐藏字段。

干杯!

于 2014-09-12T20:12:01.560 回答