Retrieve "Applicable When" and "Success Criteria" from Dynamics CRM SLA programmatically - c#

Currently developing a console application to check all the SLAs in the Dynamics 365 instance.
How to retrieve the "applicable when" and "success criteria" from Dynamics CRM SLA programmatically?

That information is stored in the applicablewhenxml and successconditionsxml fields of the slaitem entity. You can use the following FetchXML to retrieve them:
<fetch>
<entity name="slaitem" >
<attribute name="applicablewhenxml" />
<attribute name="successconditionsxml" />
</entity>
</fetch>
As you can imagine from the names, the information is stored as XML. For example, for a success condition of Status equals to Active:
<and>
<condition>
<column id="colEntity" value="incident" />
<column id="colAttribute" value="statecode" />
<column id="colOperator" value="eq" />
<column id="colStaticValue" value="0" dataslugs="" />
</condition>
</and>

Related

Avoid Aliases in result from FetchXml with linked entities

I have the following FetchXML:
<fetch>
<entity name="list" >
<attribute name="listname" />
<attribute name="listid" alias="List" />
<filter type="and" >
<condition attribute="listname" operator="eq" value="Test 1" />
</filter>
<link-entity name="listmember" from="listid" to="listid" intersect="true" alias="listmember" >
<attribute name="entitytype" />
<attribute name="listmemberid" />
<attribute name="entityid" />
<link-entity name="contact" from="contactid" to="entityid" alias="contact" >
<attribute name="contactid" />
<attribute name="owneridname" />
<attribute name="owneridtype" />
<attribute name="ownerid" />
</link-entity>
</link-entity>
</entity>
The result looks like this:
<resultset morerecords="0" paging-cookie="<cookie page="1"><listid lastnull="1" firstnull="1" /></cookie>">
<result>
<listname>Test 1</listname>
<listmember.entitytype formattedvalue="2">2</listmember.entitytype>
<listmember.listmemberid>{6739D9B9-xxxx-xxxx-xxxx-000D3A3852A3}</listmember.listmemberid>
<listmember.entityid type="2">{039FD4C6-xxxx-xxxx-xxxx-000D3A385A1C}</listmember.entityid>
<contact.contactid>{039FD4C6-xxxx-xxxx-xxxx-000D3A385A1C}</contact.contactid>
<contact.ownerid name="CRM Test" dsc="" type="8" yomi="CRM Test">{5ABA5CBA-xxxx-xxxx-xxxx-D472F64781F6}</contact.ownerid>
</result>
</resultset>
My issue now is that I have a generic way to retrieve the attributes returned by a FetchXml. But since this one has linked entites the aliases are added to the result, like:
<listmember.listmemberid>
So my retrieve will throw an error since I am looking for "listmemberid"
Is there a way to avoid having these aliases added to the result? Specially since the attribute names are unique?
Any ideas to get around this issue?
The only option to override the alias of the select column which is working - when you do aggregate function and I tested the below working example.
<fetch top="50" aggregate="true" >
<entity name="account" >
<attribute name="businesstypecode" alias="test" groupby="true" />
<link-entity name="contact" from="accountid" to="accountid" link-type="inner" alias="acc" >
<attribute name="accountrolecode" alias="testing" groupby="true" />
</link-entity>
</entity>
</fetch>
Result:
test testing
1 3
1
This didn't work. I don't know why, maybe limitation of fetchxml.
<fetch top="50" >
<entity name="contact" >
<attribute name="fullname" />
<link-entity name="account" from="accountid" to="accountid" alias="acct" >
<attribute name="name" alias="acctNAME" />
</link-entity>
</entity>
</fetch>
Result:
fullname acct.name
arun account arun account
Even with the Query Expression, you will get a special datatype called AliasedValue to handle this, so this is intended behavior. Read more
You get these aliases and then it’s value because you are retrieving data from linked entity. Whenever you do that I.e retrieving from linked entity you will have to use aliases, as far as I know you cannot circumvent it.
What I could suggest is if you need data only from ListMember entity and not from List and contact,
Create your fetch based on list member entity such as
select * from listmember where listname="Test 1"
But as soon as you need data from related entity, that related entity shall be aliased.

SQL Server bulk insert XML format file with Maximum LENGTH

I want to set the length to MAX for one of my XML fields.
XML Code:
<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
<FIELD ID="1" xsi:type="CharFixed" LENGTH="3" />
<FIELD ID="2" xsi:type="CharFixed" LENGTH="MAXLENGTH" />
<FIELD ID="3" xsi:type="CharFixed" LENGTH="10" />
<FIELD ID="4" xsi:type="CharFixed" LENGTH="8" />
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="Field1" xsi:type="SQLNVARCHAR"/>
<COLUMN SOURCE="2" NAME="Field2" xsi:type="SQLNVARCHAR"/>
<COLUMN SOURCE="3" NAME="Field3" xsi:type="SQLNVARCHAR"/>
<COLUMN SOURCE="4" NAME="Field4" xsi:type="SQLNVARCHAR"/>
</ROW>
</BCPFORMAT>
But 'MAXLENGTH' does not seem to work.
Error message:
bad value MAXLENGTH for attribute "LENGTH"
Any suggestions on how to put the LENGTH to maximum ?
The length of a string is limited to 4000 (nchar) or 8000 (char) type. There is no max length-constant - AFAIC
Refer to this, "Field Attributes"
If you look up the schema meta-data just as SQL Server does, you can follow this link
http://schemas.microsoft.com/sqlserver/2004/bulkload/format/bulkloadschema.xsd
to find this
<xsd:attribute name="LENGTH" type="xsd:positiveInteger" use="required"/>
[...]
<xsd:attribute name="MAX_LENGTH" type="xsd:positiveInteger" use="optional"/>
So the length is required while max_length seems to be optional, but if you specify it, it must be a positive integer.

How do I build FetchXML to give me all members of list A outer joined with all members of list B by emailaddress1

And is that even possible?
I have the fetch xml already to filter list B out of list A by record id however I also want the option to filter it by the email address.
For example:
List A has 2 records. Record 1: Bob#BobMail.com and Record 2: Susy#SusyMail.com.
List B has 1 record. Record 3 Bob#BobMail.com which is a different contact than Record 1 but has the same email address.
Still with me? Sorry for the confusing explanation...
The query should take List A, do an outer join with List B to produce the result which only contains Record 2 with the email address Susy#SusyMail.com.
If there's any way I can be more clear on what I'm trying to do don't hesitate to ask.
After a night to think about it and a fresh pair of eyes in the morning I realized I simply needed to link the listmember to the contact exactly as I had been with the by ID option and then add an additional link to the contact entity on the email address.
<fetch mapping="logical" version="1.0" page="1" count="100" >
<entity name="contact" >
<link-entity name="listmember" from="entityid" to="contactid" >
<filter>
<condition attribute="listid" operator="eq" value="06197bff-a299-e311-aae4-6c3be5a892e8" />
</filter>
</link-entity>
<attribute name="contactid" />
<attribute name="emailaddress1" />
<filter type="and" >
<condition attribute="emailaddress1" operator="not-null" />
<condition attribute="donotbulkemail" operator="ne" value="1" />
</filter>
<link-entity name="listmember" from="entityid" to="contactid" link-type="outer" alias="exclusionlist" >
<attribute name="entityid" />
<filter type="and" >
<condition attribute="listid" operator="eq" value="06197bff-a299-e311-aae4-6c3be5a892e8" />
</filter>
</link-entity>
<link-entity name="contact" from="emailaddress1" to="emailaddress1" link-type="outer" alias="emailaddress" >
<attribute name="fullname" />
<attribute name="emailaddress1" />
</link-entity>
<order descending="false" attribute="emailaddress1" />
</entity>
And then we filter out the reults by the alias:'
results = results.Where(x => !x.Contains("exclusionlist.entityid") || !x.Contains("emailaddress.emailaddress1")).ToList();
Hopefully this helps someone else though I do feel a bit dumb for asking now...

CRM 4.0 - FetchXML to retrieve data from related entity

I'm trying to use FetchXML to pull in a list of courses with an 'entry year' related entity. What I would like to do is only return a single record for each course (could return multiple courses) with the latest year (e.g I would want it to pick the last year out of 2012, 2013, 2014 - so in this case 2014). So I currently have:
<fetch mapping="logical" distinct="true">
<entity name="course">
<all-attributes/>
<order attribute="name" />
<link-entity name="course_entryyear" from="courseid" to="courseid">
<link-entity name="entryyear" from="entryyearid" to="entryyearid">
<attribute name="year" />
</link-entity>
</link-entity>
</entity>
</fetch>
Is this possible to do within FetchXML and if so how can I amend the above?
Cheers
What about adding count="1" and inner join to the linked entities along with some ordering:
So it would look like:
<fetch mapping="logical" distinct="true">
<entity name="course">
<all-attributes/>
<order attribute="name" />
<link-entity name="course_entryyear" from="courseid" to="courseid" link-type="inner">
<link-entity name="entryyear" from="entryyearid" to="entryyearid" link-type="inner" count="1">
<attribute name="year" />
<order attribute="year" descending="true"/>
</link-entity>
</link-entity>
</entity>
</fetch>
Depending on how you are displaying the course list you might want to change the link-type for the "course-entryyear" from inner to outer, so that all courses get displayed even if they haven't been served yet.

How to write mappings for a stored procedure

There is an excellent post on how to map return values for a stored procedure call here:
http://elegantcode.com/2008/11/23/populating-entities-from-stored-procedures-with-nhibernate/
The mapping in this example has been done through hbm files.
I am trying to use the latest version of Nhibernate (3.2) where we can do mapping through code. I really want to find out the C# code that would create a mapping like below:
<sql-query name="GetProductsByCategoryId">
<return class="Product">
<return-property column="ProductID" name="Id" />
<return-property column="ProductName" name="Name" />
<return-property column="SupplierID" name="Supplier" />
<return-property column="CategoryID" name="Category" />
<return-property column="QuantityPerUnit" name="QuantityPerUnit" />
<return-property column="UnitPrice" name="UnitPrice" />
<return-property column="UnitsInStock" name="UnitsInStock" />
<return-property column="UnitsOnOrder" name="UnitsOnOrder" />
<return-property column="ReorderLevel" name="ReorderLevel" />
<return-property column="Discontinued" name="Discontinued" />
</return>
exec dbo.GetProductsByCategoryId :CategoryId
</sql-query>
To be honest I never tried it, by you should take a look to the extension method AddNamedQuery(..): you call it from you Configuration instance (NHibernate.Cfg namespace)).
Some examples on the NHibernate test project.
By the way, you can mix the new 3.2 mapping-by-code and xml one.
Start to look at this question;

Categories

Resources