Archiv für die ‘Dynamics CRM’ Kategorie

Howto: Use pre-generated Guids for records

5 Juni 2010

A customer asked me recently if it is possible to get the id of a record in a pre-create plugin. By default, it is not possible because the record is not yet created at this point and has not got an id.

However, Dynamics CRM allows you to create your own id for a record. The primary key of a record is stored in its Key-Property. On all default entities, for example account, the id property is marked as Valid for create (see accountid). This means, the sdk allows you to pass your own id.

using System;
using Microsoft.Crm.Sdk;
 
namespace PreCreateKeyPlugin
{
    public class PreCreateKeyPlugin : IPlugin
    {
        public void Execute(IPluginExecutionContext context)
        {
            if (IsPreCreate(context))
            {
                if (context.InputParameters.Contains(ParameterName.Target))
                {
                    DynamicEntity target = context.InputParameters[ParameterName.Target] as DynamicEntity;
 
                    if (target != null)
                    {
                        Guid customId = new Guid("{d7256b93-a5b5-45f9-9f2d-a1838279c35c}");
 
                        target["accountid"] = new Key(customId);
                    }
                }
            }
        }
 
        private bool IsPreCreate(IPluginExecutionContext context)
        {
            return  context.MessageName == MessageName.Create && 
                    context.Stage == MessageProcessingStage.BeforeMainOperationOutsideTransaction;
        }
    }
}

If you register this plugin for pre-create on the account entity and create a new account, it will have the provided id.

Please keep in mind, that you are in the responsibility to assign an new GUID for every execution of this plugin, but this should be clear when working with primary keys.

Howto: Get label for an attribute with JavaScript

19 Mai 2010

If you have to retrieve the label of an attribute in a form script, you have two options. The first one, is of course not supported, but quick and easy to implement.

function GetFieldLabel(fieldname)
{
  var field = crmForm.all[fieldname+ '_c'];
 
  if (field != null){
    return field.firstChild.firstChild.nodeValue;
  }
  else {
    return '';
  }
}
 
alert(GetFieldLabel('subject'));

The other option is, to retrieve the label for the user language from the crm service. I will cover this in another article.

Howto: Detect external request with JavaScript

1 Mai 2010

If you have activated Internet Facing Deployment for your CRM system, it could be necessary to detect if a request comes from outside your network or from your internal network.

For example, if you integrate a web application with an IFrame, you have to set different addresses for internal and external access. Or, if the application is only accessible from inside your network, you can show a message that it is not available for external users.

With help of Form Scripting you can detect an external request with little effort. You can make use of the function prependOrgName(”) which adds the organization prefix to the argument, if it is necessary. That means for an external request, it returns always the argument which you have passed.

function IsExternalRequest() 
{
  var ifdTestValue = prependOrgName('');
  var isIFDCall = ifdTestValue == '';
 
  return isIFDCall;
}

Calling IsExternalRequest will return true, if the request comes from an external network range. You could also define this function globally in the form context (see also http://www.stunnware.com/crm2/topic.aspx?id=JS5)

IsExternalRequest = function() 
{
  var ifdTestValue = prependOrgName('');
  var isIFDCall = ifdTestValue == '';
 
  return isIFDCall;
}

Common errors on organization import

21 Februar 2010

When you import organizations into an existing environment, there is the possibility that the import could fail. This post should help you to solve the most common issues.

With large organizations, the most likely problem is that the import process runs into a timeout when it updates the user mapping. Update Rollup 8 contains a fix for this problem which has to be enabled manually. See http://xrm.ascentium.com/blog/crm/Post768.aspx.

Another common issue is that the import fails with the following error

ExecuteNonQuery requires an open and available Connection. The connection’s current state is closed.

This error is likely caused by an issue in the .NET 2 Framework. See this article of the EMEA CRM Support team for a description
http://blogs.msdn.com/emeadcrmsupport/archive/2009/10/12/error-while-importing-an-organization-executenonquery-requires-an-open-and-available-connection.aspx

Another cause of this issue are reports which could not be published to the report server by any reason. For example, reports which are spiced up with custom assemblies cannot be published without the referenced assemblies. The import will fail when it tries to publish such a report. In this case enable the tracing, which generates a trace file for the mmc-console. With help of this file you can identify the report which is making problems. To solve the issue, you can either delete it directly from the database (unsupported) or you delete it prior to the backup of the originating system. After the successful import, you can import the problematic report again.

See this thread in the CRM Deployment forum for further description http://social.microsoft.com/Forums/en/crmdeployment/thread/1336bb5d-a119-44e3-83dd-1f557edccc31