There has been a slight change in the way you default the Customer Lookup field to Contact in CRM 2011 compared with CRM 4.
Use the following code to set the Customer Lookup to Contact instead of Account and set the default view.
document.getElementById("customerid").setAttribute("defaulttype", "2"); Xrm.Page.getControl("customerid").setDefaultView("a2d479c5-53e3-4c69-addd-802327e67a0d");
The first line sets the default entity type to Contact for the Customer Lookup dialog.
The second line sets the Customer Lookup dialog’s view to the ‘Contacts Lookup View’ using its guid value. If you do not use this line of code it will default to the ‘My Active Contacts View’ or whatever you have selected as the default Contact view.
That is quite useful. But is there a way to limit this lookup so they cannot select an Account -i.e. remove it entirely? We want all Opps to only be associated to contacts. If I use the create new opp from within the Account Opportunity associated view, it defaults to the account and the lookup default is Account. I know that I could use a new custom link to Contacts and not use the vanilla Potential Customer but we are several weeks (and 20K Opps) down the road so I’d rather not have to convert to a new field. We use Outlook client and convert emails to Opps too and that is mapped to Potential Customer so another reason why I don’t want to change fields.
You can try the following
document.getElementById("parentcustomerid").setAttribute("lookuptypes", "2");
document.getElementById("parentcustomerid").setAttribute("lookuptypenames", "contact:2");
document.getElementById("parentcustomerid").setAttribute("lookuptypeIcons", "/_imgs/ico_16_2.gif");
document.getElementById("parentcustomerid").setAttribute("defaulttype", "2");
or if using jQuery
$("#parentcustomerid").attr("lookuptypes", "2");
$("#parentcustomerid").attr("lookuptypenames", "contact:2");
$("#parentcustomerid").attr("lookuptypeIcons", "/_imgs/ico_16_2.gif");
$("#parentcustomerid").attr("defaulttype", "2");
Hi Rhett,
Is it possible to clear all the public views listed in potential customer lookup [for both Contact/Account] ?
I want only the custom view to be available for the end-user.
My first thought would be to check out the options on the field’s change properties dialog for the potential customer field.
Rhett -Many thanks. I should have posted a long time ago that this worked a treat
Hi Rhett
how this can be achieved in CRM2013?
Hi, This code is not working for crm 2015 online update 1
Do you get an error? Maybe if you copied and pasted then the double quotes may have got messed up. I just set them using " in my blog post html so hopefully it should resolve it self if that was the problem. You can of course just update your version by replacing the quote characters manually and see if that works.
Cheers,
Rhett
How do you get the Guid value?
love it, excellent piece of work.
Do you know if its possible to perform the same thing, but on a sub grid?
i.e. dynamically change the view used in the form’s subgrid
cheers
ian
Without testing this, the closest I can find to help you is here http://mscrmking.blogspot.com/2011/06/crm-2011-how-to-filter-add-existing.html
Pingback: A Configuration Data Framework for CRM 2011 « Gareth Tucker's Microsoft CRM Blog
I am new to javascripting in CRM2011, where would I put this code to make these changes, specifically to add this to the opportunity entity?
Hi Jamie, you could place this in the onload event of your client side code.
Cheers,
Rhett
Rhett,
Thanks for the reply. That is what I thought but I was receiving the following error message:
‘document.getElementById(…)’ is null or not an object.
Jamie, without investigating your solution in great detail I think you might find an example here which should help you on how to implement it.
Cheers,
Rhett
On which object do I place this client side code? i have tried it on a case OnLoad event but I just get a form error. Where is the ‘Customer Lookup’ form so I can add it to its onload event?
Thanks,
This code is for CRM 2011, and should be placed in the onload event script. In the code, ‘customerid’ is the Customer Lookup field and the guid ‘a2d479c5-53e3-4c69-addd-802327e67a0d’ is the desired Contact View id.
I hope this helps, cheers
Rhett
Hi Rhett
i cant’ find the form owner of the onload event.
for example i want it to use to select a Contact (instead of an Account) in the Regardinf of an Appontment.
which onload event i have to use? not the one of the Appointment, i suppose.
thanks!
Hi Rhett,
I am a newbie and am having issues with this. Similar to the comment made by Steve – “On which object do I place this client side code? i have tried it on a case OnLoad event but I just get a form error. Where is the ‘Customer Lookup’ form so I can add it to its onload event?”
The error I get is “The value off the property ‘setContact’ is null or undefined, not a Function object.” I am trying to apply this code to the Case entity form via the client in the solution section. It would seem this would have to be applied on a Customer Lookup form which seems to me to be the actual onLoad event, but I can’t find this form anywhere in the solution. Do I need to be doing this via Visual Studio?
Hi Damian,
You need to create an onload function in a js web resource and then attach that to your Case form onload event. Inside the onload function you can call this code. This code alters the default view that the Lookup field will load in the custom dialog when you click the Lookup field button.
I hope that helps.
Rhett
Hi Rhett,
This is what I’ve been doing but to no avail. Here are my steps, I’m not sure if i’m just missing something very basic or if it has something to do with my CRM environment. I’m running CRM 2011 On-premise with rollup 6.
1. Created Jscript web resource file called new_Employee (this particular org is for HR, renamed contact entity to employee)
2. Added following code to .js web resource
function setLookupEmployee()
{
document.getElementById(“customerid”).setAttribute(“defaulttype”, “2″);
Xrm.Page.getControl(“customerid”).setDefaultView(“00000000-0000-0000-00AA-000010001004”);
}
I have also tried this without the second line of code to see if it defaults to the default view but get the same error.
3. Added web resource to Case entity form as available library
4. Added setLookupEmployee to Form onLoad event.
5. Published All Customizations
When I open a new case record i get an error: “There was an error with this field’s customized event.
Field: window
Event: onload
Error: The value of the property ‘setLookupEmployee’ is null or undefined, not a function object.”
Not sure what I’m doing wrong. There MS error log show a line about an invalid character as below but I don’t know what exactly this is referring to:
1.0
Invalid character
3
/%7B634708046070000000%7D/WebResources/new_Employee
/userdefined/edit.aspx?etc=112&pagemode=iframe&preloadcache=1335207843180
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3; Tablet PC 2.0; MS-RTC LM 8)
en-us
en-us
en-us
1280×800
Web
2012-04-23T12:04:03
1033
1033
1033
1033
{EA55B557-0972-E111-8D30-0050569F003E}
{6E5F5E0F-0C72-E111-8D30-0050569F003E}
5.0.9690.1992
Try replacing the quotes in the code by typing them again as they may have got messed up if you copy/pasted that line of code, they could be the invalid characters.
That was it, works beautifully now. I retyped the quotes, I guess as you say they didn’t copy over as they appeared. I mentioned this to one of my colleagues in passing and she laughed at me saying “Ah yes, the dreaded quotes”. I guess this is a common rookie mistake when copying code from a web page.
Thank you so much for your help.
It works perfectly !!!
thanks for that.
Chris
Hi Rhett,
I am using following code to freeze lookup
document.getElementById(“parentcustomerid”).setAttribute(“lookuptypes”, “2”);
document.getElementById(“parentcustomerid”).setAttribute(“lookuptypenames”, “contact:2”);
document.getElementById(“parentcustomerid”).setAttribute(“lookuptypeIcons”, “/_imgs/ico_16_2.gif”);
document.getElementById(“parentcustomerid”).setAttribute(“defaulttype”, “2”);
it works fine, but the script after this code is not working.
as i’m trying to freeze “Regarding” field of any activity,and after that i’m setting some value for that field,that time it not sets the field value.
Regards,
Atul
Hi Atul,
You mention this code works but the code after this doesn’t. what is the code which is not working?
Cheers,
Rhett
Hi Rhett,
I am using this code on “onload” event of my form.
E.g
On Contact form OnLoad event i’m freezing Account lookup only to Account.
after that i am doing onchange event on another field. but it doesn’t fires that onchange event. if i removes this code then it works.
Regards,
Atul
Hi, This is code is not working for crm 2015 online update 1.
Hello Rhett,
I have issue with this code:
function form_onLoad()
{
hideContactOnCustomeridField();
}
function hideContactOnCustomeridField()
{
document.getElementById(“customerid”).setAttribute(“lookuptypes”, “1”);
document.getElementById(“customerid”).setAttribute(“lookuptypenames”, “account:1”);
document.getElementById(“customerid”).setAttribute(“lookuptypeIcons”, “/_imgs/ico_16_1.gif”);
document.getElementById(“customerid”).setAttribute(“defaulttype”, “1”);
}
It is run on opportunity. When I am creating new opportunity everything is ok (I can fill/change customer field only to account), but after saving when I try to change customerid field to other account, customerid field is empty (is not filled) and old value is removed.
Best regards,
Michał
Thanks for the info on this. However, I put the code in the onload event and get a generic error when I click on the lookup button on the Case entity.
Here’s what I put in the Onload event on the Case entity – working with CRM 2011 – Online. Grabbed the default view guid from the address bar when I clicked on the lookup (without the event enabled). I also put the code in Visual Studio to verify quotes – all looks good.
I’m guessing it’s having a hard time with the guid. I did try using the guid mentioned in this blog, but got the same results.
function setLookupContact()
{
document.getElementById(“customerid”).setAttribute(“defaulttype”, “2”);
Xrm.Page.getControl(“customerid”).setDefaultView(“%7bA9AF0AB8-861D-4CFA-92A5-C6281FED7FAB%7d”);
}
Your help is appreciated.
Brenda
Hi Brenda,
Remove the %7b and %7d from the guid as this is actually the encoded { and }.
Cheers,
Rhett
Hi Rhett,
Thanks – I finally figured it out. Haven’t been able to set the default view however, but at least the lookup defaults to Contacts. Fortunately the users want the system defaulted view so I’m good.
Thanks.
Brenda
Hi Brenda, I think if you had removed those characters I mentioned and if you had the right view guid (which you can get by opening one in the customisations area of CRM and viewing the address bar, which I think you already did..) then it should work.
Cheers,
Rhett
Rhett,
I am using this code to set the “to” field on a phone call to default to Contacts. It works just fine on the initial lookup click. But if you I open the advanced find, change the picklist from contacts to accounts and then click back on contacts the advanced find stays on account records.
It sounds like you might need to execute the code in the onchange event aswell.
Cheers,
Rhett
Jeremy, did you change the element ID or anything from the code snippet? All I did was add “function setLookupContact()” and braces. I can’t even get the initial lookup to default to Contacts. It’s still defaulting to Accounts.
Rhet,
I am using this code to set the “to” field on a Email Activity to default to Contacts and User. It works just fine on the initial lookup click. when a person decides to change from the new default lookup (say Contacts) to another lookup (User) then back to the original (Contacts). Then there is an error of “This object does’t support this property or method”.
Below is my javaScript, which is in the Onload
crmForm.all.to.setAttribute(“lookuptypes”, “2,8”); //only show contact and System User
crmForm.all.to.setAttribute(“lookuptypeIcons”, “/_imgs/ico_16_2.gif :/_imgs/ico_16_8.gif”); // set Icons
crmForm.all.to.setAttribute(“defaulttype”, “2”); // set Default.
Please help me How I can get rid of this error
Can this approach be applied to default lookups like products on opportunity product? This code did nothing in onChange:
Xrm.Page.getControl(“productid”).setDefaultView(’68C9758D-155E-E111-8C0E-0800277B32DC’);
You need to obtain the view id that you want to display, I am not sure of any reason why this view will not default for you if you have a valid view id.
There is a issue with Connections entity where by the defautt view is set internally upon clicking the Lookup button which prevents the default view from being set and this may apply elsewhere though I have not come across other scenarios like this.
Regards,
Rhett
Here’s what I tried.
I copied Id of custom Products view and added following code, which did nothing.
“””
$(“#productid”).attr(“disableViewPicker”, “0”);
Xrm.Page.getControl(“productid”).setDefaultView(‘A066D18A-8A4A-44EE-B78E-9A870DC799DD’);
“””
Ended up building View with addCustomView() in js code, but that’s no big deal.
Hi Rhett,
Do you know if there is a way to set the default view in Connections?
I have also tried addCustomView with no luck 😦
Regards,
Joakim
Thanks Rhett,
The Code below works in MSCRM 2011 for setting default contact and user in the party list.
crmForm.all.to.setAttribute(“lookuptypes”, “2,8”); //show contact and user
crmForm.all.to.setAttribute(“lookuptypeIcons”, “/_imgs/ico_16_2.gif :/_imgs/ico_16_8.gif”);//set the icons
crmForm.all.to.setAttribute(“defaulttype”, “2”);
Xrm.Page.getControl(“to”).setDefaultView(“00000000-0000-0000-00AA-000010001003”);
Regards
Surendra Iyer
Thanks Rhett for the post, and thanks Srendra Iyer for posting that working multi-entity lookup code!
I did
crmForm.all.to.setAttribute(“lookuptypes”, “2″); //show contact and user
crmForm.all.to.setAttribute(“defaulttype”, “2″);
and the default view is My Active Contact, but the filter won’t be triggered to show only my active contacts,it shows all contacts. How to trigger the filter?
You need to use Xrm.Page.getControl(“”).setDefaultView(“a2d479c5-53e3-4c69-addd-802327e67a0d”); and specify the guid that corresponds to the view you want to use.
I have two entities “Template” and “Products”.. I want to create a lookup in template entity for grouping products.. I have crated a lookup field in template form but it is selecting only one product, how can I enable lookup to allow multiple product selection???
Please help
You will have to add a MAny to Many relationship I think to achieve your desired outcome.
Cheers,
Rhett
thanks for replying… I have done it, but I have an another question..
How can I retrieve all the “products” related to a “template”, as a “template” can have multiple “products”.
It is like an “Account” can be related to multiple “Opportunities”. I want to retrieve all the “Opportunity” related to an “Account” entity.
“Product” and “Template” are custom entity I created.
Is there any way achieving the task using C#?.
Any help will be appreciated… thanks.
Rohit
Pingback: CRM 2011 – Good list of Javascript articles | Roman's Blog
this code is not running for CRM 2013 plz how we change or update it for CRM 2013
as in CRM 2013 DOM ahs been changed.
document.getElementById(“customerid”).setAttribute(“defaulttype”, “2”);
Xrm.Page.getControl(“customerid”).setDefaultView(“a2d479c5-53e3-4c69-addd-802327e67a0d”);
plz update it for CRM 2013