Working with Goals – Rollup Query

rollup

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’

value=’1fc74cd7-c00b-e611-80db-005056a71a16’/></filter></entity></fetch>

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));
 
 service.Create(rollupQuery);
 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.

Thanks.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s