Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getUsersFromBisunessUnit(roleName, businessunitid) { //funkcja pobiera użytkowników z danej jednostki biznesowej
- const fetchXml = `<fetch><entity name='systemuserroles' ><attribute name='systemuserid' /><link-entity name='role' from='roleid' to='roleid' ><filter><condition attribute='name' operator='eq' value='${roleName}' /></filter></link-entity><link-entity name='systemuser' from='systemuserid' to='systemuserid' ><filter><condition attribute='businessunitid' operator='eq' value='${businessunitid}' /></filter></link-entity></entity></fetch>`;
- return Xrm.WebApi.retrieveMultipleRecords('systemuserrolescollection', `?fetchXml=${fetchXml}`).then(
- result => {
- if (result.entities.length == 0)
- return [] //brak użytkowników
- else
- return result.entities.map(el => el.systemuserid)
- },
- error => {
- console.error(`Wystąpił błąd: ${error.message}`);
- return false
- }
- ) //On success, returns a promise
- }
- function addLookup(field, array = []) { //filtrowanie lookupa zadaną tablicą użytkowników
- let filter;
- if (!array.length)
- filter = "<filter><condition attribute='systemuserid' operator='eq' value='00000000-0000-0000-0000-000000000000' /></filter>";
- else
- filter = `<filter><condition attribute='systemuserid' operator='in' >${array.map(el => `<value>${el}</value>`).join("")}</condition></filter>`;
- Xrm.Page.getControl(field).addPreSearch(
- () => Xrm.Page.getControl(field).addCustomFilter(filter)
- );
- }
- function lookup() {
- const fetchXml = `<fetch><entity name='businessunit' ><attribute name='parentbusinessunitid' alias='parentBu' /><attribute name='businessunitid' alias='bu' /><filter><condition attribute='businessunitid' operator='eq-businessid' /></filter><link-entity name='businessunit' from='businessunitid' to='parentbusinessunitid' link-type='outer' ><attribute name='parentbusinessunitid' alias='nextParentBu' /></link-entity></entity></fetch>`;
- const roleName = "Kierownik";
- Xrm.WebApi.retrieveMultipleRecords('businessunit', `?fetchXml=${fetchXml}`).then(
- result => {
- let [bu, parentbu, nextparentbu] = [result.entities[0].businessunitid, result.entities[0].parentBu, result.entities[0].nextParentBu];
- getUsersFromBisunessUnit(roleName, bu).then(
- result => {
- if (result.length != 0)
- addLookup("kierownik", result)
- else {
- if (parentBu !== undefined) {
- getUsersFromBisunessUnit(roleName, parentbu).then(
- result => {
- if (result.length != 0)
- addLookup("kierownik", result)
- else {
- if (nextparentbu !== undefined) {
- getUsersFromBisunessUnit(roleName, parentbu).then(
- result => {
- if (result.length != 0)
- addLookup("kierownik", result)
- else
- addLookup("kierownik")
- },
- error => addLookup("kierownik")
- )
- } else
- addLookup("kierownik")
- }
- },
- error => addLookup("kierownik")
- )
- } else
- addLookup("kierownik")
- }
- },
- error => addLookup("kierownik")
- )
- }
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement