Monday 11 March 2013


How to have a custom filtering on lockup using form java script in mscrm 2011: by Srinivas kalwakuntla





function Accountview() {

    var account = Xrm.Page.getAttribute('tls_parentaccount');
    var accountid;

    if (account != null) {

        var lookUpObjectValue = account.getValue();

        if ((lookUpObjectValue != null)) {

            var lookuptextvalue = lookUpObjectValue[0].name;

            var accountid = lookUpObjectValue[0].id;

        }


        if (!IsNull(accountid)) {
            var viewId = "{1DFB2B35-B07C-44D1-868D-258DEEAB88E2}";
            var entityName = "account";
            var viewDisplayName = "Default View For Account";


            var fetchXml = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">' +
                     '<entity name="account">' +
                        '<attribute name="name" />' +
                         '<attribute name="sic" />' +
                        '<attribute name="accountid" />' +
                         '<attribute name="address1_city" />' +
                         '<attribute name="telephone1" />' +
                         '<attribute name="primarycontactid" />' +
                         '<filter type="and">' +
                          '<condition attribute="accountid" operator="ne" value="' + accountid + '"/>' +
                        '</filter>' +
                     '</entity>' +
                  '</fetch>';

            // build Grid Layout
            var layoutXml = "<grid name='resultset' " +
                "object='1' " +
                "jump='account' " +
                "select='0' " +
                "icon='0' " +
                "preview='1'>" +
                "<row name='result' " +
                "id='accountid'>" +
                "<cell name='name' " +
                "width='200' />" +
                "<cell name='primarycontactid' " +
                "width='150' />" +
                "<cell name='sic' " +
                "width='150' />" +
                "<cell name='address1_city' " +
                "width='150' />" +
                "<cell name='telephone1' " +
                "width='150' />" +
                "</row>" +
                "</grid>";

            Xrm.Page.getControl("new_newemployer").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
            Xrm.Page.getControl("new_previousemployer").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);

        }

    }
}





Javascript code for phone number format in mscrm 2011:by srinivas kalwakuntla


//Phone Number Format
function FormatPhoneNumber(context) {
    var oField = context.getEventSource().getValue();
    var sTmp = oField;

    if (typeof (oField) != "undefined" && oField != null) {
        sTmp = oField.replace(/[^0-9]/g, "");
        switch (sTmp.length) {
            case 10:
                sTmp = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
                break;


            default:
                alert("Phone Number must contain 10 numbers.");
                break;
        }
    }
    context.getEventSource().setValue(sTmp);
}



Mscrm 2011 plugin registered on Associate message code:


 //Getting the Relationship Name ,Target(Practice group id) and Relted entity (Account ID)


                    if (!context.InputParameters.Contains("Relationship")) { return; }
                    Relationship relationship = (Relationship)context.InputParameters["Relationship"];
                    if (relationship.SchemaName != "lrl_lrl_practicegroup_account") { return; }
                    if (!context.InputParameters.Contains("Target")) { return; }
                    EntityReference target = (EntityReference)context.InputParameters["Target"];
                    Guid practiceGroupId = target.Id;
                    if (!context.InputParameters.Contains("RelatedEntities")) { return; }
                    EntityReferenceCollection related = (EntityReferenceCollection)context.InputParameters["RelatedEntities"];
                    foreach (EntityReference entityReferenceObj in related)
                    {
                        accountId = entityReferenceObj.Id;
                    }
                     


Mscrm 2011 plugin: direct fetch XML syntax  used for retrieve:

 public List<Guid> GetRelatedPracticeGroupIds(IOrganizationService service, Guid accountId)
        {
            List<Guid> PracticeGroupIds = new List<Guid>();
            string fetchXml = @"<fetch version=""1.0"" output-format=""xml-platform"" mapping=""logical"" distinct=""true"">
                      <entity name=""lrl_practicegroup"">
                        <attribute name=""lrl_practicegroupid"" />
                        <order attribute=""lrl_name"" descending=""false"" />
                        <link-entity name=""lrl_lrl_practicegroup_account"" from=""lrl_practicegroupid""    to=""lrl_practicegroupid"" visible=""false"" intersect=""true"">
                          <link-entity name=""account"" from=""accountid"" to=""accountid"" alias=""ac"">
                            <filter type=""and"">
                              <condition attribute=""accountid"" operator=""eq"" uitype=""account"" value=""" +               accountId + @""" />
                            </filter>
                          </link-entity>
                        </link-entity>
                      </entity>
                    </fetch>";

            EntityCollection Result = service.RetrieveMultiple(new FetchExpression(fetchXml));
            if (Result.Entities.Count > 0)
            {

                foreach (Entity node in Result.Entities)
                {
                    PracticeGroupIds.Add(node.Id);
                }
            }
            return PracticeGroupIds;



        }
     
        }



Mscrm C# code for associate and disassociate:

 #region Associate

                                        AssociateRequest associatePracticeGroup = new AssociateRequest
                                        {
                                            Target = new EntityReference("lrl_practicegroup", practiceGroupId),
                                            RelatedEntities = new EntityReferenceCollection
                                          {
                                          new EntityReference("account", parentAccountId)
                                         },
                                            Relationship = new Relationship("lrl_lrl_practicegroup_account")
                                        };

                                        // Execute the request.
                                        service.Execute(associatePracticeGrou
  #endregion

 #region DisAssociate
 DisassociateRequest disassociatePracticeGroup = new DisassociateRequest
                                            {
                                                Target = new EntityReference("lrl_practicegroup", practiceGroupId),
                                                RelatedEntities = new EntityReferenceCollection
                                             {
                                             new EntityReference("account", parentAccountId)
                                              },
                                                Relationship = new Relationship("lrl_lrl_practicegroup_account")
                                            };

                                            // Execute the request.
                                            service.Execute(disassociatePracticeGroup);

  #endregion