Wednesday, May 21, 2014

How to get/set Form header field in MS CRM 2011 and MS CRM 2013 !!!

There is no supported method in SDK to get and set form header field value using JavaScript. However, there is a workaround to achieve this just place field in Form Header and Form as well (You can hide field from Form in case of you don’t want to show the user). Then you can get and set the field values.

// to get field value
var Value=Xrm.Page.data.entity.attributes.get("new_field").getValue();

// to set field valuenew_field
Xrm.Page.data.entity.attributes.get("new_field").setValue(100);

Tuesday, May 20, 2014

How to set the value of Boolean attribute in MS CRM 2013

Xrm.Page.getAttribute("alb_trigger").setValue(true);

OR


Xrm.Page.getAttribute("alb_trigger").setValue(1);

Why window.showModalDialog does not refresh

Problem
I have some pages that is opened by window.showModalDialog().  But why is it not refreshed?
  • For example, when there is a compile error in the page, it will show it when opening it.  This is correct.  But after I correct the error, it will still show the same error.  I have to terminate ASP.NET worker process by issuing "iisreset" command.  Then it will show the corrected page.
  • Or when some data is changed.  When I open that page, it will still show the old data.  Like it is using the browser's local cache.  After I clear the brower's cache, then it will show the updated data.
Solution

In IE7, go to Tools  |  Internet Options.  Click the Browsing History "Settings" button.  Check for newer versions of stored pages: and select "Everytime I visit the page"

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);

}

Wednesday, May 7, 2014

Find GUID of the current record in MS CRM 2013

In CRM 2013 you do not have option to find record GUID in URL.

to retrieve the GUID of the current record follow the below steps:
1. press F12 key
2. in Console type frames[0].Xrm.Page.data.entity.getId()3.The result is "{101EC4E2-B5D5-E311-93ED-005056B618D8}"

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, ...