0
votes

On Microsoft Dynamics CRM 2011:

If I set the value of a lookup field using Javascript, it appears correctly on the form (name, icon, etc) but does not save when the user hits the save button. The record does save, just not that field. If I do it "manually" (ie - use the lookup control) it will save the field value.

For some reason, it is working in the form's "on Save" event, but it is Not working in the form's "on Save & Close" event. Any ideas?

Here is the code:

//Fetching the required accountId using REST method
function fetchAccountId() {
    try
    {
        var LE;
        var Fund;
        var LEId = new Array();
        LEId = Xrm.Page.getAttribute('ssi_legalentityid').getValue();   
        if (LEId != null) {
            LE = LEId[0].id;
        }
        var FundId = new Array();
        FundId = Xrm.Page.getAttribute('ssi_fundid').getValue();    
        if (FundId != null) {
            Fund = FundId[0].id;    
        }

        var LEId = Xrm.Page.getAttribute('ssi_legalentityid').getValue();
        var FundId = Xrm.Page.getAttribute('ssi_fundid').getValue();
        var TransactionType = Xrm.Page.getAttribute('ssi_transactiontypes').getValue();
        if (LEId != null && FundId != null && TransactionType == '6')
        {       
            var options = "$select=ssi_AccountId,ssi_name&$filter=Ssi_LegalEntityId/Id eq (guid'"+LE+"') and Ssi_FundId/Id eq (guid'"+Fund+"')";            
            SDK.REST.retrieveMultipleRecords('ssi_Account', options, saved, err, contactsRetrieveComplete);
        }   
    }
    catch (Err) {
    alert(Err);
    return;
    }
}
function saved(ssi_account)
{
    if (ssi_account != null && ssi_account.length > 0) 
    {
        var fetchedAccId;
        for (var i = 0; i < ssi_account.length; i++) 
        {
            fetchedAccId = ssi_account[i].ssi_AccountId;
            fetchedName = ssi_account[i].ssi_name;
        }       
        var Contact_Lookup = new Array();
        Contact_Lookup[0] = new Object();
        Contact_Lookup[0].id = fetchedAccId;
        Contact_Lookup[0].name = fetchedName;
        Contact_Lookup[0].entityType = 'ssi_Account';
        Xrm.Page.getAttribute('ssi_accountid').setValue(Contact_Lookup);
        Xrm.Page.data.entity.save();        
    }
}
function err(error){
    alert('Error occurred while fetching the details of the Client Account. '+error.message);
}
function contactsRetrieveComplete() {
}

The above code is called on the Save event of the CRM form.

2

2 Answers

0
votes

Your code retrieves the data onSave using the CRM SDK function retrieveMultipleRecords, which signature is this:

function (type, options, successCallback, errorCallback, OnComplete)

Your code passes the saved function to the successCallback parameter. The saved function is called asynchronously. In the Save scenario this is OK, but in the Save and Close scenario the form is closed and its scripts are disposed before retrieveMultipleRecords is able to receive the queried data.

Your code must operate synchronously to succeed.

In the RetrieveMultipleRecords function of the SDK.REST library you will find these lines:

var req = new XMLHttpRequest();
req.open("GET", this._ODataPath() + type + "Set" + optionsString, true);

Change the last true into false. This will put the function into synchronous mode.

-2
votes

Try to change line

Contact_Lookup[0].entityType = 'ssi_Account';

to line

Contact_Lookup[0].entityType = 'ssi_account';