Wednesday, June 27, 2012

Dealing with Dates in ODATA Endpoints.

I quote from MSDN:

When you retrieve records using the REST endpoint date values are returned as strings that use the format “\/Date()\/” where is the number of milliseconds since midnight January 1, 1970. For example: "\/Date(1311170400000)\/". All values returned from Microsoft Dynamics CRM represent the Universal Coordinated time (UTC) values.

Sample Code:

Note: In the below code sample "result" is the response from the ODATA querry.

//Retrieving the Date object as string
var stringDateValue = eval(result.CreatedOn).toString();               

Once the string date values are converted to Date objects you can use a variety of JScript methods to display the date as a string that can be displayed in a user interface .

var createdOn  = new Date(parseInt(stringDateValue.replace("/Date(", "").replace(")/", ""), 10)); 
createdOn = getODataLocalDateFilter(createdOn);

function getODataLocalDateFilter(date) {
//-- Description: For converting the date object to local time format
//-- You can also convert this to UTC Date format
//-- UTC Usage: getUTCMonth(), getUTCFullYear(), getUTCHours() ...
 var monthString;
 var rawMonth = (date.getMonth()+1).toString();
 if (rawMonth.length == 1) {
  monthString = "0" + rawMonth;
 }
 else
 { monthString = rawMonth; }
 var dateString;
 var rawDate = date.getUTCDate().toString();
 if (rawDate.length == 1) {
  dateString = "0" + rawDate;
 }
 else
 { dateString = rawDate; }

 var DateFilter = "";
 DateFilter += date.getFullYear() + "-";
 DateFilter += monthString + "-";
 DateFilter += dateString;
 DateFilter += " T" + date.getHours() + ":";
 DateFilter += date.getMinutes() + ":";
 DateFilter += date.getSeconds() + ":";
 DateFilter += date.getMilliseconds();

 return DateFilter;
}

Have fun working with ODATA!!

Tuesday, June 26, 2012

Updating the Owner in CRM.


For updating the Owner field programatically, we need to make use of the Assignrequest message. I have captured the sample code to update the Owner Id in CRM 4.0 and CRM 2011.

Updating the Owner Id in CRM 4.0:

Note: Plase, make sure to update the "ownerUpdatingEntity" with one you are trying to update. Also, Replace the "Entity_Name" with the name of the entity you are updating.

private static void AssignUser(CrmService service, Guid ownerId, Guid recordId)
{
 SecurityPrincipal assignee = new SecurityPrincipal();
 assignee.PrincipalId = ownerId;
 TargetOwnedDynamic ownerUpdatingEntity = new TargetOwnedDynamic();

 ownerUpdatingEntity.EntityId = recordId;
 ownerUpdatingEntity.EntityName = "Entity_Name";

 AssignRequest assign = new AssignRequest();

 assign.Assignee = assignee;
 assign.Target = ownerUpdatingEntity;

 AssignResponse assignResponse = (AssignResponse)service.Execute(assign);
}


Updating the Owner Id in CRM 2011:

Note: Please, make sure to replace the "Account.EntityLogicalName" with the right Entity in the below code.
 

private static void AssignUser(IOrganizationService service, Guid ownerId, Guid recordId)
{
 // Create the Request Object and Set the Request Object's Properties
 AssignRequest assign = new AssignRequest
 {
   Assignee = new EntityReference(SystemUser.EntityLogicalName, ownerId),
   Target = new EntityReference(Account.EntityLogicalName,recordId)
 };
 // Execute the Request
 service.Execute(assign);
}

Hope this helps!

 

Tuesday, June 5, 2012

Retrieve Plugin: MS CRM 2011

I was curious in retrieving the information from the context in Retrieve Plugin message. I realized that the context.InputParameters[“Target”], would return null/ throw exception in the retrieve plugin message.
  1.  To overcome that and to access the attribute collection through the context, here are the steps.
Plugin Configuration


2. The changes that needs to go in to the Plugin Retrieve message code. We need to make use of the OutputParameters collection.

            var orderRetrieve = (Entity) context.OutputParameters["BusinessEntity"];
            DateTime? date = null;
            if (orderRetrieve.Attributes.Contains("modifiedon"))
                date = (DateTime)orderRetrieve.Attributes["modifiedon"];



Also, couple of constraints, that I noticed in the Retrieve message plugin,
·         We cannot debug the Retrieve plugin by throwing an exception, in turn, we will not be able to see the tracing messages.
·         We cannot run plugin profiler to debug.

There is a way to handle the above constarints. will be blogging it on those topic soon...