locked
use FetchXML to get the EntityReference RRS feed

  • Question

  • Hi everyone,

    I am new to CRM 2016 (generally to Microsoft CRM) and I just want to retrieve the field of my "gsc_contactid"(field) value from Marketing List Entity. As I've retrieved it, I want to put it into entityreference (gsc_contactid) to the Phone Call Entity. If someone does know how to code it? The code below is my handler from a plugin.

    -------------------------

    using GSC.CRM.Abenson.BusinessLogic;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace GSC.CRM.Abenson.Platform.Handlers
    {
        public class PhoneHandler
        {
            private readonly IOrganizationService _organizationService;
            private readonly ITracingService _tracingService;
        

            public PhoneHandler(IOrganizationService service, ITracingService trace)
            {
                _organizationService = service;
                _tracingService = trace;
            }  

            public void GetAssignedUser(Entity target) 
            {
                //Prepare the variables to be use.
                Entity phoneCall = _organizationService.Retrieve(GlobalValues.PhoneCall, target.Id,new ColumnSet(true));
           
                string phoneSubject = phoneCall.GetAttributeValue<string>("subject");
                EntityReference assignedTo = new EntityReference();
               // EntityReference regardingObjectId = phoneCall.GetAttributeValue<EntityReference>("regardingobjectid");
                
                //Entity campaignActivity = _organizationService.Retrieve(GlobalValues.CampaignActivity, regardingObjectId.Id, new ColumnSet("gsc_contactid"));
                //EntityReference assignedTo = campaignActivity.GetAttributeValue<EntityReference>("gsc_contactid");
                //Entity marketingList = _organizationService.Retrieve(GlobalValues.MarketingList,,new ColumnSet(true));
                //EntityReference = 
                
                //EntityReference assignedTo = marketingList.GetAttributeValue<EntityReference>("gsc_contactid");
                
                //Entity marketingList = _organizationService.Retrieve("list",)

                //GET GSC_CONTACTID (ASSIGNED PORTAL USER) IN CORRESPONDING MARKETING LIST
                string fetch = @"
                <fetch distinct='true' mapping='logical' output-format='xml-platform' version='1.0'>
                    <entity name='list'>
                        <attribute name='listname'/>
                        <attribute name='listid'/>
                        <attribute name='gsc_contactid'/>
                        <order descending='true' attribute='listname'/>
                    <link-entity name='campaignactivityitem' intersect='true' visible='false' to='listid' from='itemid'>
                    <link-entity name='campaignactivity' to='campaignactivityid' from='activityid' alias='ap'>
                    <link-entity name='phonecall' to='activityid' from='regardingobjectid' alias='aq'>
                        <filter type='and'>
                            <condition attribute='activityid' value='"+ target.Id + @"' uitype='phonecall' uiname='"+ phoneSubject +@"' operator='eq'/>
                        </filter>
                    </link-entity>
                    </link-entity>
                    </link-entity> 
                    
                    </entity>
                </fetch>
                        ";

                EntityCollection result = _organizationService.RetrieveMultiple(new FetchExpression(fetch));
                
                foreach (var c in result.Entities) 
                {

                    //assignedTo = c.GetAttributeValue<EntityReference>("gsc_contactid"); 
                    //assignedTo = c.GetAttributeValue<EntityReference>("gsc_contactid"); 
                    _tracingService.Trace(c.ToString()); 
                    //_tracingService.Trace(c["phonecall"].ToString()); 
                    //phoneCall["gsc_contactid"] = new EntityReference("contact", new Guid(c["gsc_contactid"].ToString())); 
                       
                }

                //Check field in CRM.
                //If logic - if contains, set assign to if not contains add gsc_contactid.
                //if (phoneCall.Attributes.Contains("gsc_contactid"))
                //{
                //    phoneCall["gsc_contactid"] = assignedTo;
                //} 
                //else
                //{
                //    phoneCall.Attributes.Add("gsc_contactid", assignedTo);
                //}
                //Update the phone call activity created.
                _organizationService.Update(phoneCall);

                //Trace purposes.
                #region Trace
                /*
                foreach (var item in phoneCall.Attributes)
                {
                    _tracingService.Trace(item.ToString());
                }
                */
                #endregion
            }
            

        }
    }


    Monday, December 19, 2016 1:36 AM

All replies

  • //Retreive the value of contact
    assignedTo = c.GetAttributeValue<EntityReference>("gsc_contactid");                    
    
    
    // Create an Activity Party record for each Contact.
                        var activityParty = new ActivityParty
                        {
                            PartyId = new EntityReference("contact",assignedTo.Id ),
                        };
    
    
    To = new ActivityParty[] { activityParty }
    
    
    
    phoneCall["gsc_contactid"] = new EntityReference("contact", assignedTo.Id);


    Regards Faisal


    • Edited by Faisal Fiaz Monday, December 19, 2016 2:51 PM
    Monday, December 19, 2016 2:51 PM
  • Hi Justin,

    Try with below

     //Check field in CRM.
                //If logic - if contains, set assign to if not contains add gsc_contactid.
                if (phoneCall.Attributes.Contains("gsc_contactid") && phoneCall.Attributes["gsc_contactid"] != null)
                {
                    phoneCall.Attributes.Remove("gsc_contactid");
                    phoneCall.Attributes.Add("gsc_contactid", new EntityReference(assignedTo.LogicalName, assignedTo.Id));
                    //phoneCall["gsc_contactid"] = assignedTo;
                }
                else
                {
                    phoneCall.Attributes.add("gsc_contactid", new EntityReference(assignedTo.LogicalName, assignedTo.Id));
                    //phoneCall.Attributes.Add("gsc_contactid", assignedTo);
                }
                //Update the phone call activity created.
                _organizationService.Update(phoneCall);

    Please don't forget to Vote and marked as answer If this post answers your question or was helpful, please click "Mark As Answer" on the post and "Mark as Helpful" Be wise..:)

    Tuesday, December 20, 2016 8:23 AM
  • Hi everyone,

    I have a big problem so I think you guys can help me.:) I have a plugin that throws two Marketing List (Entity) from the fetchXML I construct. I have a scenario, I have a Campaign Activity (PhoneCall_001) with two Marketing List (ML_01, ML_02) and each Marketing List has a custom field named "gsc_contactid".

    Now, once I've distributed campaign activity (phonecall), each phone call should have retrieved its corresponding "gsc_contactid" (Phone Call entity has also a custom field named gsc_contactid). The plugin should transfer the value of "gsc_contactid" from Marketing List to the PhoneCall["gsc_contactid"].

    I have a code here but it retrieves the two Marketing List (ML_01, ML_02) from the Phone Call activity which is correct. When I execute the plugin, ML_01 has its correct "gsc_contactid". The BIG PROBLEM is ML_02 also gets the "gsc_contactid" value of ML_01. If anyone knows how to correct my code.

                                                                                                                         

    using GSC.CRM.Abenson.BusinessLogic;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace GSC.CRM.Abenson.Platform.Handlers
    {
        public class PhoneHandler
        {
            private readonly IOrganizationService _organizationService;
            private readonly ITracingService _tracingService;


            public PhoneHandler(IOrganizationService service, ITracingService trace)
            {
                _organizationService = service;
                _tracingService = trace;
            }  

            public void GetAssignedUser(Entity target) 
            {
                //Prepare the variables to be use.
                Entity phoneCall = _organizationService.Retrieve(GlobalValues.PhoneCall, target.Id,new ColumnSet(true));

                string phoneSubject = phoneCall.GetAttributeValue<string>("subject");
                EntityReference assignedTo = new EntityReference();


                //GET GSC_CONTACTID (ASSIGNED PORTAL USER) IN CORRESPONDING MARKETING LIST
                string fetch = @"
                <fetch distinct='true' mapping='logical' output-format='xml-platform' version='1.0'>
                    <entity name='list'>
                        <attribute name='listname'/>
                        <attribute name='listid'/>
                        <attribute name='gsc_contactid'/>
                        <order descending='true' attribute='listname'/>
                    <link-entity name='campaignactivityitem' intersect='true' visible='false' to='listid' from='itemid'>
                    <link-entity name='campaignactivity' to='campaignactivityid' from='activityid' alias='ap'>
                    <link-entity name='phonecall' to='activityid' from='regardingobjectid' alias='aq'>
                        <filter type='and'>
                            <condition attribute='activityid' value='" + target.Id + @"' uiname='" + phoneSubject + @"' uitype='phonecall' operator='eq'/>
                        </filter>
                    </link-entity>
                    </link-entity>
                    </link-entity> 

                    </entity> 
                </fetch>
                        ";

                EntityCollection result = _organizationService.RetrieveMultiple(new FetchExpression(fetch));


                foreach (Entity c in result.Entities)  
                {
                    assignedTo = c.GetAttributeValue<EntityReference>("gsc_contactid");


                    if (phoneCall.Attributes.Contains("gsc_contactid"))
                    { 
                        phoneCall["gsc_contactid"] = assignedTo;
                    }
                    else
                    {
                        phoneCall.Attributes.Add("gsc_contactid", assignedTo);
                    }

                    //_tracingService.Trace(assignedTo.ToString());
                    //_tracingService.Trace(phoneSubject.ToString());


                }

                //Check field in CRM.
                //If logic - if contains, set assign to if not contains add gsc_contactid.

                //Update the phone call activity created.
                _organizationService.Update(phoneCall);

                //Trace purposes.
                #region Trace
                /*
                foreach (var item in phoneCall.Attributes)
                {
                    _tracingService.Trace(item.ToString());
                }
                */
                #endregion
            }


        }
    }

    Thanks a lot. <3

    Wednesday, December 21, 2016 3:11 AM