Monday 11 March 2013


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

Tuesday 27 November 2012

Multiple entities selection in single look up:


If you create custom lookup in CRM, this lookup is oriented only for one entity.
I want to add possibility to select from some entities in one lookup.

I have custom lookup "originatingleadid". This lookup is oriented on Lead entity. I want to select from views of three different entities: Lead, Contact, User.

For this I've done some customization in opportunity entity, I've created three hidden text fields:
new_type - to save lookup type
new_guid - to save lookup guid
new_name - to save lookup name.
That is all customization.

I've developed three javascript function:

1. function to handle OnChange event on lookup.


function onchange()
{
var originatingLeadValue = Xrm.Page.getAttribute("originatingleadid").getValue();
if (IsNull(originatingLeadValue) || originatingLeadValue[0].type == "4")
{
// Lookup is null or there is a Lead, need to clear hidden fields. To clear one of them will be enough, we will be check this field during onload.
Xrm.Page.getAttribute("new_guid").setValue(null);
}
else
{
// Lookup contains Contact or User, so need to store lookup properties to our hidden fields.
Xrm.Page.getAttribute("new_type").setValue(originatingLeadValue[0].type);
Xrm.Page.getAttribute("new_guid").setValue(originatingLeadValue[0].id);
Xrm.Page.getAttribute("new_name").setValue(originatingLeadValue[0].name);
}
}

2. function to handle OnSave event on Opportuniry Form.

function onsave()
{
var originatingLeadValue = Xrm.Page.getAttribute("originatingleadid").getValue();
if (!IsNull(originatingLeadValue) && originatingLeadValue[0].type != '4')
{
Xrm.Page.getAttribute("originatingleadid").setValue(null);
}
}
 3. function to handle OnLoad event on Opportunity Form.

function onload()
{
lookuptypeIcons = '/_imgs/ico_16_2.gif:/_imgs/ico_16_4.gif:/_imgs/ico_16_8.gif';
lookuptypenames = 'contact:2:Contact,lead:4:Lead,systemuser:8:User';
lookuptypes = '2,4,8';
var savedId = Xrm.Page.getAttribute("new_guid").getValue();
var savedType = Xrm.Page.getAttribute("new_type").getValue();
var savedName = Xrm.Page.getAttribute("new_name").getValue();
var savedEntityName = savedType == "8" ? "systemuser" : "contact";
document.getElementById("originatingleadid").setAttribute("lookuptypes", lookuptypes);
document.getElementById("originatingleadid").setAttribute("lookuptypenames", lookuptypenames);
document.getElementById("originatingleadid").setAttribute("lookuptypeIcons", lookuptypeIcons);
document.getElementById("originatingleadid").setAttribute("defaulttype", "4"); // default type - Lead entity
var originatingLeadValue = Xrm.Page.getAttribute("originatingleadid").getValue();
if (IsNull(originatingLeadValue) && !IsNull(savedId))
{
var value = new Array();
value[0] = new Object();
value[0].displayClass = "ms-crm-Lookup-Item";
value[0].keyValues = new Object();
value[0].values = new Object();
value[0].onclick = "openlui()";
value[0].id = savedId;
value[0].entityType = savedEntityName;
value[0].typename = savedEntityName;
value[0].name = savedName;
value[0].type = savedType;
Xrm.Page.getAttribute("originatingleadid").setValue(value);
}
}
So, now you can select from  three different entity in one lookup :-)
Custom Lookup view

Friday 26 October 2012

Code for creation of new view:

 
System.String layoutXml =
@"<grid name='resultset' object='3' jump='name' select='1' 
    preview='1' icon='1'>
    <row name='result' id='opportunityid'>
    <cell name='name' width='150' /> 
    <cell name='customerid' width='150' /> 
    <cell name='estimatedclosedate' width='150' /> 
    <cell name='estimatedvalue' width='150' /> 
    <cell name='closeprobability' width='150' /> 
    <cell name='opportunityratingcode' width='150' /> 
    <cell name='opportunitycustomeridcontactcontactid.emailaddress1' 
        width='150' disableSorting='1' /> 
    </row>
</grid>";

                    System.String fetchXml =
                    @"<fetch version='1.0' output-format='xml-platform' 
    mapping='logical' distinct='false'>
    <entity name='opportunity'>
    <order attribute='estimatedvalue' descending='false' /> 
    <filter type='and'>
        <condition attribute='statecode' operator='eq' 
        value='0' /> 
    </filter>
    <attribute name='name' /> 
    <attribute name='estimatedvalue' /> 
    <attribute name='estimatedclosedate' /> 
    <attribute name='customerid' /> 
    <attribute name='opportunityratingcode' /> 
    <attribute name='closeprobability' /> 
    <link-entity alias='opportunitycustomeridcontactcontactid' 
        name='contact' from='contactid' to='customerid' 
        link-type='outer' visible='false'>
        <attribute name='emailaddress1' /> 
    </link-entity>
    <attribute name='opportunityid' /> 
    </entity>
</fetch>";

                    SavedQuery sq = new SavedQuery
                    {
                        Name = "A New Custom Public View",
                        Description = "A Saved Query created in code",
                        ReturnedTypeCode = "opportunity",
                        FetchXml = fetchXml,
                        LayoutXml = layoutXml,
                        QueryType = 0
                    };
                    
                    _customViewId = _serviceProxy.Create(sq);
                    Console.WriteLine("A new view with the name {0} was created.", sq.Name);