creatiive
creatiive

Reputation: 1093

CRM 2011 Open a new window after save

Essentially what I need is to run some JavaScript after a record has been saved. This will pick up a guid from a field which has been populated by a plugin. My code looks like;

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

    var newguid = Xrm.Page.getAttribute("new_copyguid").getValue();
    Xrm.Utility.openEntityForm("new_myentity", newguid);

The problem is the code runs past the call to save() and continues executing before a plugin has populated the "new_copyguid" field. Is there a way to wait for the plugin to complete before continuing with the javascript? I have tried AddOnSave() without success. Any javascript callback seems to execute before the plugin finishes as well. The plugin is set to run synchronously.

I am performing this javascript from a button on the form. The button sets a field value and then saves the record, triggering the plugin. The button is a "Copy Entity" button which creates a clone. I need to open this new record in the browser.

I have read that this does not work either, as it happens before the save;

    Xrm.Page.data.refresh(save).then(successCallback, errorCallback);

Any pointers would be great!

Upvotes: 2

Views: 1576

Answers (4)

user6117020
user6117020

Reputation:

Try this:

function OpenForm()
{
  setTimeout(function () {
    var newguid = Xrm.Page.getAttribute("new_copyguid").getValue();
    Xrm.Utility.openEntityForm("new_myentity", newguid);
  }, 3000);
}

Upvotes: 0

Yaqub Ahmad
Yaqub Ahmad

Reputation: 27659

Try this:

function onPageLoad() {
    var formType = Xrm.Page.ui.getFormType();
    if (formType == 0 || formType == 1) { // 0 = Undefined, 1 = Create
        // If form is in Create Mode then
        if (Xrm.Page.data != null && Xrm.Page.data.entity != null) {
            Xrm.Page.data.entity.addOnSave(onSaveDoThis);
        }
    }
}

function onSaveDoThis() {
    setTimeout(onFormSaveSuccess, 300);
}

function onFormSaveSuccess() {

    var newguid = Xrm.Page.getAttribute("new_copyguid").getValue();
    if (newguid == "") {
        onSaveDoThis();
    } else {
        // Don't need to trigger the function onSaveDoThis anymore
        Xrm.Page.data.entity.removeOnSave(onSaveDoThis);
        Xrm.Utility.openEntityForm("new_myentity", newguid);
    }
}

Upvotes: 0

Munavvar Husein Shaikh
Munavvar Husein Shaikh

Reputation: 601

You can just wait for some seconds by putting this code.

function YourFunction()
{
    Xrm.Page.data.entity.save();
    OpenForm();
}

Its a new function.

function OpenForm()
{
  setTimeout(function () {
    var newguid = Xrm.Page.getAttribute("new_copyguid").getValue();
    Xrm.Utility.openEntityForm("new_myentity", newguid);
  }, 3000);
}

Upvotes: 0

Daryl
Daryl

Reputation: 18895

I think you'll have to run your logic in the OnLoad section. The save should force a refresh and your onload logic will run again. You'll need to do some check to see if the modified on date is within a certain time frame.

Other option is you perform the update manually through a rest call or Soap call, then you can read the value from the plugin in another call.

Upvotes: 2

Related Questions