CRM 4 - Default value for Lookup fields
- We have added Account & Contact Lookup fields in "Activity" form. What we have to do is
1. User must select an Account from "Account" Lookup field.
2. Based on the user selection "Contacts" (only the contacts related to selected account) should be listed in "Contact" lookup field.
3. By default "Primary Contact" or the first "Contact" should be selected automatically.
We were able to achive the first 2 points but regarding setting default value for "Contacts" lookup field, we have no clues... We tried lot of possible ways but without any success... Can anyone guide us in this?
Answers
Hi.
Try it now...
crmForm.all.regardingobjectid.attachEvent("onafterselect", OnAfterAccountSelect);
function OnAfterAccountSelect()
{
var contactLookup = crmForm.all.new_contactid;
if (contactLookup.DataValue != null)
contactLookup.DataValue = null;
var accountLookup = crmForm.all.regardingobjectid;
primaryContact = accountLookup.items[0].keyValues.primarycontactid;
if (primaryContact == undefined)
return;
contactDiv = document.all.new_contactid_d.getElementsByTagName("DIV")[0];
contactDiv.innerText = primaryContact.value;
contactLookup.Lookup( true , true , primaryContact.value , true );
}Cheers,
Adi
Hi.
The items won't exist unless you open the lookup dialog.
Although you can retrieve additional columns < fill the items array > using the auto resolve feature, this is becoming heavily unsupported.
I suggest that if the accountLookup.items[0] is null or undefined create an Ajax call to the server with the accountid and retrieve the primarycontactid your self.
Afterwards you can assign the retrieved values to the contact lookup using the traditional syntax e.g.
contactLookup.DataValue = [ new LookupControlItem( "[ contact guid ]" , 2 , "[ contact name ]" ) ]
Cheers,
Adi
All Replies
Hi.
The primary contact column is available to you in the account lookup view.
When a user selects an account you can extract the primarycontactid information using the account lookup onafterselect event and assign it the contacts lookup.
Now, the primarycontactid column does not contain the contact guid so I'm using the lookup auto-resolve feature to validate the item.
For example:
function OnCrmPageLoad()
{
crmForm.all.parentaccountid.attachEvent( "onafterselect" , OnAfterAccountSelect );
}
function OnAfterAccountSelect()
{
var contactLookup = crmForm.all.primarycontactid;
// Override only if null
if( contactLookup.DataValue != null )
return;
/*
//OR delete current primary contact value
contactLookup.DataValue = null;
*/
contactLookup.AutoResolve = 1;
var accountLookup = crmForm.all.parentaccountid;
primaryContact = accountLookup.items[0].keyValues.primarycontactid;
contactLookup.SetFocus();
contactDiv = document.all.primarycontactid_d.getElementsByTagName("DIV")[0];
contactDiv.innerText = primaryContact.value;
contactDiv.blur();
}
Cheers,
Adi
- Hi Adi,
First-of-all Thanks a lot for the inputs...
Tried your code after changing a bit and placed it in the form "Activity->Phone Call" under the "OnLoad" event.
Form is : Activity -> Phone Call
Accounts Lookup Field Name : regardingobjectid
Contacts Lookup Field Name : new_contactid (user lookup field which points to contacts)
crmForm.all.regardingobjectid.attachEvent("onafterselect", OnAfterAccountSelect);
function OnAfterAccountSelect()
{
var contactLookup = crmForm.all.new_contactid;
// Override only if null
if( contactLookup.DataValue != null )
return;
contactLookup.AutoResolve = 1;
var accountLookup = crmForm.all.regardingobjectid;
primaryContact = accountLookup.items[0].keyValues.primarycontactid;
alert(primaryContact.id) /*Getting error as Undefined*/
alert(primaryContact.value) /*displays the primary contact Name*/
contactLookup.SetFocus();
contactDiv = document.all.primarycontactid_d.getElementsByTagName("DIV")[0];
contactDiv.innerText = primaryContact.value;
contactDiv.blur();
}
Also, the default value is not getting displayed in the Contact Lookup field's textbox... Can you please give some more inputs? PrimaryContact does't contain an id attribute.
It does contain a value <lookup Text> and name <Schema Name> attributes.
//alert(primaryContact.id) <-- Delete this row
Changed the code marked in red bold. it should work now. because you're using the regardingobjectid lookup you should check if the primaryContact is not null.
function OnAfterAccountSelect()
{
var contactLookup = crmForm.all.new_contactid;
if( contactLookup.DataValue != null )
return;
var accountLookup = crmForm.all.regardingobjectid;
primaryContact = accountLookup.items[0].keyValues.primarycontactid;if( primaryContact == undefined )
return;
contactLookup.AutoResolve = 1;
contactLookup.SetFocus();
contactDiv = document.all.new_contactid_d.getElementsByTagName("DIV")[0];
contactDiv.innerText = primaryContact.value;
contactDiv.blur();
}Cheers,
Adi
- Hi Adi,
Thanks for the reply....
Now, contact name is getting displayed in the textbox... But it seems that the AutoResolve is not working properly... Only the Contact name is getting applied in the textbox, OID is not getting updated for the relavent Contact ID. Hi.
Added a new contact ( 1 : N ) relation to appointment > Added the lookup to the appointment form > Added the script to the onload event > works like a charm.
Witch activity form are you using?
Try to step through the code in debug mode.
function OnCrmPageLoad()
{
crmForm.all.regardingobjectid.attachEvent("onafterselect", OnAfterAccountSelect);
}
function OnAfterAccountSelect()
{
debugger;
var contactLookup = crmForm.all.new_contactid;
if( contactLookup.DataValue != null )
return;
var accountLookup = crmForm.all.regardingobjectid;
primaryContact = accountLookup.items[0].keyValues.primarycontactid;if( primaryContact == undefined )
return;contactLookup.AutoResolve = 1;
contactLookup.SetFocus();
contactDiv = document.all.new_contactid_d.getElementsByTagName("DIV")[0];
contactDiv.innerText = primaryContact.value;
contactDiv.blur();
}OnCrmPageLoad();
Cheers,
Adi
- Hi Adi,
Thanks a lot for the reply & sorry for the delay...
We are customizing the "Phone Call" form (NOTE: Not Activity form) just because for the time being we are customizing the phone call activities alone.
Not sure whether this is the problem, as the option to create "1-to-Many Relationship" is not getting displayed in "Phone Call" form we have created "Many-to-1 Relationship".
Relationship definition as mentioned below
Primary Entity : Contact
Related Entity : Phone Call
Name : new_contact_phonecall
Display Name : Contact
Type : lookup
Name : new_contactid
Requirement Level : Business Recommended
Display Option : Do not Display
Type of Behaviour : Referential
Lookup information as mentioned below
Display Name : Contact
Type : lookup
Name : new_contactid
Requirement Level : Business Recommended
Searchable : Yes
As per your suggession we have added the below code on "OnLoad" event of the "Phone Call" Form.
Code added on "Phone Call" form "OnLoad" event
//To attach an event that triggers after selecting the Account
crmForm.all.regardingobjectid.attachEvent("onafterselect", OnAfterAccountSelect);
function OnAfterAccountSelect()
{
//To activate debugger --TODO: Remove the below line before deploying
debugger;
var contactLookup = crmForm.all.new_contactid;
if (contactLookup.DataValue != null)
contactLookup.DataValue = null;
var accountLookup = crmForm.all.regardingobjectid;
primaryContact = accountLookup.items[0].keyValues.primarycontactid;
if (primaryContact == undefined)
return;
//To autoresolve the Contact ID based on the default value
contactLookup.AutoResolve = 1;
//To display the primary contact for the selected account
contactLookup.SetFocus();
contactDiv = document.all.new_contactid_d.getElementsByTagName("DIV")[0];
contactDiv.innerText = primaryContact.value;
contactDiv.blur();
//As blur event of Contact field is not getting triggered automatically, forcing the system to triger by changing the focus back to account
accountLookup.SetFocus();
}
As like I said earlier, we are not getting any errors while debugging/running... The only issue is AutoResolve is not working... The "Contact" name is getting displayed in the "Contact Id" field as plain text (without the link & icon).
Please let me know if I need to explain more about the settings we have in our CRM Server. Hi.
Try it now...
crmForm.all.regardingobjectid.attachEvent("onafterselect", OnAfterAccountSelect);
function OnAfterAccountSelect()
{
var contactLookup = crmForm.all.new_contactid;
if (contactLookup.DataValue != null)
contactLookup.DataValue = null;
var accountLookup = crmForm.all.regardingobjectid;
primaryContact = accountLookup.items[0].keyValues.primarycontactid;
if (primaryContact == undefined)
return;
contactDiv = document.all.new_contactid_d.getElementsByTagName("DIV")[0];
contactDiv.innerText = primaryContact.value;
contactLookup.Lookup( true , true , primaryContact.value , true );
}Cheers,
Adi
- Hi Adi,
Thanks a lot for the code... This is working fine when the user chooses the "Account" manually.
But, when the form get's loaded for the first time (form type is new) I'm not able to set the default contact... Tried calling the "OnAfterAccountSelect" on the form load (after loading the contacts list in lookup using the FetchXML) but it fails to load... Getting error message as "items.0 is null or not an object"...
if(crmForm.FormType == 1)
{
//If Account is not empty then load the Contacts
if (crmForm.all.regardingobjectid != null) {
crmForm.all.regardingobjectid.FireOnChange();
}
//To set the Primary contact as default contact
OnAfterAccountSelect();
}
//To attach an event that triggers after selecting the Account
crmForm.all.regardingobjectid.attachEvent("onafterselect", OnAfterAccountSelect);
//Set the Primary Contact as default contact
function OnAfterAccountSelect()
{
try
{
var contactLookup = crmForm.all.new_contactid;
if (contactLookup.DataValue != null)
contactLookup.DataValue = null;
var accountLookup = crmForm.all.regardingobjectid;
alert("AL :" + crmForm.all.regardingobjectid); //getting alert with the message "AL :[object]"
primaryContact = accountLookup.items[0].keyValues.primarycontactid; //Exception occured :'items.0' is null or not an object
if (primaryContact == undefined)
return;
alert("Primary Contact :" + primaryContact.value);
contactDiv = document.all.new_contactid_d.getElementsByTagName("DIV")[0];
contactDiv.innerText = primaryContact.value;
contactLookup.Lookup( true , true , primaryContact.value , true );
//To assign the phone number of selected account
crmForm.all.phonenumber.value = accountLookup.items[0].keyValues.telephone1.value;
}
catch (ex)
{
alert("Exception occured :" + ex.description);
}
} Hi.
The items won't exist unless you open the lookup dialog.
Although you can retrieve additional columns < fill the items array > using the auto resolve feature, this is becoming heavily unsupported.
I suggest that if the accountLookup.items[0] is null or undefined create an Ajax call to the server with the accountid and retrieve the primarycontactid your self.
Afterwards you can assign the retrieved values to the contact lookup using the traditional syntax e.g.
contactLookup.DataValue = [ new LookupControlItem( "[ contact guid ]" , 2 , "[ contact name ]" ) ]
Cheers,
Adi
- Ok Adi... Thanks a lot... Will try as per your suggestions...
Hi Ajaikumar,
How did you achieve your second point?
I mean, Based on the Account selected, you displayed the related Contacts. How did you achieve this?
Even I have a similar requirement but am unable to cross this point itself. Can you please let me know..
Thank you..
Hi.
Here , follow this link... it about filtering lookups in v4.0.
http://forums.microsoft.com/Dynamics/ShowPost.aspx?PostID=2792222&SiteID=27
Cheers,
Adi
