Wednesday, November 23, 2011

Reset App Pool in Command Line

One of major issue happens when you customize CRM entities is, even you publish the solution, SOMETIMES your changes are not getting applied and hence you need to do a IIS Reset. This is becomes an issue,

1. when the client is also accessing the system
2. QA team performing testing in the same CRM Instance
3. Your team members may be using the system or in a debugging their code.

So instead of doing a IIS Reset, you can do a Application Pool reset. But normally that need to open the Internet Information Service (IIS InetMgr.exe) and need to navigate to App pools and you have to manyually reset all the relevent application pools.
Instead of that you may create a bat file, and call once.

@echo off
echo.
echo recycling 'CRMAppPool' Application Pool
%windir%\System32\inetsrv\appcmd recycle apppool /apppool.name:CRMAppPool
echo.

Thursday, November 17, 2011

Hide Section in CRM 2011

Do you want to hide a section in CRM 2011?

Steps

1. Open the customization form
2. Double click the section and copy the section Label. Let's say it's name is 'General'
3. Copy the below java script function
function HideSection(sectionLabel)
{
   try
   {
       var tabs = Xrm.Page.ui.tabs;
       for (var i = 0; i < tabs.getLength(); i++)
       {
           var tab = tabs.get(i);
           var sections = tab.sections;
           for (var j = 0; j < sections.getLength(); j++)
           {
               var section = sections.get(j);
               if (section.getLabel() == sectionLabel)
               {
                   section.setVisible(false);
                   break;
               }
           }
       }
   }
   catch (err)
   {
   }
}

4. Call the function in CRM Form Load event
HideSection('General');

Wednesday, November 2, 2011

Cancel/Inactivate a record when it is already in the same status


You many need to cancel Order, Invoice or any other record using workflow.
Before cancelling the workflow, you must check whether the record is already in cancel status.
If the record is already in cancel status, when you are trying to cancel it again, you will experience below error.


Workflow paused due to error: Unhandled Exception:
System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: 
The entity cannot be closed because it is not in the correct state.Detail:
-2147220941 The entity cannot be closed because it is not in the correct state. 2011-11-02T06:09:20.2231504Z
-2147220941 The entity cannot be closed because it is not in the correct state. 2011-11-02T06:09:20.2231504Z
at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType) at Microsoft.Crm.Extensibility.InprocessServiceProxy.ExecuteCore(OrganizationRequest request) at Microsoft.Crm.Workflow.Services.SetStateActivityService.<>c__DisplayClass1.b__0(IOrganizationService sdkService) at Microsoft.Crm.Workflow.Services.ActivityServiceBase.ExecuteInTransactedContext(ActivityDelegate activityDelegate) at Microsoft.Crm.Workflow.Services.SetStateActivityService.SetStateInternal(String entityName, Guid entityId, Int32 state, Int32 status) at Microsoft.Crm.Workflow.Services.SetStateActivityService.ExecuteInternal(ActivityContext executionContext, SetState setState) at Microsoft.Crm.Workflow.Services.SetStateActivityService.Execute(ActivityContext executionContext, SetState setState) at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager) at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

Tuesday, November 1, 2011

Workflow Scope

One of big mistakes we do when we are implementing CRM workflows are we do not set the Workflow scope. By default CRM sets the workflow scope to User Level

So the workflow is executing only when the workflow assign user triggers the workflow. To fix the problem, you have to set the scope to 'Organization' level, so the workflow will fire when any user execute a task that triggers the workflow.

Wednesday, October 26, 2011

Open CRM Process Dialog using Javascript

Here, one of important thing is Process Dialog Id doesn't change if you export the solution to any environment.

Step 1
Include the below function to onLoad in the form

function RunMembershipPaidProcessDialog()
{
    //Set the process dialog Id below
    var dialogID = "%7b706886CB-FFAB-4ECC-967E-B457CBE8C2C1%7d";
 
    //Set the entity logical name
    var entityName = Xrm.Page.data.entity.getEntityName();

    //Get the current record Id
    var recordId = Xrm.Page.data.entity.getId();
    
    //Set the Process Dialog width & height
    var width = "550";
    var height = "450";
 
    LoadProcessDialog(dialogID, entityName, recordId, width, height);
}
Step 2 Create a Common Javascript resource file (if you havn't create yet) so you can add all the common javascript function in this web resource. and add the below function
function LoadProcessDialog(dialogID, entityName, recordId, width, height)
{
    var context = Xrm.Page.context;
    var sUrl = window.parent.location.href;
    sUrl = sUrl.substring(0, sUrl.indexOf('/main.aspx'));

    var fullUrl = sUrl + "/cs/dialog/rundialog.aspx?DialogId=" + dialogID + "&EntityName=" + entityName + "&ObjectId=" + recordId;

    ShowPopUpWindow(fullUrl, "newWindow", width, height);
}


function ShowPopUpWindow(url, title, w, h)
{
    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2);
    var targetWin = window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);    
}

Monday, October 24, 2011

Custom width for columns in CRM Views

When you want to change width of a column in CRM view, CRM lets you to select the width from parameter set. These are the values you get.

25px, 50px, 75px, 100px, 125px, 150px, 200px, 300px

But sometimes, you have to give your own width.
Ex.
1.If there are only one or two columns in your width, you may need to give may be 500px for one column.
2. If you want to show many columns and if you are not like to show the horizontal scroll bar, instead of 200px and 300px, you may need to give 250px.

There is a solution for this.

Steps.
1. Publish your CRM solution.
2. Export the unmanaged solution.
3. Unzip the solution.
4. Open the Custermization.xml file (using XML viewable editor ex. Visual Studio).
5. Navigate to your entity Entities -> Entity -> SavedQueries -> savedqueries.
6. Find your view from the savedqueries.
7. Navigate to layoutxml -> grid
8. Now you can give any custom width to your columns
(
cell width="125" name="accountnumber"
cell width="500" name="name"
)

9. Save the Xml File

10. Zip the package

11. Import the solution

Tuesday, October 18, 2011

Creating a record using OData Service & JQuery

If you search in the net, you may find many examples of createing a record using OData Service & JQuery.
But in all these examples, you can see, that string values added to the entity object.
But how you are going to create a record which has a Lookup (Entity Refernce) value.

You can use following code


var entity = new Object();
entity.Subject = "subject goes here";
//Entity Reference (Also called as Lookup)
entity.RegardingObjectId = {
            Id: Xrm.Page.data.entity.getId(), 
            LogicalName: Xrm.Page.data.entity.getEntityName()
};

createRecord(
entity,
"new_entityNameSet",
 createAuditActionCompleted,
errorOnAuditCreate
);