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.