Hi All,
It's been a long time since I posted anything. Now I found something interesting, which I thought should go here as I did not find anything related to customer field for MS CRM 2011 on the net.
You all must have seen the customer field in MS CRM 4.0. But it's a bit differnt to implement it in CRM 2011. I took some hint from
http://www.stunnware.com/crm2/topic.aspx?id=js34 as to how it was done in CRM 4.0 and then modified the code to make it work for CRM 2011.
Here is the code.
//Code for Form Onload Event
function OnLoad() {
crmForm.all.tsf_supporterid_c.style.visibility = 'hidden';
crmForm.all.tsf_supporterid_d.style.visibility = 'hidden';
var accountLookup = Xrm.Page.getAttribute("tsf_accountsupporterid");
var contactLookup = Xrm.Page.getAttribute("tsf_supporterid");
// Set the available lookup types to account and contact.
document.getElementById("tsf_accountsupporterid").setAttribute('lookuptypes', "1,2");
document.getElementById("tsf_accountsupporterid").setAttribute('lookuptypenames', "account:1:company,contact:2:person");
document.getElementById("tsf_accountsupporterid").setAttribute('lookuptypeIcons', "/_imgs/ico_16_1.gif:/_imgs/ico_16_2.gif");
var lookupAcc = new Array();
var lookupCon = new Array();
lookupAcc = accountLookup.getValue();
lookupCon = contactLookup.getValue();
if (lookupCon != null) {
accountLookup.DefaultValue = contactLookup.getValue();
var custom = new Array();
custom[0] = new Object();
custom[0].name = lookupCon[0].name;
custom[0].id = lookupCon[0].id;
custom[0].typename = "contact";
document.getElementById("tsf_accountsupporterid").setAttribute('defaulttype', "2");
Xrm.Page.getAttribute("tsf_accountsupporterid").setValue(custom);
}
}
//Code for Form OnSave Event
function OnSave() {
var accountLookup = Xrm.Page.getAttribute("tsf_accountsupporterid");
var contactLookup = Xrm.Page.getAttribute("tsf_supporterid");
if (accountLookup.getValue() == null) {
Xrm.Page.data.entity.attributes.get("tsf_supporterid").setValue(null);
}
else {
var customer = new Array();
customer = accountLookup.getValue();
// A type code of 1 represents an account.
if (customer[0].type == "1") {
// If it is an account, then clear the contact lookup.
Xrm.Page.data.entity.attributes.get("tsf_supporterid").setValue(null);
}
else {
// A contact was selected, so copy the value to the contact lookup and clear the account lookup.
var cust = new Array();
cust[0] = new Object();
cust[0].name = customer[0].name;
cust[0].id = customer[0].id;
cust[0].typename = "contact";
document.getElementById("tsf_accountsupporterid").setAttribute('defaulttype', "2");
Xrm.Page.getAttribute("tsf_supporterid").setValue(cust);
Xrm.Page.data.entity.attributes.get("tsf_accountsupporterid").setValue(null);
}
}
}
Thanks,
Ashwani