Wednesday 11 July 2012

Advanced Find for Fetch XML in crm 2011

senario:

if we want to select all opportunity products relating to opportunity:
1. need to select the advanced find.
2. choose the opportunity product entity.
3. check the relation between opportunity and opp.product.
if the 1:N relation name is new_opportunityId and display name is :"opportunity".
in condition:
4. select the opportunity .
5. eqal
6. select the opportunity name.

we will get the Fetch XML like:


<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
- <entity name="new_opportunityitproduct">
  <attribute name="new_opportunityitproductid" />
  <attribute name="new_name" />
  <attribute name="createdon" />
  <order attribute="new_name" descending="false" />
- <filter type="and">
  <condition attribute="new_opportunityid" operator="eq" uiname="test dates" uitype="opportunity" value="{77470EB8-19CB-E111-9E1C-00155D000B45}" />
  </filter>
  </entity>
  </fetch>


if you want dynamically. remove the value and give the id like:

value="+leadids+"

but in the code we need to give it only single quotation.

In c# code we need give like this:

string fetchquery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
  "<entity name='new_leaditproduct'>" +
    "<attribute name='new_leaditproductid' />" +
    "<attribute name='new_name' />" +
    "<attribute name='new_category' />" +
    "<attribute name='new_subcategory' />" +
    "<attribute name='new_productservice' />" +
    "<attribute name='createdon' />" +
    "<order attribute='new_name' descending='false' />" +
    "<filter type='and'>" +
      "<condition attribute='new_productid' operator='eq' uitype='lead' value='" + var1.Id + "' />" +
    "</filter>" +
  "</entity>" +
"</fetch>";

then read the fetch xml:

RetrieveMultipleRequest req = new RetrieveMultipleRequest();
                    FetchExpression fetch = new FetchExpression(fetchquery);
                    req.Query = fetch;
                    RetrieveMultipleResponse resp = (RetrieveMultipleResponse)service.Execute(req);

                    //oipt.Id = leadids;

                    EntityCollection col = resp.EntityCollection;

reading the values.

Entity opp_product = new Entity();
                    opp_product.LogicalName = "new_opportunityitproduct";
                    EntityReference opp = new EntityReference();
                    opp.Id = entity.Id;
                    opp.LogicalName = entity.LogicalName;

foreach (var c in col.Entities)
                    {
                        opp_product["new_category"] = (EntityReference)c["new_category"];
                        opp_product["new_subcategory"] = (EntityReference)c["new_subcategory"];
                        opp_product["new_productservice"] = (EntityReference)c["new_productservice"];
                        opp_product["new_price"] = new Money() { Value = 0 };
                        opp_product["new_opportunityid"] = (EntityReference)opp;
                        service.Create(opp_product);
                    }

No comments:

Post a Comment