Wednesday, May 14, 2014

Open an entity record in a new window in MS CRM 2013 by Javascript

In CRM 2013 opening an entity record differs from previous versions. While CRM 2011 was all about popup windows CRM 2013 has a flat HTML5 navigation style. But you can still open a record in a popup window.
To open an entity record in a new window pass the following query string parameters.
etnThe logical name of the entity
extraqsOptional for forms (see Microsoft Dynamics CRM SDK for 2013 documentation)
pagetypeentityRecord
idThe id of the entity
newWindowtrue (undocumented)
histKeya random numeric sequence (undocumented)
The following sample shows the URL and query string parameters to open an account record in a new window:
window.open("https://contoso.crm.dynamics.com/main.aspx?etn=account&extraqs=&histKey=469645694&id={02c2e648-0acd-41f0-9ca8-d8d131e5f47b}&newWindow=true&pagetype=entityrecord","","status=0,resizable=1,width=1000px,height=600px");
click here for more info.

Ribbondiffxml for Custom Button on Form, Home, Sub-Grid in MS CRM 2011 and MS CRM 2013

<RibbonDiffXml>
        <CustomActions>
          <CustomAction Id="alb.alb_aiscontract.Button1.Button.CustomAction" Location="Mscrm.Form.alb_aiscontract.MainTab.Save.Controls._children" Sequence="65">
            <CommandUIDefinition>
              <Button Command="alb.alb_aiscontract.Command0.Command" Id="alb.alb_aiscontract.Button1.Button" Image32by32="$webresource:alb_aiscontractImage32" Image16by16="$webresource:alb_aiscontractImage16" LabelText="$LocLabels:alb.alb_aiscontract.Button1.Button.LabelText" Sequence="65" TemplateAlias="o2" ToolTipTitle="$LocLabels:alb.alb_aiscontract.Button1.Button.ToolTipTitle" />
            </CommandUIDefinition>
          </CustomAction>
          <CustomAction Id="alb.alb_aiscontract.Button2.Button.CustomAction" Location="Mscrm.HomepageGrid.alb_aiscontract.MainTab.Management.Controls._children" Sequence="45">
            <CommandUIDefinition>
              <Button Command="alb.alb_aiscontract.Command1.Command" Id="alb.alb_aiscontract.Button2.Button" Image32by32="$webresource:alb_aiscontractImage32" Image16by16="$webresource:alb_aiscontractImage16" LabelText="$LocLabels:alb.alb_aiscontract.Button2.Button.LabelText" Sequence="45" TemplateAlias="o2" ToolTipTitle="$LocLabels:alb.alb_aiscontract.Button2.Button.ToolTipTitle" />
            </CommandUIDefinition>
          </CustomAction>
          <CustomAction Id="alb.alb_aiscontract.Button3.Button.CustomAction" Location="Mscrm.SubGrid.alb_aiscontract.MainTab.Management.Controls._children" Sequence="45">
            <CommandUIDefinition>
              <Button Command="alb.alb_aiscontract.Command1.Command" Id="alb.alb_aiscontract.Button3.Button" Image32by32="$webresource:alb_aiscontractImage32" Image16by16="$webresource:alb_aiscontractImage16" LabelText="$LocLabels:alb.alb_aiscontract.Button3.Button.LabelText" Sequence="45" TemplateAlias="o1" ToolTipTitle="$LocLabels:alb.alb_aiscontract.Button3.Button.ToolTipTitle" />
            </CommandUIDefinition>
          </CustomAction>
        </CustomActions>
        <Templates>
          <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
        </Templates>
        <CommandDefinitions>
          <CommandDefinition Id="alb.alb_aiscontract.Command0.Command">
            <EnableRules />
            <DisplayRules>
              <DisplayRule Id="alb.alb_aiscontract.DisplayRule0.DisplayRule" />
            </DisplayRules>
            <Actions>
              <JavaScriptFunction FunctionName="CreateAISContractVersion" Library="$webresource:alb_aiscontract">
                <CrmParameter Value="FirstPrimaryItemId" />
              </JavaScriptFunction>
            </Actions>
          </CommandDefinition>
          <CommandDefinition Id="alb.alb_aiscontract.Command1.Command">
            <EnableRules>
              <EnableRule Id="alb.alb_aiscontract.EnableRule0.EnableRule" />
            </EnableRules>
            <DisplayRules />
            <Actions>
              <JavaScriptFunction FunctionName="CreateAISContractVersion" Library="$webresource:alb_aiscontract">
                <CrmParameter Value="SelectedControlSelectedItemIds" />
              </JavaScriptFunction>
            </Actions>
          </CommandDefinition>
        </CommandDefinitions>
        <RuleDefinitions>
          <TabDisplayRules />
          <DisplayRules>
            <DisplayRule Id="alb.alb_aiscontract.DisplayRule0.DisplayRule">
              <FormStateRule State="Create" InvertResult="true" />
            </DisplayRule>
          </DisplayRules>
          <EnableRules>
            <EnableRule Id="alb.alb_aiscontract.EnableRule0.EnableRule">
              <SelectionCountRule AppliesTo="SelectedEntity" Minimum="1" Maximum="1" />
            </EnableRule>
          </EnableRules>
        </RuleDefinitions>
        <LocLabels>
          <LocLabel Id="alb.alb_aiscontract.Button1.Button.LabelText">
            <Titles>
              <Title description="Create Version" languagecode="1033" />
            </Titles>
          </LocLabel>
          <LocLabel Id="alb.alb_aiscontract.Button1.Button.ToolTipTitle">
            <Titles>
              <Title description="Create Version" languagecode="1033" />
            </Titles>
          </LocLabel>
          <LocLabel Id="alb.alb_aiscontract.Button2.Button.LabelText">
            <Titles>
              <Title description="Create Version" languagecode="1033" />
            </Titles>
          </LocLabel>
          <LocLabel Id="alb.alb_aiscontract.Button2.Button.ToolTipTitle">
            <Titles>
              <Title description="Create Version" languagecode="1033" />
            </Titles>
          </LocLabel>
          <LocLabel Id="alb.alb_aiscontract.Button3.Button.LabelText">
            <Titles>
              <Title description="Create Version" languagecode="1033" />
            </Titles>
          </LocLabel>
          <LocLabel Id="alb.alb_aiscontract.Button3.Button.ToolTipTitle">
            <Titles>
              <Title description="Create Version" languagecode="1033" />
            </Titles>
          </LocLabel>
        </LocLabels>
      </RibbonDiffXml>

Use OData service to Create a Record through Javascript

function CreateAISContractVersion(aisContractId)
{
     var originRecordType = "alb_aiscontract";
     var originRecordGuid = aisContractId;

    var cloneData = {};
    var activityId;
    var serverUrl = document.location.protocol + "//" + document.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
    var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/" +  originRecordType + "Set?$select=*&$filter=" + originRecordType + "Id eq guid'"
                   + originRecordGuid + "'";
   jQuery.support.cors = true;
    jQuery.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: oDataUri,
        async: false, //Synchronous operation
        beforeSend: function (XMLHttpRequest) {
            //Specifying this header ensures that the results will be returned as JSON.          
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (data && data.d && data.d.results) {
                cloneData = data.d.results[0];
                //Here insert the code to skip/transform fields such as Record Id, Date fields, etc..
                replacer = function (key, value) {
                    if (key == "ModifiedOn" || key == originRecordType + "Id" ||
                        key == "CreatedOn" || key == "StateCode" ||
                        key == "StatusCode") {
                      
                        return undefined;
                    } else if (key == "alb_contractdate" || key == "alb_warrantyperiodstartdate"|| key == "alb_warrantyperiodenddate") {
                        if (value) {
                            var date =
                            eval('new ' +
                               value.replace("/", "").replace("/", ""));
                            return date.format('yyyy-MM-dd'); //Format the date
                        }
                    }
                                                                                else if (key == "alb_version")     {
                                                                                                if (value) {                           
                            return (parseInt(value) + 1).toString(); //Format the date
                        }
                                                                                }
                    else return value;
                }
                //Create new Activity
                var oDataUri = serverUrl +
                               "/xrmservices/2011/OrganizationData.svc/" +
                               originRecordType + "Set";
                jQuery.support.cors = true;
                jQuery.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    datatype: "json",
                    url: oDataUri,
                    async: false, //Synchronous operation
                    data: JSON.stringify(cloneData, replacer),
                    beforeSend: function (XMLHttpRequest) {
                        //Specifying this header ensures that the results will be returned as JSON.          
                        XMLHttpRequest.setRequestHeader("Accept","application/json");
                    },
                    success: function (data, textStatus, XmlHttpRequest) {
                        if (data && data.d) {
                            activityId = data.d.ActivityId;
                        }
                    },
                    error: function (XmlHttpRequest, textStatus, errorThrown) {
                      alert("Error :  has occured during creation of the activity "  + originRecordType.text + ": " +
                             XmlHttpRequest.responseText);
                    }
                });
            } else {
                //No data returned
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
            alert("Error :  has occured during retrieving of the activity "
                    + originRecordType.text + ": " +
                    XmlHttpRequest.responseText);
        }
    });

    alert(activityId);

}

Split the String values with a special character in MS Flow to convert this into Array

 Many times we have a requirement to prepare the Mailing address for some of the documents, suppose there are Address Line1, Address Line2, ...