Showing posts with label Javascripts. Show all posts
Showing posts with label Javascripts. Show all posts

Monday, August 13, 2012

Dependent Optionlist/Picklist values in CRM 2011

This is not possible with OOTB CRM features. We can use lookup options with cascading features from OOTB functions.

So is there a way to come around this? Yes, there is. There is a solution which has been created and shared among the developers. This is perfect when it comes to CRM interfaces.

The links below would take you to articles which describe it more.
http://blogs.msdn.com/b/paf/archive/2011/04/21/how-to-easily-create-dependent-picklist-in-crm-2011.aspx

The recurring problems have been included in this article.
http://blogs.msdn.com/b/paf/archive/2011/09/29/dependent-picklist-recurrent-problems.aspx

Please not you can have multiple picklist dependent on the same picklist in an entity. This is supported. All you have to do is to create a single web resource with all the mapping data inside it and it will work. 

If you have any issues doing the above let me know.

Thursday, August 9, 2012

CRM 2011 Integer fields and comma symbol

In CRM if you create an integer field by default the CRM group 2 numbers together by using a comma. This is the standard feature.

However sometimes customers ask this comma to be removed. This is not possible with OOTB functionality in CRM. You have couple of options.

•    Remove the integer field, have a text field and include javascripting to validate the field. This might be cumbersome.
•    Can create another field (text) and using a workflow assign the value of the integer field to this. This field would be getting displayed in views and forms. The drawback is you have synch both these fields.
•    You can have a javascript to remove the comma from the CRM form when it’s loading. Not much difficult. However you still would not be able to get rid of commas when you display a view.

Below I have given a common javascript function which can be used to get rid of commas. Add this to a web resource.


function RemoveComma(fieldname)
{
document.getElementById(fieldname).value = Xrm.Page.data.entity.attributes.get(fieldname).getValue();
}

•    Add the web resource to form onload event.
•    Call the function RemoveComma with the field name as the parameter.


This way you can use this function anywhere you want just by passing the field name to it.

Tuesday, July 24, 2012

Working with CRM 2011 Multiple Forms

Multiple forms concept came to CRM with 2011 edition. Previously we only had one form per entity. This was fine, but we needed to use javascript to hide fields based on the security role of the user (if the user was not supposed to see a particular field due to security)

With CRM 2011 we don’t have to use javascripts for this. Of course this is still possible. But there is another way of getting this done. You can use multiple forms.

You can have a form to represent a particular security role and you can have only the fields that particular user role should be seeing. So no more javascripts are needed for this.

However if this is not the reason that you have multiple forms in, but for another reason such as
•    Depending on a particular response the form should be picked
then we need to do some changes.

We had the same scenario and were greatly helped by the following blog posts.

http://www.avanadeblog.com/xrm/2011/06/crm-2011-form-navigation.html

This post describes how to do it.

http://social.microsoft.com/Forums/zh/crmdevelopment/thread/c06033d2-e158-4931-9340-e1de39c1d09e

This blog post has implemented it.

There are pros and cons for each method. The best method to do this cannot be pinpointed as it would depend on the requirement.

In our case we went ahead with

Xrm.Page.ui.formSelector.items.get(itemId).navigate(); (
itemId being the form name or form id)

Wednesday, July 11, 2012

Setting the Default Price List for a Product for the first time

When a product is created for the first time the default price list field is disabled. However you can set a value to this by using a javascript.

var lookup = new Object();
var lookupValue = new Array();
lookup.id = “Guid Value”;
lookup.entityType = “pricelevel”;
lookup.name = “Record Name”;
lookupValue[0] = lookup;
Xrm.Page.getAttribute(“pricelevelid”).setValue(lookupValue);

There are very good articles about this which I have given below.

http://mahenderpal.wordpress.com/2011/06/28/set-default-price-list-in-ms-crm-2011-using-java-script/

http://crmbusiness.wordpress.com/2011/02/18/crm-2011-how-to-set-up-a-lookup-using-javascript/

However you might face an issue here as the first time that this fields value is not getting saved to the database. The second time it gets saved.this functionality i have seen only with products. if you save an opportunity you don't get this issue. (If someone has overcome this using javascripts or have done this in any other way than using plugins i would be very much happy to hear about it.)

Is there a way to set the value when the record is saved?.

You cannot use a workflow as the form field is disabled and as you know if a field is disabled on a form then we cannot set a value to it. But using a plugin we can get this done.

We need to use a post create plugin with synchronous mode. I have given below the code for it.
This works correctly and the record is opening with setting a default price list for this field.



        public void Execute(IServiceProvider provider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)provider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory OrganizationServiceFactory = (IOrganizationServiceFactory)provider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService OrganizationService = OrganizationServiceFactory.CreateOrganizationService(context.UserId);

            switch (context.MessageName.ToLower())
            {
                case "create":
                    Entity target = context.PostEntityImages["Target"] as Entity;
                    EntityReference ent = new EntityReference("pricelevel", new Guid("D63F72CC-41CB-E111-B129-080027B1770C"));
                    target.Attributes.Add("pricelevelid", ent);
                    OrganizationService.Update(target);
                    break;
            }
        }

Monday, July 2, 2012

Disabling View Picker in a Look up (disableViewPicker )


There are times that we want the lookup filter to be disabled and display only the view that we want the customer to see. This is possible by creating a custom view and using the in-built functionality of CRM look up filter to the view picker.

If you want to do this in java script how would you go on about it? Is there a way?

There are couple of java scripts that you can write. However some of them won’t work with some of the Update roll ups in CRM 2011. Here I have listed some of them below.

Xrm.Page.ui.controls.get("lookupname").$0_3._element.disableViewPicker = 1;
This works with UR 6, but doesn’t work with UR 8.

document.getElementById("lookupname").disableViewPicker = 1;
The above seems to be a safe bet as it works with UR 6 and 8 both.

Thursday, June 28, 2012

Multi Selection option Lists in CRM 2011

Sometimes customers ask showing an option list whether it’s possible to have a multi selection list instead of an option list which will let the customer select only one option from the list. Unfortunately this is not supported with CRM 2011.

However there are ways to get this done and the following blog post state a very nice way to get this done. Please note as the author of the post says it’s unsupported.

Thursday, June 21, 2012

Window.event.target javascript in IE 8

We needed to get the id of an event that fired and we used event.taget.id there. We tested this functionality in IE 9 and Mozilla Firefox and it was working fine. 

However when this was tested with IE 8 it was not working and it gave an error saying that the target.id is null. Strange ha…

So we did some research on this with the help of google and found out that this is not working with IE 8. In IE 8 if you want to get the Id you need to use the srcElement attribute.
So instead of event.taget.id it should be event. srcElement.id in IE 8.

The best way to get this is use the following method. 

var controlId = (event.target || event.srcElement).id

This would give you the id of the event of the attribute that fired.

Thursday, February 3, 2011

Javascript Window.Open and Window.ShowModelDialogBox

There are difference between these methods when used. One cannot state this is the best method as these are suited for different scenarios.


For an example if you want your child window to be on top of the parent window and you cannot do anything to do the parent window till you close the child window, you will probably use Window.ShowModelDialogBox method.


If you want to access the parent window information from child window you will have use Window.Open method as Window.ShowModelDialogBox will not keep tabs with the parent window once it is executed.


However Window.ShowModelDialogBox can be used to pass a parameter value to the parent window from the child window using a return value. 


I will post some more information about these that i have experienced later.

Monday, January 24, 2011

CRM Grid Menu Items

in CRM grid menu items we have to use ISV.Config to pass the parameters of the selected records to the custom page that we have created.

In this page we have to use a javascript method to filter the selected record Guid's and if you want to display them in the load event you will have to move to another pages using window.open or window.showModalDialog method.

The issue here is that the intermediate page wouldn't be closed and when you close the newly opened page you might get the below error.

"Are you sure you want to navigate away from this page? Press OK to continue, or Cancel to stay on the current page."

If you want to avoid this you will have to put a javscript method.

window.onbeforeunload = null;

by adding this you will not get that message.

The following link was helpful to me for this post.

Tuesday, December 28, 2010

CRM 4.0 Show Associated-View in an IFRAME

This was possible and was a help to almost all the developers. But after doing this some would need to hide the buttons(Add new and Add existing buttons) from the associated view.

For this i found a nice JS which was written in the below post. This was written for hiding one particular button, but i changed it to hide both button by calling the same functions. However all credit must go to the original poster as without that i wouldn't have got the idea to do this. Thanks mate.

http://social.microsoft.com/Forums/en/crmdevelopment/thread/1e8eeb04-947f-4cea-920d-4fa1946f633d


My Changed JS Snippet....



function HideAllViewButtons(Iframe) {
Iframe.onreadystatechange = function HideTitledButtons() {
if (Iframe.readyState == 'complete') {
var iFrame = frames[window.event.srcElement.id];
var liElements = iFrame.document.getElementsByTagName('li');

for (var i = 0; i < liElements.length; i++) {
if (liElements[i].getAttribute('title').indexOf('Add existing') != -1) {
liElements[i].style.display = 'none';
}
if (liElements[i].getAttribute('title').indexOf('Add a new') != -1) {
liElements[i].style.display = 'none';
}

}
}
}
}

Tuesday, December 14, 2010

Sunday, June 13, 2010

Form Assistance in CRM 4

In CRM 4 this feature is inbuilt to form customisation. When you open a CRM form through customization area in one of the tabs you have 2 check boxes.

one will be to add the form assistance to the CRM form but not expanded by default. the other one is to expand the form assistance by default.

if you need to expand the form assistance you have click this check box as well. but if you only wants to load it, but not expand it then you can leave the check box without ticking it and it wouldn't be expanded by default.

Monday, June 7, 2010

Hiding the Date Part of a Date Time Field

Use the following javascript to do that.


document.getElementById("field_name").childNodes[0].childNodes[0].style.display = "none";
document.getElementById("field_name").childNodes[0].childNodes[1].style.display = "none";

Sunday, June 6, 2010

Disabling a time filed in CRM 4

If you need to disable the date field of the date time field and enable the time field you need to use the below java script to do so.

crmForm.all.Field_Name.Disabled = true;
crmForm.all.Field_Name.all.time.enable();


if you want to disable the time field only you need to use the below javascript.

crmForm.all.Field_Name.all.time.disable();

Friday, April 2, 2010

Getting the Querystring using Javascripts

The following javascript displays how to get a querystring value from a URL.

function RetreiveValues()
{
//Getting the URL part
var querystring = location.search.substring(1);
//Getting the string value
var objectTypeCode = getQuerystring('Flag');
}


function getQuerystring(key, default_)
{
if (default_ == null) default_ = "";
key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
var qs = regex.exec(window.location.href);
if (qs == null)
return default_;
else
return qs[1];
}

Thursday, September 3, 2009

Javascript issues with IE 7 and IE8

For the past 2 years we have been using CRM 3 web URL with IE 6 and & and have done the javascripts according to these browsers. But not we get javascript errors. This is due to the fact that the customer have upgraded IE 7 to IE 8. Accessing labels are bit different in IE8. I will post some issues that we have faced in a nother post.

Thursday, August 27, 2009

Remove Existing Buttons from CRM Pages - Updates

While i have done the testing in my browser after i have done the previous post i was told by some members of the QA team that this functiolaity was not working. I checked from my Broswer and it was woring fine in my machine. Then i inquired which browser they were using as mine was IE8. They were using IE7. I checked the DIV tag returned from each section and found out that in IE 8 ID attribute is followed by the class tag, but in IE 7 it's followed by Title tag.

I changed the javascript after that to check the browser version and it worked fine for both IE 8 and 7. |I will be publishing the changed javascript shortely.

Tuesday, August 18, 2009

Remove Existing Buttons from CRM Pages

This is something that when we upgraded one of our systems from 3 to 4 identified. This was a something we wanted to hide and i wanted to get some ideas from the web regarding this.
Fortunetly for me there was very good article by Dave Hawes. http://blog.davehawes.com/post/2008/04/23/MSCRM-4-Remove-Add-Existing-xxxxx-button.aspx

I used this article to get an idea. After reading this article i was able to get a clear idea of how to do this, but i wanted a generic solution as i wanted to apply this to all the CRM forms. i have pasted below the javascript i used.



var navGroup= document.getElementById('crmNavBar');
if (navGroup != null)
{
var x= navGroup.getElementsByTagName('li');
if (x != null)
{
//First 3 links are for the same entity, activity and activty history, Hence i removed them.
for( var i = 2; i < x.length ; i++ )
{
//getting the dive area. Refer below
var Div = x[i].innerHTML;
//get the index of id
var T = Div.indexOf('id');
//get the index of class
var T2 = Div.indexOf('class');
//get the value of title
var Title = (Div.substring(T+3,T2));

if (Title.indexOf('nav_') !=-1)
{
Title = Title.substr(4,Title.length-5);
}
else if (Title.indexOf('nav') !=-1)
{
Title = Title.substr(3,Title.length-4);
}

//Sub Accounts are different when treated by this javascript
if (Title.toLowerCase() != 'subact')
{
HideAssociatedViewButtons2(Title);
}
else
{

HideAssociatedViewButtons('SubAccts', ['Add existing Member to this record']);

}

}
}
}


function HideAssociatedViewButtons2(loadAreaId)
{

var isNativeEntity = false;
var navElement = document.getElementById('nav_' + loadAreaId);

if (navElement == null) {
navElement = document.getElementById('nav' + loadAreaId);
isNativeEntity = true;
}



if (navElement != null) {
navElement.onclick = function LoadAreaOverride()
{
if (isNativeEntity)
{
loadArea('area' + loadAreaId);
HideViewButtons2(document.getElementById('area' + loadAreaId + 'Frame'));
}
else
{
loadArea(loadAreaId);
HideViewButtons2(document.getElementById(loadAreaId + 'Frame'));
}
}
}
}



function HideViewButtons2(Iframe)
{


if (Iframe != null ) {
Iframe.onreadystatechange = function HideTitledButtons() {
if (Iframe.readyState == 'complete') {
var iFrame = frames[window.event.srcElement.id];
var liElements = iFrame.document.getElementsByTagName('li');
for (var i = 0; i < liElements.length; i++) {
if (liElements[i].getAttribute('title').indexOf('Add existing') !=-1){
liElements[i].style.display = 'none';
break;
}
}
}
}
}
}


function HideAssociatedViewButtons(loadAreaId, buttonTitles)
{

var navElement = document.getElementById('navSubAct');
navElement.onclick = function LoadAreaOverride()
{
loadArea('area' + loadAreaId);
HideViewButtons(document.getElementById('area' + loadAreaId + 'Frame'), ['Add existing Member to this record']);
}

}



function HideViewButtons(Iframe, buttonTitles)
{
if (Iframe != null ) {
Iframe.onreadystatechange = function HideTitledButtons() {
if (Iframe.readyState == 'complete') {
var iFrame = frames[window.event.srcElement.id];
var liElements = iFrame.document.getElementsByTagName('li');
for (var j = 0; j < buttonTitles.length; j++) {
for (var i = 0; i < liElements.length; i++) {
if (liElements[i].getAttribute('title').indexOf('Add existing') !=-1)
{
liElements[i].style.display = 'none';
liElements[i].style.visibility = 'hidden';
break;
}
}
}
}
}
}
}

DIV is similar to the below statement.

Associate Members


I would like to thank Dave and all the contributors for the their comments in the above mentioned post as without that i wouldn't be able to come up with this. Thanks mates.

Cheers.

Thursday, May 28, 2009

Custom Lookup Filters for CRM 4

Hi Guys,

We were facing some issues with the unsupported customizations that we have done to our CRM 3.0 Look up as these unsupported JavaScript customizations were not working with CRM 4.0.
Hence we had find alternative ways of getting this done.

Thanks to some nice articles by very good people we found a way.
i have posted a link which states how to do this. also i have copied an example here.
http://social.microsoft.com/forums/en-US/crmdevelopment/thread/3f6c8c3d-73a7-4601-98a3-f72cc4c7ea9b/

but the thing is nobody can guarantee that these will work with all the roll ups that Microsoft are putting out. hence be careful with these.

Example:-

var fetchStr = "";
crmForm.all.new_accountid.lookupbrowse = 1;
crmForm.all.new_accountid.additionalparams = "search=" + fetchStr;



Monday, March 2, 2009

Change the default filter in activities in crm 3.0

We needed to change the default activity history filter as a customer asked us to do that. I searched the net to find some answers and there were 2 very good articles regarding this. i have pasted the links below.

http://www.stunnware.com/crm2/topic.aspx?id=js11
http://ts2community.com/blogs/larrylentz/archive/2008/12/19/changing-the-default-activities-filter-on-view.aspx

Retrieving Calendar of a Bookable Resource in Dynamics

There are occasions where we need to retrieve working days and working times of a resource in Dynamics grammatically. This is quite possible...