Featured Post

Web API Requests Series

Web API Series:  Create and Retrieve , Update and Delete , Retrieve Multiple , Associate/Disassociate , Impersonation , Run Workflows

04 July 2012

How to associate a record as Activity Party to an activity.

In this post I will show you how to associate a record to an activity (in this case an email) as an Activity Party using the OData REST end point with Javascript.
Setting the "To", "From", "Cc", "Bcc" fields on an email message is not so straight forward as other lookup fields such as the "Regarding" field. This is because these fields are of Activity Party type.
There are two important steps to follow:
  1.  Set the "party" of the ActivityParty (what will be related to the activity) as an EntityReference. activityParty.PartyId = { Id: record.RecordId, LogicalName: "logicalName" }; 
  2.  Set the "activity" of the ActivityParty (the e-mail, in this case) as an EntityReference. activityParty.ActivityId = { Id: email.ActivityId, LogicalName: "email" }; 
The third steps is to set the participation type (what role the party has on the activity)

Use: activityParty.ParticipationTypeMask = { Value: 1 };  To set the "From" field
Use: activityParty.ParticipationTypeMask = { Value: 2 };  To set the "To" field
Use: activityParty.ParticipationTypeMask = { Value: 3 };  To set the "Cc" field 
Use: activityParty.ParticipationTypeMask = { Value: 4 };  To set the "Bcc" fiel.

Ultimately perform the REST request.

Here is the complete code:

 createActivityParty: function (object, email, logicalName, recipient) {

        var activityParty = {}, jsonActivityParty;
        switch (logicalName) {
            case "account":
                activityParty.PartyId = {
                    Id: object.AccountId,
                    LogicalName: "account"

            case "contact":
                activityParty.PartyId = {
                    Id: object.ContactId,
                    LogicalName: "contact"
            case "systemUser":
                activityParty.PartyId = {
                    Id: object.SystemUserId,
                    LogicalName: "systemuser"

        // Set the "activity" of the ActivityParty (the e-mail, in this case) as an EntityReference.
        activityParty.ActivityId = {
            Id: email.ActivityId,
            LogicalName: "email"

        // Set the participation type (what role the party has on the activity). For this
        // example, we'll put the account in the From field (which has a value of 1).
        if (recipient == "sender") {
            activityParty.ParticipationTypeMask = { Value: 1 };
        else if (recipient == "to") {
            activityParty.ParticipationTypeMask = { Value: 2 };
        else if (recipient == "cc") {
            activityParty.ParticipationTypeMask = { Value: 3 };

        //stringify the object to be created
        jsonActivityParty = JSON.stringify(activityParty);

            type: "POST",
            url: this._ODataPath() + "/ActivityPartySet",
            data: jsonActivityParty,
            success: function (request) {

            error: function (request) {


Hope this helps, also don't forget to check the SDK. which is where I learned about this.

13 April 2012

How to assign a record to another User in CRM 2011

In this brief post I will show you how to set the Owner of a record in C#, in other words how to assign a record to a User.

To assign a record an Owner you have to send an Assign Request using the Execute method.

service.Execute(new AssignRequest()
                                                        Assignee = new EntityReference("systemuser", userId),
                                                        Target = new EntityReference(updateEntity, recordId)

The Assign  Request has two properties:

  1. Assignee: This can be either a Team or a User.
  2. Target: this is the record you want to assign.

Hope this helps.

03 April 2012

Open CRM form via JavaScript

Have you been struggling to find out how to open a CRM form from another form or from a custom page?

Here is the solution:

Use window.open("/main.aspx?etn=entityname&pagetype=entityrecord&id=" + recordGUID");

or simply:

to create a new record.

You can also pass custom parameters using the extraqs parameter and encodeURIComponent. You can find out more from the Crm SDK.


25 March 2012

Read only fields are not saved.

If you are writing your custom web resource and using it to update standard crm fileds, rememember that they will not be saved if they are read-only.
Let me make myself clear with an example.
I was developing an HTML web resource and I was changing the value of a field only shown on the footer. In order to be able to change the value of that field I had to place it on the form as well and hide it. This means that I set, from the form designer, the following properties:
  1. Visible by default = false
  2. Field is read-only = true
If you do this you will be able to access the read-only field but when you save the form that field will not be saved because is read-only.
So what you should do is: Hide the field on the form but don't set the field to be read-only as shown below.

If it is a bug or it is normal, I don't know. If someone knows the reason of this behavior please let me know.
Thank you.

19 March 2012

How to pass parameters from subgrids to JavaScript

In this post I will show you how to pass parameters regarding a grid or subgrid to a JavaScript function by clicking a custom button on the ribbon menu.
A common requirement is to pass poarameters to a JavaScript function called when clicking a Ribbon button.
To pass parameters regarding a grid or a subgrid we can use the "crmparameter" element of the RibbonDiffXml file.
The available parameters can be devided in three groups:

  1. Selected items 
    • SelectedControlSelectedItemCount: The number of selected items in a grid or subgrid.
    • SelectedControlSelectedItemIds: A string array of GUID Id values for all selected items in a grid.
    • SelectedControlSelectedItemReferences: An array of EntityReference objects that represent all the selected items in the grid
  2. All items
    • SelectedControlAllItemCount: The number of items in a grid or subgrid.
    • SelectedControlAllItemIds: A string array of GUID Id values for all items in a grid.
    • SelectedControlAllItemReferences: An array of EntityReference objects that represent all the items in the grid.
  3.  Unselected items
    • SelectedControlUnselectedItemCount: The number of unselected items in a grid or subgrid.
    • SelectedControlUnselectedItemIds: A string array of GUID Id values for all unselected items in a grid.
    • SelectedControlUnselectedItemReferences: An array of EntityReference objects that represent all the unselected items in the grid. 
The way you can pass these parameters by clicking a Ribbon button is shown below:

  <CommandDefinition Id="Mscrm.SubGrid.opportunity.Command">
              <EnableRule Id="Mscrm.SubGrid.opportunity.EnableRule"></EnableRule>
              <JavaScriptFunction Library="$webresource:functions.js" FunctionName="MyFunction" >
                <CrmParameter Value="SelectedControlSelectedItemIds" />
                <CrmParameter Value="SelectedControlSelectedItemCount" />

To access these parameters add them to your function definition like below:

function MyFunction(SelectedControlSelectedItemIds, SelectedControlSelectedItemCount) {
      for (i = 0; i < SelectedControlSelectedItemCount; i++) {

So as you can see it is possible to access records displayed in a subgrid using supported code, so don't get tempted to use document.getElementById.  ;)

I hope this help.

14 March 2012


Using an ITIL accredited ITSM suite can dramatically improve your operations by:
•Enabling users to log and track their own incidents, change requests and service requests
•Automating SLA adherence, workflows and assignments
•Root cause analysis with deep insight from powerful reporting tools
•Increase end user satisfaction with timely actions and accurate information

During this seminar you will learn how to achieve all of the above and more. Our ITSM enables you to leverage your exisiting environment and maximise effectiveness. We look forward to seeing you in two weeks!!

Register here: http://www.alfapeople.com/UK/EN/Events/Pages/Events.aspx

03 March 2012

Crm 2011 Survey Creator Beta

The Crm 2011 Survey Creator is a CRM solution that allows us to create Surveys in CRM 2011.
A survey can be created in different ways, for example:
  1. From the Contact form
  2. From the Account form
  3. From the Main Page Navigation Pane
A survey consists of the following parts/entities:
  1. Survey Template
  2. Questions Group
  3. Question
  4. Survey
 To create a Survey we first need to create a Survey Template, add Questions Groups to the Template, add Questions to the Group and then we are ready to create our Survey/questionnaire.

1. Create a Survey Template  

2. Add Questions Groups to the Survey

We can specify an order number, this will tell in what order to display the question group in the Survey.

3. Add Questions to the Group/s

4. Here we can specify the question name, if an answer is required or not, the order in which the Question will appear in the Question Group in the Survey, and the type of answer we expect:
  1. Text
  2. Number
  3. Decimal
  4. Date
  5. Yes or No
  6. Picklist/Dropdown
Once we are done with the Template, Groups and Questions we are ready to create a Survey based on the template we created.

Fill the required fields (the Performed On date will automatically filled in and is read-only) and then save the Survey, when the survey form reloads it will come up with the questions on the form, see below.

Part 1

Here we can see Text fields, Ye/No and Dropdown.

Part 2

Here we can see a required field in red and Number fields. We also have a little help that says what is the type of the filed. However, Validation on the fields is implemented on the Survey Form. See below for the validation messages:

Validation on Number Fields

Validation on Date Fields

The date separator is based on your CRM customisation so if you enter an "invalid" date you'll be prompted with an error.

Validation on Decimal Fields

Validation on Required Fields

Finally if we try to save a Survey without filling require field we will not be able to save the survey and the user will be prompted with the above message.
One more thing, the survey can be marked as complete by clicking the Custom button "Save as Complete". If we do this the next time we open the survey, all the fields will be disabled and the Survey status at the bottom will say "Completed".

Survey Complete

This is all for today, hope you find this post useful.

11 January 2012

How to retrieve all the activities for a record

In this post I will show how to retrieve all the activities in which a record is related to as:
  1. BBC Recipient
  2. CC Recipient
  3. Customer Optional Attendee
  4. Organizer
  5. Owner
  6. Regarding
  7. Required attendee
  8. Resource
  9. Sender
  10. To Recipient
It is also possible, of course, to select only few of them. To achieve this I will use a fetch XML query.
Here is  the query:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
  <entity name="activitypointer">
    <attribute name="activitytypecode" />
    <attribute name="subject" />
    <attribute name="statecode" />
    <attribute name="prioritycode" />
    <attribute name="modifiedon" />
    <attribute name="activityid" />
    <attribute name="instancetypecode" />
    <order attribute="modifiedon" descending="false" />
    <link-entity name="activityparty" from="activityid" to="activityid" alias="aa">
      <filter type="and">
        <condition attribute="participationtypemask" operator="in">
        <condition attribute="partyid" operator="eq" uitype="contact" value="Xrm.Page.data.entity.getId()" />

The corresponding advanced find query is:

Hope this helps,