Search This Blog

Wednesday, January 29, 2014

Update SubGrid Control at Runtime on Entity Form

Updating Subgrid control on the MS CRM Form.
below example demonstrate the Subgrid on custom entity which will filter Contact in contact sub grid , on search of specific firstname and lastname.

 


function UpdateSubGrid()
        {

            var subgridcontact = window.parent.document.getElementById("contact_subgrid");
            var subgridlead = window.parent.document.getElementById("lead_subgrid");

            var fname = window.parent.Xrm.Page.getAttribute("new_firstname").getValue();
            var lname = window.parent.Xrm.Page.getAttribute("new_lastname").getValue();
            var phonenumber = window.parent.Xrm.Page.getAttribute("new_phonenumber").getValue();

            //If this method is called from the form OnLoad, make sure that the grid is loaded before   proceeding

            if (subgridcontact == null || subgridcontact.readyState != "complete")
             {
                 setTimeout('UpdateSubGrid()', 1000);
                 return;
            }
            if (subgridlead == null || subgridlead.readyState != "complete")
            {
                setTimeout('UpdateSubGrid()', 1000);
                return;
            }

            //Update the fetchXML that will be used by the grid - Search in Contact.
     

            var fetchXml = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>";
            fetchXml += "<entity name='contact'> <attribute name='firstname'/> <attribute name='lastname'/> <attribute name='mobilephone'/>";
            fetchXml += "<order descending='false' attribute='firstname'/>";
            fetchXml += "<filter type='and'>";
            if (fname != null)
            {
                fetchXml += "<condition attribute='firstname' value='" + fname + "%' operator='like'/>";
             
            }
            if (lname != null)
            {
                fetchXml += "<condition attribute='lastname' value='" + lname + "%' operator='like'/>";
            }
            if (phonenumber != null)
            {
                fetchXml += "<condition attribute='mobilephone' value='" + phonenumber + "%' operator='like'/>";
            }
            fetchXml += "</filter>";
            fetchXml += "</entity>";
            fetchXml += " </fetch>";

            subgridcontact.control.SetParameter("fetchXml", fetchXml);
            //Force the subgrid to refresh
            subgridcontact.control.refresh();
       }

Read Entity Attributes in MS CRM 2011

below is the way to read Entity Attributes and Display in GridView

// please connect to your CRM organization using appropriate URL

           IOrganizationService crmservice;
            CrmConnection crmconn = new CrmConnection();
            crmservice = crmconn.Connect2CRMNew(orguri);

            RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest
            {
                EntityFilters = EntityFilters.All,
                LogicalName = entityname,
                RetrieveAsIfPublished = true
            };

            RetrieveEntityResponse retrieveEntityResponse = (RetrieveEntityResponse)crmservice.Execute(retrieveEntityRequest);
            EntityMetadata currentEntity = retrieveEntityResponse.EntityMetadata;
           
            // Attributes
           
            DataTable dtmain = new DataTable();
            dtmain.Columns.Add(new DataColumn("Schema name"));
            dtmain.Columns.Add(new DataColumn("Data Type"));
            dtmain.Columns.Add(new DataColumn("Size"));
            dtmain.Columns.Add(new DataColumn("Required Level"));
            dtmain.Columns.Add(new DataColumn("Is Audit Enabled"));
            dtmain.Columns.Add(new DataColumn("Is Custom Attribute"));
            dtmain.Columns.Add(new DataColumn("AttributeID"));
           
            foreach(AttributeMetadata allattributes in  currentEntity.Attributes)
            {
                DataRow dtrow = dtmain.NewRow();

                dtrow[0] = allattributes.LogicalName.ToString();
                dtrow[3] = allattributes.RequiredLevel.Value.ToString();
                dtrow[4] = allattributes.IsAuditEnabled.Value.ToString();
                dtrow[5] = allattributes.IsCustomAttribute.Value.ToString();
                dtrow[6] = allattributes.MetadataId.Value.ToString();
                dtrow[1] = allattributes.AttributeType.Value;

                switch (allattributes.AttributeType.Value.ToString())
                {
                    //case "Money":
                    //    dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.MoneyAttributeMetadata)(allattributes)).MaxValue.ToString();
                    //    break;
                    //case "Virtual":
                    //    dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.V)(allattributes)).MaxLength.ToString();
                    //    break;
                    case "Decimal":
                        dtrow[2] = ((Microsoft.Xrm.Sdk.Metadata.DecimalAttributeMetadata)(allattributes)).MaxValue.ToString();
                        break;
                    //case "Boolean":
                    //    //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.BooleanAttributeMetadata)(allattributes)).
                    //    break;
                    //case "State":
                    //    //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.StateAttributeMetadata)(allattributes)).
                    //    break;
                    case "String":
                        dtrow[2] = ((Microsoft.Xrm.Sdk.Metadata.StringAttributeMetadata)(allattributes)).MaxLength.ToString();
                        break;
                    //case "DateTime":
                    //    //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.DateTimeAttributeMetadata)(allattributes)).
                    //    break;
                    case "Integer":
                        dtrow[2] = ((Microsoft.Xrm.Sdk.Metadata.IntegerAttributeMetadata)(allattributes)).MaxValue.ToString();
                        break;
                    //case "Lookup":
                    //    //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.LookupAttributeMetadata)(allattributes)).
                    //    break;
                    //case "Uniqueidentifier":
                    //    //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.Uniq)(allattributes)).
                    //    break;
                    //case "Picklist":
                    //    //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)(allattributes)).OptionSet.
                    //    break;



                };
                dtmain.Rows.Add(dtrow);
            }
            gvAttributes.DataSource = dtmain.DefaultView;
            gvAttributes.DataBind();
            pnlattribute.Visible = true;
            Session.Add("TaskTable",dtmain);

Merge Records in MS CRM 2011 using C#

following in the code to Merge 2 records through Asp.Net

 // Connect to CRM 2011
                    IOrganizationService crmservice;
                    CrmConnection conn = new CrmConnection();
                    crmservice = conn.Connect2CRM();

// below are 2 sample account records





                    //D9E2197D-244C-E211-9675-005056B0000B // Test Account - Vilas M
                    //BA59DFF0-E939-E211-946D-005056B0000B // Test Company - Vilas

                    MergeRequest mreq = new MergeRequest();
                    Guid targetAccountID =  new Guid("D9E2197D-244C-E211-9675-005056B0000B");
                    Guid tobemergedAccountID =  new Guid("BA59DFF0-E939-E211-946D-005056B0000B");
                    mreq.Target = new EntityReference("account", targetAccountID);  // Target Account ID , where other account will be mergered
                    mreq.SubordinateId = tobemergedAccountID;

                    Entity entAccnt = new Entity("account");
                    entAccnt.Attributes["Address1_Line1"] = "Test";

                    mreq.UpdateContent = entAccnt;

                    MergeResponse mresp = (MergeResponse)crmservice.Execute(mreq);
                    Console.WriteLine("Sucessfully Merged !! ");

Wednesday, May 15, 2013

Auto Numbering for MS CRM 2011

Implementation Procedure

 

1.     Import Auto-Number solutions and publish customisation

2.     Go to setting > Auto Number

a.     Create entity record ( Make sure you create new record for each entity for which you want to use AutoNumber )

                              i.    Make sure all the required field is completed and entity name and field name is correct (us name not schema name, usually all lowercase)

This is an example:
            
Target field is a field which will hold the auto number which is generated

 

            

For adding criteria to generate AutoNumber you need to add the XML in “field Mapping” column.

The format of the XML has to be as below

 

<?xml version="1.0" encoding="utf-8"?>

<Fields>

    <Field>

      <fieldname>ownershipcode</fieldname>

      <parameter>

        <value>1</value>

        <prefix>PUB</prefix>

        <suffix></suffix>

      </parameter>

      <parameter>

        <value>2</value>

        <prefix>PRV</prefix>

        <suffix></suffix>

      </parameter>

      <parameter>

        <value>3</value>

        <prefix>SUB</prefix>

        <suffix></suffix>

      </parameter>

      <parameter>

        <value>4</value>

        <prefix>OTR</prefix>

         <suffix>INT</suffix>

      </parameter>

    </Field>

  </Fields>

 

 

In the XML above, the field is “ownershipcode” and for each different type of ownership of organization different prefix and suffix can be added.

 

 

For creating AutoNumber without any criteria you need to add the XML in “field Mapping” column.

The format of the XML has to be as below

 

<?xml version="1.0" encoding="utf-8"?>

<Fields>

    <Field>

      <fieldname>NA</fieldname>

      <parameter>

        <value>1</value>

        <prefix>COMM</prefix>

        <suffix></suffix>

      </parameter>

      </Field>

  </Fields>

 

Use the same format and in fieldname use “NA” and add your prefix and Suffix if required

This will generate not conditional Auto Numbers

 
3.     Launch plug-in register(available in CRM SDK) and connect to your deployment

4.     Add trigger in plug-in register – you will find an assembly name (Assembly) counter. Just select it

 

 
 
a.     Then under register, select new step

b.    Make sure you have created the record in Auto Number entity before you register any steps

c.     Make sure you register it as Post-operation pipeline

d.    Make sure you register step for each entity for which you want to use autonumber




 
PS : Solution and Plugin code available at codeplex

https://autonumbercrm2011.codeplex.com/


 

 

CRM 2011 Timeouts and Limits

Timeouts

Limits