From b82344746e4a7acaaf5dd29f771fd00507b157eb Mon Sep 17 00:00:00 2001 From: Betim Beja <11160171+BetimBeja@users.noreply.github.com> Date: Thu, 6 May 2021 16:37:04 +0200 Subject: [PATCH] Refactored samples --- .gitignore | 350 ++++++++++++++++++ .../bin/setEmailSenderToQueue.js | 85 ----- .../package-lock.json | 52 +++ .../package.json | 15 + .../setEmailSenderToQueue.ts | 46 +-- .../tsconfig.json | 14 +- 6 files changed, 449 insertions(+), 113 deletions(-) create mode 100644 .gitignore delete mode 100644 samples/set-email-sender-to-parent-queue/bin/setEmailSenderToQueue.js create mode 100644 samples/set-email-sender-to-parent-queue/package-lock.json create mode 100644 samples/set-email-sender-to-parent-queue/package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f1229f --- /dev/null +++ b/.gitignore @@ -0,0 +1,350 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ \ No newline at end of file diff --git a/samples/set-email-sender-to-parent-queue/bin/setEmailSenderToQueue.js b/samples/set-email-sender-to-parent-queue/bin/setEmailSenderToQueue.js deleted file mode 100644 index 05bc253..0000000 --- a/samples/set-email-sender-to-parent-queue/bin/setEmailSenderToQueue.js +++ /dev/null @@ -1,85 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __generator = (this && this.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (_) try { - if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] & 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; -exports.__esModule = true; -exports.setEmailSenderToQueue = void 0; -// Export function to make it available for import in other files -function setEmailSenderToQueue(ctx) { - return __awaiter(this, void 0, void 0, function () { - var formContext, regarding, queue; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - formContext = ctx.getFormContext(); - if (!(formContext.ui.getFormType() === 1 /* Create */ || formContext.getAttribute("from").getValue() === null)) return [3 /*break*/, 2]; - regarding = formContext.getAttribute("regardingobjectid").getValue(); - if (!(regarding !== null && regarding[0].entityType.toLowerCase() === "incident")) return [3 /*break*/, 2]; - return [4 /*yield*/, Xrm.WebApi.retrieveMultipleRecords("queueitem", "?$select=_queueid_value&$filter=statecode eq 0 and _objectid_value eq " + regarding[0].id).then(getEntityReferenceFromMultipleResult)]; - case 1: - queue = _a.sent(); - // Set sender to queue if exists - if (queue !== null) { - formContext.getAttribute("from").setValue([queue]); - } - _a.label = 2; - case 2: return [2 /*return*/]; - } - }); - }); -} -exports.setEmailSenderToQueue = setEmailSenderToQueue; -function getEntityReferenceFromMultipleResult(results) { - return __awaiter(this, void 0, void 0, function () { - var queue; - return __generator(this, function (_a) { - // Return lookup value of queue if exactly 1 active queueitem is found - if (results.entities.length === 1) { - queue = {}; - queue.entityType = results.entities[0]["_queueid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]; - queue.id = results.entities[0]._queueid_value; - queue.name = results.entities[0]["_queueid_value@OData.Community.Display.V1.FormattedValue"]; - return [2 /*return*/, queue]; - } - // Throw error if more than 1 active queueitem is found - if (results.entities.length > 1) { - throw "Number of active queue items is more than 1"; - } - // Return null by default - return [2 /*return*/, null]; - }); - }); -} diff --git a/samples/set-email-sender-to-parent-queue/package-lock.json b/samples/set-email-sender-to-parent-queue/package-lock.json new file mode 100644 index 0000000..513763e --- /dev/null +++ b/samples/set-email-sender-to-parent-queue/package-lock.json @@ -0,0 +1,52 @@ +{ + "name": "set-email-sender-to-parent-queue", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "version": "1.0.0", + "license": "MIT", + "bin": { + "set-email-sender-to-parent-queue": "setEmailSenderToQueue.js" + }, + "devDependencies": { + "@types/xrm": "^9.0.39", + "typescript": "^4.2.4" + } + }, + "node_modules/@types/xrm": { + "version": "9.0.39", + "resolved": "https://registry.npmjs.org/@types/xrm/-/xrm-9.0.39.tgz", + "integrity": "sha512-EMX0Bsim8t21qCn2Lc4Fa5oOAszj01BR3QvPLIm+MD8zsSeLiMKog37M7qiMmGhN534wNE1xZJv5spD8Ee+dWA==", + "dev": true + }, + "node_modules/typescript": { + "version": "4.2.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.2.4.tgz", + "integrity": "sha512-V+evlYHZnQkaz8TRBuxTA92yZBPotr5H+WhQ7bD3hZUndx5tGOa1fuCgeSjxAzM1RiN5IzvadIXTVefuuwZCRg==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + } + }, + "dependencies": { + "@types/xrm": { + "version": "9.0.39", + "resolved": "https://registry.npmjs.org/@types/xrm/-/xrm-9.0.39.tgz", + "integrity": "sha512-EMX0Bsim8t21qCn2Lc4Fa5oOAszj01BR3QvPLIm+MD8zsSeLiMKog37M7qiMmGhN534wNE1xZJv5spD8Ee+dWA==", + "dev": true + }, + "typescript": { + "version": "4.2.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.2.4.tgz", + "integrity": "sha512-V+evlYHZnQkaz8TRBuxTA92yZBPotr5H+WhQ7bD3hZUndx5tGOa1fuCgeSjxAzM1RiN5IzvadIXTVefuuwZCRg==", + "dev": true + } + } +} diff --git a/samples/set-email-sender-to-parent-queue/package.json b/samples/set-email-sender-to-parent-queue/package.json new file mode 100644 index 0000000..555e1fc --- /dev/null +++ b/samples/set-email-sender-to-parent-queue/package.json @@ -0,0 +1,15 @@ +{ + "name": "set-email-sender-to-parent-queue", + "version": "1.0.0", + "description": "", + "scripts": { + "build": "tsc --project tsconfig.json", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "MIT", + "devDependencies": { + "@types/xrm": "^9.0.39", + "typescript": "^4.2.4" + } +} diff --git a/samples/set-email-sender-to-parent-queue/setEmailSenderToQueue.ts b/samples/set-email-sender-to-parent-queue/setEmailSenderToQueue.ts index 2656f42..a49201d 100644 --- a/samples/set-email-sender-to-parent-queue/setEmailSenderToQueue.ts +++ b/samples/set-email-sender-to-parent-queue/setEmailSenderToQueue.ts @@ -1,15 +1,34 @@ // Export function to make it available for import in other files +async function getEntityReferenceFromMultipleResult(results: Xrm.RetrieveMultipleResult): Promise { + // Return lookup value of queue if exactly 1 active queueitem is found + if (results.entities.length === 1) { + const queue: Xrm.LookupValue = { + entityType: results.entities[0]["_queueid_value@Microsoft.Dynamics.CRM.lookuplogicalname"], + id: results.entities[0]._queueid_value, + name: results.entities[0]["_queueid_value@OData.Community.Display.V1.FormattedValue"] + }; + + return queue; + } + // Throw error if more than 1 active queueitem is found + if (results.entities.length > 1) { + throw "Number of active queue items is more than 1"; + } + // Return null by default + return null; +} + export async function setEmailSenderToQueue(ctx: Xrm.Events.EventContext) { - let formContext = ctx.getFormContext(); + const formContext = ctx.getFormContext(); // Only run if form type is create or if sender is empty (null) if (formContext.ui.getFormType() === XrmEnum.FormType.Create || formContext.getAttribute("from").getValue() === null) { - let regarding = formContext.getAttribute("regardingobjectid").getValue() as Xrm.LookupValue[]; + const regarding = formContext.getAttribute("regardingobjectid").getValue(); // Validate that email is set regarding and entity type is incident. Last part can be removed to support any parent entity if (regarding !== null && regarding[0].entityType.toLowerCase() === "incident") { // Retrieve active queue item with queue reference. Alternatively add more logic to verify that the queue is activated for email - var queue = await Xrm.WebApi.retrieveMultipleRecords( + const queue = await Xrm.WebApi.retrieveMultipleRecords( "queueitem", - "?$select=_queueid_value&$filter=statecode eq 0 and _objectid_value eq " + regarding[0].id + `?$select=_queueid_value&$filter=statecode eq 0 and _objectid_value eq ${regarding[0].id}` ).then(getEntityReferenceFromMultipleResult); // Set sender to queue if exists @@ -19,22 +38,3 @@ export async function setEmailSenderToQueue(ctx: Xrm.Events.EventContext) { } } } - -async function getEntityReferenceFromMultipleResult(results: Xrm.RetrieveMultipleResult): Promise { - // Return lookup value of queue if exactly 1 active queueitem is found - if (results.entities.length === 1) { - let queue = {} as Xrm.LookupValue; - queue.entityType = results.entities[0]["_queueid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]; - queue.id = results.entities[0]._queueid_value; - queue.name = results.entities[0]["_queueid_value@OData.Community.Display.V1.FormattedValue"]; - - return queue; - } - // Throw error if more than 1 active queueitem is found - if (results.entities.length > 1) - { - throw "Number of active queue items is more than 1"; - } - // Return null by default - return null; -} \ No newline at end of file diff --git a/samples/set-email-sender-to-parent-queue/tsconfig.json b/samples/set-email-sender-to-parent-queue/tsconfig.json index e93447f..8206c36 100644 --- a/samples/set-email-sender-to-parent-queue/tsconfig.json +++ b/samples/set-email-sender-to-parent-queue/tsconfig.json @@ -1,7 +1,11 @@ // tsconfig.json { - "compilerOptions": { - "lib": [ "es2015" ], - "outDir": "bin" - } - } \ No newline at end of file + "compilerOptions": { + "lib": ["DOM", "es2015" ], + "outDir": "bin" + }, + "include": [ "**/*.ts" ], + "exclude": [ + "node_modules" + ] +} \ No newline at end of file