简体   繁体   中英

CRM and iframe aspx page form submission

Scenario :

I have aspx page which I need to Iframe on CRM's Opportunity form. This aspx page has form which submits data into the other database.

Requirement : I would like when user clicks save button on CRM opportunity form ,aspx page should store the data in external database and opportunity form should also save all the changes on CRM form.

My Efforts :

Till now I have Iframed aspx page on CRM form.I am also submitting the form using OnSave event. But the only problem is the form gets submitted but by the time it executes the complete code CRM form gets refreshed . End result is that Data on aspx page does not get stored in the external database.

What can be the other possible way to achieve this functionality ?

Thanks for taking time to read. Thank you in advance.

Option 1: The better solution is to do this from an opportunity post event plug-in. This ensures data consistency between CRM and external data (if required). Also you could use WCF or a web service to transmit the data to external DB.

Option 2: If you must use javascript you could (1) bind to opportunity form OnSave, (2) Prevent the form from submitting , (3) submit the iframe and (4) wait until it comes back and then (5) do another save to complete the action. This however might cause inconsistencies between CRM and external DB if opportunity save fails.

Here is a pseudo code example

function OpportunityOnLoad() {

    IFRAME.OnReadyStateChange = function() {
        // (4) Check success if possible 
        // (5) unbind save event and complete the opportunity save
        Form.RemoveOnSave(OpportunityOnSave)
        Form.Save();
    }

    //OnLoad
    Form.AddOnSave (OpportunityOnSave);
}


function OpportunityOnSave(context) {
   //(1) Save clicked
   //(2) Stop save
   context.PreventDefault();
   //(3) Submit iframe form
   IFRAME.Submit();
}

EDIT: Regarding Q1 : unfortunately not. Regarding Q2 :

This is a rough translation of the concept above into Javascript and CRM client side API. I didn't test it but it should put you on the right track.

Change the Params to match the iframe id, url etc. also since you're using an aspx you might experience cross domain issue that could be easily overcome if you're browsing IE and not so easily overcome if you're using CROME for example.

var IFRAME, SaveMode; 
var FORM = Xrm.Page.data.entity;
var UI = Xrm.Page.ui;

var SaveModes = {
   1 : "save", 
   2 : "saveandclose", 
   59: "saveandnew" 
}

var Params = {
   IframeBaseUrl : "",
   IframeId : "IFRAME_test",
   IframeFormId : "form1"
}

function OpportunityOnLoad() {
    var sUrlparams = "?";  //add required params after ?  
    var IframeUrl = Params.IframeBaseUrl + sUrlParams; 

    IFRAME = UI.controls.get(Params.IframeId);
    IFRAME.setSrc(IframeUrl);
    IFRAME.Dom = document.getElementById(Params.IframeId);
    IFRAME.add_readyStateComplete(OnAfterIfameSave);

    FORM.addOnSave(OpportunityOnSave);
}

function OnAfterIfameSave() {
    //SubmitSuccess indicates that the form has reloaded after a  
    //successful submit. You'll need to set this variable inside your iframe.

    if (IFRAME.contentWindow.SubmitSuccess) {
        FORM.removeOnSave(OpportunityOnSave);
        FORM.save(SaveModes[SaveMode]);
    }
}

function OpportunityOnSave(execObj) {
   var evArgs = execObj.getEventArgs();

   evArgs.preventDefault();
   SaveMode = evArgs.getSaveMode();
   IFRAME.contentWindow.document
    .getElementById(Params.IframeFormId)
    .Submit();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM