Lightweight JS builder for Microsoft CRM FetchXmls. Useful to create dynamic Subgrids filtering or WebService Queries.
- Easiest way to learn how to use it, is to take a look at the tests. (mlabs.xrm.fetchxmlbuilder.spec.js)
//Imagine you're on some entity that has relation with account, but doesn't have direct relation with contacts.
var query = new mlabs.xrm.queryBuilder();
query.setEntityName("contact");
query.addFilter(
function (filter) {
filter.type("and")
.addCondition(function (where) {
where.attribute('statuscode').equals(0); /* active */
})
.addCondition(function (where) {
where.attribute("parentaccountid").equals(Xrm.Page.getAttribute("new_relatedaccount")[0].id);
});
});
var gridControl = GetGridControl();
gridControl.SetParameter("fetchXml", query.getFetchXml()); returns built FetchXML
adds new opening tag
close first tag from the stack
inserts string (xml)
close all opened tags from the stack
sets 'count' attribute
sets 'top' attribute
gets complete fetchXml
adds filter. See "filterBuilder" section for more info
sets 'name' attribute in 'entity' node.
adds attribute node. Called for the first time marks '<all-attributes />' as not needed
function addEntityLink([function: mlabs.xrm.entityBuilder -> mlabs.xrm.queryBuilder]entityBuilder) : queryBuilder
builds entityLink node.
accepts values "or" / "and". defines filter type (filter 'type' attribute).
adds condition node. injected argument is a conditionBuilder object.
adds nested filter node. injected argument is a new filterBuilder object.
sets filtered attribute name and returns condition operator builder
- equals
- isNotEqual
- isLessThan
- isLessEqual
- isGreaterThan
- isGreaterEqual
- contains
- isIn
- isBetween
- doesNotContain
each operator has signature: function: object -> undefined - sample:
function(where) {
where.attribute("new_number").equals(1)
}
//or
function(where) {
where.attribute("new_number").isIn(1,2,3)
} let query = new mlabs.xrm.fetchXmlBuilder();
let fetchXml = query.setEntityName("account")
.addFilter(filter =>
filter
.type('and')
.addCondition(where => where.attribute("statuscode").equals(0))
.addCondition(where => where.attribute("name").contains("Ltd"))
)
.getFetchXml();
/* output:
<fetch count="5000" distinct="true">
<entity name="account">
<all-attributes/>
<filter type="and">
<condition attribute="statuscode" operator="eq" value="0"/>
<condition attribute="name" operator="like" value="Ltd"/>
</filter>
</entity>
</fetch>
*/ let query = new mlabs.xrm.fetchXmlBuilder();
let fetchXml = query.setEntityName("account")
.addEntityLink(link =>
link.setTo('owninguser')
.setName('systemuser')
.addFilter(filter =>
filter.addCondition(where => where.attribute("lastname").isNotEqual("Cannon"))))
.getFetchXml();
/*
Output:
<fetch count="5000" distinct="true">
<entity name="account">
<all-attributes/>
<link-entity to="owninguser" name="systemuser">
<all-attributes/>
<filter type="and">
<condition attribute="lastname" operator="ne" value="Cannon"/>
</filter>
</link-entity>
</entity>
</fetch>
*/