Working with Goals – Rollup Query


We can create Rollup Queries using CRM SDK (plugin or workflow or some external tool). In this entity, MS CRM provided similar to advance find capability for Query attribute, CRM stores it in database as ‘fetchxml’ attribute in fetch-XML (XML) format.

Thus, we can create edit that attribute with code behind.  Here is the sample of it,

<fetch version=’1.0′ output-format=’xml-platform’ mapping=’logical’ distinct=’false’>

<entity name=’opportunity’><attribute name=’name’/><attribute name=’customerid’/>

<attribute name=’estimatedvalue’/><attribute name=’statuscode’/>

<attribute name=’opportunityid’/><order attribute=’name’ descending=’false’/>

<filter type=’and’>

<condition attribute=’hsb_productlineid’ operator=’eq’ uiname=’Softonovation’

uitype=’product’ value=’d369f8da-c20b-e611-80dw-001056a71a16’/>

<condition attribute=’hsb_regionid’ operator=’eq’ uiname=’India’ uitype=’territory’


If you observe little, you will learn that we can modify it by pretty easily, wont you?

I used switch – case for it based on my conditions, I was modifying region, product and their GUID.

Here, is the sample to create Rollup Query

private Guid CreateRollupQuery(Entity rollupQueryData, IOrganizationService service)
 Entity rollupQuery = new Entity("goalrollupquery");

 rollupQuery.Attributes["name"] = (rollupQueryData.Contains("name") ? (rollupQueryData.Attributes["name"].ToString()) : "");
 rollupQuery.Attributes["queryentitytype"] = "opportunity";
 rollupQuery.Attributes["fetchxml"] = getFetchXml(rollupQueryData.Attributes["name"].ToString(), service);
 rollupQuery.Attributes["ownerid"] = rollupQueryData.Attributes["ownerid"] = new EntityReference("systemuser",
 gen.getId("systemuser", "domainname", ConfigurationManager.AppSettings["UserName"], service));
 regionId = Guid.Empty;
 return new Guid();

In above code ‘getFetchXml’ is a function who returns string value of fetch XML with my requirements, we may keep there fetch XML as is in string format also.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s