-
Notifications
You must be signed in to change notification settings - Fork 14
Tutorial 6
PhuocLe edited this page Aug 26, 2018
·
5 revisions
- Lead can qualify by a user who has security role:
Qualify Lead
- Finish Tutorial 1: Plugin
- Finish Tutorial 2: Unit Test Plugin
- Finish Tutorial 3: WebResource
- Finish Tutorial 4: Unit Test WebResource
- Finish Tutorial 5: Custom Workflow
- Open file
Lead.js
in the folderentities
of the projectPaz.LuckeyMonkey.WebResource
- Add function
hasRole
and returnHasRole
. PleaseDON'T
copy/paste code. Trytyping
to getintellisense
thatPL.DynamicsCrm.DevKit
support. And remember to use View-FetchXML to copy/paste Javascript code
...
function hasRole(role) {
var fetchData = {
name: role
};
var fetchXml = [
"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>",
" <entity name='systemuser'>",
" <attribute name='systemuserid'/>",
" <filter type='and'>",
" <condition attribute='systemuserid' operator='eq-userid'/>",
" </filter>",
" <link-entity name='systemuserroles' from='systemuserid' to='systemuserid' visible='false' intersect='true'>",
" <link-entity name='role' from='roleid' to='roleid' alias='ae'>",
" <filter type='and'>",
" <condition attribute='name' operator='eq' value='", fetchData.name, "'/>",
" </filter>",
" </link-entity>",
" </link-entity>",
" </entity>",
"</fetch>",
].join("");
var req = new LuckeyMonkey.WebApi.RetrieveRequest();
req.async = false;
req.fetchXml = fetchXml;
req.entityName = "systemuser";
var res = WebApiClient.Retrieve(req);
if (res.value.length === 0) return false;
return true;
}
return {
OnLoad: onLoad,
OnSave: onSave,
HasRole: hasRole,
__SubjectAddOnChange__: SubjectAddOnChange,
__PhoneAddOnChange__: PhoneAddOnChange
};
...
- Open file
Lead.test.js
in the foldertest
of the projectPaz.LuckeyMonkey.WebResource
- Add these test method bellow
...
it("formLead.HasRole('Qualify Lead')", function () {
//setup
var data = {
"@odata.context": "http://fakeurl.com/api/data/v8.0/$metadata#systemusers(systemuserid,ownerid)",
"value": [{
"@odata.etag": "W/\"320689\"",
"systemuserid": "59046ca1-5579-e811-80dc-000d3aa2cf16",
"ownerid": "59046ca1-5579-e811-80dc-000d3aa2cf16"
}]
};
server.RespondWith("Qualify Lead", data);
//call
var hasRole = formLead.HasRole("Qualify Lead");
//result
expect(hasRole).toBe(true);
});
it("formLead.HasRole('System Administrator')", function () {
var data = {
"@odata.context": "http://fakeurl.com/api/data/v8.0/$metadata#systemusers(systemuserid,ownerid)",
"value": []
};
server.RespondWith("System Administrator", data);
//call
var hasRole = formLead.HasRole("System Administrator");
//result
expect(hasRole).toBe(false);
});
...
-
Run JS Tests
and you get the result bellow
------ Test started: File: C:\src\github\Dynamics-Crm-DevKit\tutorials\Paz.LuckeyMonkey.WebResource\test\Lead.test.js ------
4 passed, 0 failed, 4 total (chutzpah).
========== Total Tests: 4 passed, 0 failed, 4 total ==========
- Check-in all files to your source control
- You finished this tutorial
This tutorial, you know howto
- Use library
WebApiClient
-
Unit test Javascript
withsinon