Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

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;
            }
        }

Wednesday, July 4, 2012

Change the default browser in VS 2010

When working with web applications and when you debug a web page normally you start with an IE with the page details. Sometimes you might want the browser to be firefox, chrome or safari or the other way around.

This is pretty easy to do. All you have to do is to go to your web page. Right click on that and select browse with and you will be able to choose the browser of your liking.

 

 
You can set the default browser here as well by selecting a browser and clicking on set as default button.

Scott Guthrie has written a nice article regarding this.

http://weblogs.asp.net/scottgu/archive/2005/11/18/430943.aspx
 

Tuesday, June 26, 2012

Making an EntityReference in a CRM 2011 to Null Programmatically

First of all, is this needed? Yes, we need to do this when working with c# code sometimes where we want to clear a look up field value. 

How can we do this?

It’s very easy. All we have to do is assign null value to the field. That would do the trick.
contact.parentcustomerid = null;

the above statement will clear out the parent customer value in a contact.

Thursday, June 14, 2012

Retrieving entity information programmatically

There are times that we need to retrieve information related to an entity separately. For an instance if we want to know the primary key field name in an entity or primary field id (this can be taken using the entityname + “id”) of an entity this is useful. The code snippet needed for this given below.
RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest
            {
                EntityFilters = EntityFilters.Attributes,
                LogicalName = “contact”
            };

RetrieveEntityResponse retrieveEntityResponse = (RetrieveEntityResponse)Execute(retrieveEntityRequest);

if (retrieveEntityResponse != null)
            {
                return retrieveEntityResponse.EntityMetadata.PrimaryNameAttribute;
            }

The above will return the primary field name of the entity.

Thursday, February 17, 2011

C# Const and Read Only Difference

We have const variables and read only variables. Is there a difference between them?
There is.

When you declare a const variable it has to be given a value at the time of declaration.

public const int a = 5;

When you declare read only variables you can give a value at the run time. You don’t have to give the value at the time of declaration. But the thing is you have to give the value in the constructor. You cannot give a value anywhere else.

Calss read
{
public readonly int b;

public read(int a)
{
  b = a ;
}

}

Const variables are by default static variables.

Some differences between abstract classes and interfaces



One major difference is that you don’t have to implement all the methods in the abstract classes, unless all the methods are declared abstract.

In an interface all the methods are abstract hence they would have to be implemented in the class that implements the interface. Also there can be no variables and method bodies where as in an abstract class you can have methods with method bodies(implementation)

Sealed keyword in C#


Sealed keyword will be used when you don’t need the class to be inherited furthermore.

e.g : - string class in c#

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...