locked
How to fetch related entity record through Plugin code in CRM 2011? RRS feed

  • Question

  • Hi,

    I want to know that how can we fetch related entity records through Plugin code.

    My scenario is like below.

    I am writing a plugin on Product entity.

    I have a field on Order form. I want to fetch that field value in related Product form of that Order.

    Please help !!!

    Thursday, January 10, 2013 12:05 PM

Answers

  • Hi Milan,

    Instead of using fetchXML better to use Retrieve method direcly. use the following code

     _OrderProductid = _Entity.Id;
    _Orderid = ((EntityReference)(_Entity.Attributes["salesorderid"])).Id;
    Entity _Order = _Service.Retrieve("salesorder", _Orderid , new ColumnSet(new string[] { "orderfield1", "orderfield1", "orderfield1"}));

     // Read  fields from Order Entity
     if (_Order.Attributes.Contains("orderfield1"))
    {
     Decimal Value = ((Money)_Order.Attributes["orderfield1"]).Value;
    }

    I am assuming you are writing plugin in Order Product entity.

    _OrderProductid = _Entity.Id --- here _Entity is Target entity that you are reading from the context.

     _Entity = (Entity)context.InputParameters["Target"];

    And also i am assuming orderfield1 is in order form is Currency data type.


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    • Marked as answer by DynamicsCRM31 Thursday, January 10, 2013 1:47 PM
    Thursday, January 10, 2013 1:11 PM

All replies

  • This is a working code example
                                string fetchXml = @"<fetch distinct='false' mapping='logical'>" +
                                                       "<entity name='new_servicecategorymapping_systemuser'><filter type='and'>" +
                                                       "<condition attribute='new_servicecategorymappingid' operator='eq' value='" + MappingId + "' />" +
                                                       "</filter></entity></fetch>";
                                FetchXmlToQueryExpressionRequest fxmlReq = new FetchXmlToQueryExpressionRequest() { FetchXml = fetchXml };
                                FetchXmlToQueryExpressionResponse expression = (FetchXmlToQueryExpressionResponse)service.Execute(fxmlReq);
                                var request = new RetrieveMultipleRequest { Query = expression.Query };
                                EntityCollection entityList = ((RetrieveMultipleResponse)service.Execute(request)).EntityCollection;
                                Email newEmail = new Email();
                                newEmail.Subject = "A case has been assigned to your team";
                                newEmail.Description = "Case is attached in the regarding field.";
                                EntityCollection collToParty = new EntityCollection();
                                collToParty.EntityName = "systemuser";
                                for (int e = 0; e < entityList.Entities.Count; e++)
                                {
                                    EntityReference to = new EntityReference("systemuser", new Guid(entityList.Entities[e].Attributes["systemuserid"].ToString()));
                                    Entity toParty = new Entity("activityparty");
                                    toParty.Attributes.Add("partyid", to);
                                    collToParty.Entities.Add(toParty);
                                }
                                newEmail.Attributes.Add("to", collToParty);



    Regards Faisal

    • Proposed as answer by MubasherSharif Thursday, January 10, 2013 12:35 PM
    Thursday, January 10, 2013 12:14 PM
  • Hi,
       You can use the following fetchXML to get "totalamount" field from Order entity. Get the orderid from the Order lookup on the order prodcut form and set that value to "Id" variable in the following fetchXML.

                var fetchXml =
                    @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'><entity name='salesorder'><attribute name='totalamount' /><filter type='and'><condition attribute='salesorderid' operator='eq' uiname='test' uitype='salesorder' value='"+Id+"' /></filter></entity></fetch>";
    
                var fetchExpression = new FetchExpression(fetchXml);
                EntityCollection fetchResult =
                    _serviceProxy.RetrieveMultiple(fetchExpression);

    You can easily get the fetchXML from Advance Find window's "Download FetchXML" button once you prepare your query. Hope it helps.

       

    Vikranth http://howto-mscrm.blogspot.com "Please Mark it as answer if it helps in resolving your query"

    Thursday, January 10, 2013 12:26 PM
  • Hi Milan,

    Instead of using fetchXML better to use Retrieve method direcly. use the following code

     _OrderProductid = _Entity.Id;
    _Orderid = ((EntityReference)(_Entity.Attributes["salesorderid"])).Id;
    Entity _Order = _Service.Retrieve("salesorder", _Orderid , new ColumnSet(new string[] { "orderfield1", "orderfield1", "orderfield1"}));

     // Read  fields from Order Entity
     if (_Order.Attributes.Contains("orderfield1"))
    {
     Decimal Value = ((Money)_Order.Attributes["orderfield1"]).Value;
    }

    I am assuming you are writing plugin in Order Product entity.

    _OrderProductid = _Entity.Id --- here _Entity is Target entity that you are reading from the context.

     _Entity = (Entity)context.InputParameters["Target"];

    And also i am assuming orderfield1 is in order form is Currency data type.


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    • Marked as answer by DynamicsCRM31 Thursday, January 10, 2013 1:47 PM
    Thursday, January 10, 2013 1:11 PM
  • Hi _Vikram,

    Thanks a lot. Its helped me.

    Thursday, January 10, 2013 1:48 PM