This happens when there is &, or other invalid characters in the contract name. My question is, how do I deal with these invalid characters??!!
Microsoft.Crm.CrmException: Invalid XML. --->
System.Xml.XmlException: An error occurred while parsing EntityName.
Line 13, position 117.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.HandleEntityReference(Boolean isInAttributeValue, EntityExpandType expandType, Int32&
charRefEndPos)
at System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos, Char quoteChar, NodeData attr)
at System.Xml.XmlTextReaderImpl.ParseAttributes()
at System.Xml.XmlTextReaderImpl.ParseElement()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at Microsoft.Crm.Platform.Server.Utility.XmlHelper.LoadXmlInfo(String
xmlInfo)
at Microsoft.Crm.Query.EntityExpression.ExtractPlatformName(String fetchXml, XElement element).
Here is the code in questions:
//run a fetchXml query to get how many contract lines have these values
string fetchLines = #"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='new_yummycontractline'>
<attribute name='new_yummycontractlineid' />
<filter type='and'>
<condition attribute='new_servingtime' operator='eq' value='" + servingTime.ToString() + #"' />
<condition attribute='new_servinggroup' operator='eq' value='" + servingGroup.ToString() + #"' />
<condition attribute='new_destination' operator='eq' value='" + location.ToString() + #"' />
<condition attribute='statecode' operator='eq' value='0' />
</filter>
<link-entity name='new_yummycontract' from='new_yummycontractid' to='new_contractid' link-type='inner' alias='ad'>
<filter type='and'>
<condition attribute='new_yummycontractid' operator='eq' uiname='" + uiname + #"' uitype='new_yummycontract' value='" + contractId.ToString() + #"' />
</filter>
</link-entity>
</entity>
</fetch>";
EntityCollection results = service.RetrieveMultiple(new FetchExpression(fetchLines));
uiname and uitype are not required for your fetchXml to work. These are used for presentation and not for the query. I think these are added by the Advanced Find editor; hence the "UI"
You can rewrite that section of XML like so
<link-entity name='new_yummycontract' from='new_yummycontractid' to='new_contractid' link-type='inner' alias='ad'>
<filter type='and'>
<condition attribute='new_yummycontractid' operator='eq' value='" + contractId.ToString() + #"' />
</filter>
</link-entity>
Not sure which version of .NET you're using, but you can also use the $ special character to indicate an interpolated string which I think increases readability and reduces string concatenation
string fetchLines = #$"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='new_yummycontractline'>
<attribute name='new_yummycontractlineid' />
<filter type='and'>
<condition attribute='new_servingtime' operator='eq' value='{servingTime}' />
<condition attribute='new_servinggroup' operator='eq' value='{servingGroup}' />
<condition attribute='new_destination' operator='eq' value='{location}' />
<condition attribute='statecode' operator='eq' value='0' />
</filter>
<link-entity name='new_yummycontract' from='new_yummycontractid' to='new_contractid' link-type='inner' alias='ad'>
<filter type='and'>
<condition attribute='new_yummycontractid' operator='eq' value='{contractId}' />
</filter>
</link-entity>
</entity>
</fetch>";
I used webutility.htmlencode(uiname) and the problem seems to have disappeared. I guess the uiname has some invalid XML characters and this method gets rid of the problem!
Related
For each of the CRUD request there's a table with supported entities. Is there any way to obtain such info from SDK/WebAPI per entity type?
Probably some metadata properties or separate requests to check?
Maybe this documentation should help you.
To verify if a message and entity combination supports execution of plug-ins using a database query, you can use the following Web API query:
{{webapiurl}}sdkmessages?$select=name
&$filter=isprivate eq false
and (name ne 'SetStateDynamicEntity'
and name ne 'RemoveRelated'
and name ne 'SetRelated' and
name ne 'Execute')
and sdkmessageid_sdkmessagefilter/any(s:s/iscustomprocessingstepallowed eq true
and s/isvisible eq true)
&$expand=sdkmessageid_sdkmessagefilter($select=primaryobjecttypecode;
$filter=iscustomprocessingstepallowed eq true and isvisible eq true)
&$orderby=name
Fetchxml version:
<fetch>
<entity name='sdkmessage' >
<attribute name='name' />
<link-entity name='sdkmessagefilter' alias='filter' to='sdkmessageid' from='sdkmessageid' link-type='inner' >
<filter type='and' >
<condition attribute='iscustomprocessingstepallowed' operator='eq' value='1' />
<condition attribute='isvisible' operator='eq' value='1' />
</filter>
<attribute name='primaryobjecttypecode' />
</link-entity>
<filter>
<condition attribute='isprivate' operator='eq' value='0' />
<condition attribute='name' operator='not-in' >
<value>SetStateDynamicEntity</value>
<value>RemoveRelated</value>
<value>SetRelated</value>
<value>Execute</value>
</condition>
</filter>
<order attribute='name' />
</entity>
</fetch>
I have 2 entities in the database; an Appointment and an Email. I want to write a search function that fetches all Appointments and Emails that contains a string a user enters. However, I want to search on different attributes based on which entity it is. For instance: I want to fetch all Appointments that have the attribute subject containing the string "meeting today" and I also want to fetch all Emails that have the attribute description containing the same string. So in simple terms, only search the subject line for Appointments and only search descriptions for Emails.
Here's what my fetchXml looks so far:
<fetch count="10" distinct="true" mapping="logical" no-lock="true" output-format="xml-platform" page="1" returntotalrecordcount="false" version="1.0">
<entity name="activitypointer">
<attribute name="subject"/>
<attribute name="description"/>
<attribute name="activitytypecode"/>
<filter type="or">
<condition attribute="activitytypecode" operator="like">
<value>Email</value>
</condition>
<filter type="and">
<condition attribute="description" operator="like" value="%meeting today%"/>
</filter>
</filter>
<filter type="or">
<condition attribute="activitytypecode" operator="like">
<value>Appointment</value>
</condition>
<filter type="and">
<condition attribute="subject" operator="like" value="%meeting today%"/>
</filter>
</filter>
</entity>
</fetch>
This doesn't seem to grab any records back however. I can successfully fetch records when querying on a single entity type, but putting both filters doesn't return anything. Is what I'm asking possible to do in fetchXML? Or is there an issue with how I've constructed my query?
I just did a quick test in my environment, this is working. You can build such queries in Advanced find & download the fetchxml, before editing/testing in XrmToolBox FetchXml builder.
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" >
<entity name="activitypointer" >
<attribute name="activitytypecode" />
<attribute name="subject" />
<filter type="and" >
<filter type="or" >
<filter type="and" >
<condition attribute="activitytypecode" operator="eq" value="4202" />
<condition attribute="subject" operator="like" value="%test%" />
</filter>
<filter type="and" >
<condition attribute="activitytypecode" operator="eq" value="4201" />
<condition attribute="description" operator="like" value="%test%" />
</filter>
</filter>
</filter>
</entity>
</fetch>
The requirements for this FetchXML statement are:
Return all 'ebs_opportunityinternshipunit' where
'createdon' is 'last-week'
and
'owner' of 'account' is 'eq-userid'
'account' is referenced by 'parentaccountid' of 'opportunity'
or
'owner' of 'account2' is 'eq-userid'
'account2' is referenced by 'mitacs_partner2' of 'opportunity'
I've tried configuring the filter with out-of-the-box tools, XRMToolbox FetchXML Builder, visiting the official CRM Community forum, and reading FetchXML documentation. Still, the best that I've been able to come up with is below:
<fetch version="1.0" mapping="logical">
<entity name="ebs_opportunityinternshipunit">
<attribute name="ebs_name" />
<attribute name="createdon" />
<attribute name="ebs_opportunityinternshipunitid" />
<order attribute="ebs_name" descending="false" />
<filter type="and">
<condition attribute="createdon" operator="last-week" />
<filter type="or">
<condition entityname="ab" attribute="ownerid" operator="eq-userid" />
<condition entityname="ac" attribute="ownerid" operator="eq-userid" />
</filter>
</filter>
<link-entity name="opportunity" from="opportunityid" to="ebs_opportunity" link-type="inner" alias="aa">
<link-entity name="account" from="accountid" to="parentaccountid" link-type="inner" alias="ab" />
<link-entity name="account" from="accountid" to="mitacs_partner2" link-type="inner" alias="ac" />
</link-entity>
</entity>
</fetch>
I need to get all accounts that have phonecalls in state not open, so I created a query on fetch and got some results.
After checking my results I found that I got all accounts that have minimum 1 phonecall that was not open, but I need to get the accounts that all of their connected phonecalls are not open (can't have even 1 in open state) is it possible to do by fetch ?
** by NOT OPEN I mean state of Canceled or Completed.
Here is my fetch query:
#"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
<entity name='account'>
<attribute name='name' />
<order attribute='accountamount' descending='true' />
<link-entity name='phonecall' from='regardingobjectid' to='accountid' alias='ab'>
<filter type='and'>
<condition attribute='statecode' operator='ne' value='0' />
</filter>
</link-entity>
</entity>
</fetch>";
What you are looking for is Unmatch query. This can be achieved in 2 queries using fetchxml.
First Query: You have to pull all the Accounts with open phonecalls.
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >
<entity name="account" >
<attribute name="accountid" />
<order attribute="accountamount" descending="true" />
<link-entity name="phonecall" from="regardingobjectid" to="accountid" alias="ab" >
<filter type="and" >
<condition attribute="statecode" operator="eq" value="0" />
</filter>
</link-entity>
</entity>
</fetch>
Second Query: Iterate & pass the first query Account resultset as <value> like below, to filter out them.
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >
<entity name="account" >
<attribute name="name" />
<attribute name="primarycontactid" />
<attribute name="telephone1" />
<attribute name="accountid" />
<order attribute="name" descending="false" />
<filter type="and" >
<condition attribute="accountid" operator="not-in" >
<value><GUID of ACCOUNT1 with OPEN PHONECALL></value>
<value><GUID of ACCOUNT2 with OPEN PHONECALL></value>
<value><GUID of ACCOUNT3 with OPEN PHONECALL></value>
</condition>
</filter>
</entity>
</fetch>
Read for idea
I need fetchxml condition operator to retrieve all my appointments on today-11 days. I mean if I run my query today(12/04/2018) I want to retrieve my created records on 01/04/2018. If I run on 13/04/2018 - records created on 02/04/2018. Which operator can I use to get what I need?
<fetch distinct="false" mapping="logical" output-format="xml-platform" version="1.0">
<entity name="appointment">
<attribute name="subject"/>
<attribute name="statecode"/>
<attribute name="scheduledstart"/>
<attribute name="scheduledend"/>
<attribute name="createdby"/>
<attribute name="regardingobjectid"/>
<attribute name="activityid"/>
<attribute name="instancetypecode"/>
<order descending="false" attribute="subject"/>
<filter type="and">
<condition attribute="createdon" value="" operator=""/>
</filter>
</entity>
There is no single operator, but you can easily combine two:
<filter type="and">
<condition attribute="createdon" operator="last-x-days" value="11" />
<condition attribute="createdon" operator="olderthan-x-days" value="10" />
</filter>
There is no straight operator for this. You have to use eq operator & calculate the expression (-11) yourself if you are using this fetchxml query in SSRS report #date:
<condition attribute="new_date" operator="eq" value="#date"></condition>
Or calculate in javascript/C# & pass it to paramDate, if you are calling this in form script or server code:
'<condition attribute="new_date" operator="eq" value="' + paramDate + '"></condition>'