|
| 1 | +import microsoft from "../../microsoft_dynamics_365_sales.app.mjs"; |
| 2 | +import languageCodes from "../../common/language-codes.mjs"; |
| 3 | +import pluralize from "pluralize"; |
| 4 | + |
| 5 | +export default { |
| 6 | + key: "microsoft_dynamics_365_sales-create-custom-entity", |
| 7 | + name: "Create Custom Entity", |
| 8 | + description: "Create a custom entity. [See the documentation](https://learn.microsoft.com/en-us/power-apps/developer/data-platform/webapi/create-update-entity-definitions-using-web-api)", |
| 9 | + version: "0.0.1", |
| 10 | + type: "action", |
| 11 | + props: { |
| 12 | + microsoft, |
| 13 | + solutionId: { |
| 14 | + propDefinition: [ |
| 15 | + microsoft, |
| 16 | + "solutionId", |
| 17 | + ], |
| 18 | + }, |
| 19 | + displayName: { |
| 20 | + type: "string", |
| 21 | + label: "Display Name", |
| 22 | + description: "The name of the new entity. E.g. `Bank Account`", |
| 23 | + }, |
| 24 | + primaryAttribute: { |
| 25 | + type: "string", |
| 26 | + label: "Primary Attribute", |
| 27 | + description: "The primary name attribute of the new entity. E.g. `Account Name`", |
| 28 | + }, |
| 29 | + languageCode: { |
| 30 | + type: "integer", |
| 31 | + label: "Language Code", |
| 32 | + description: "The language code to use for the entity", |
| 33 | + options: languageCodes, |
| 34 | + default: 1033, |
| 35 | + optional: true, |
| 36 | + }, |
| 37 | + additionalAttributes: { |
| 38 | + type: "object", |
| 39 | + label: "Additional Attributes", |
| 40 | + description: "An array of objects representing attributes to add to the custom entity. [See the documentation](https://learn.microsoft.com/en-us/power-apps/developer/data-platform/webapi/create-update-entity-definitions-using-web-api) for more information about formatting attribute objects", |
| 41 | + optional: true, |
| 42 | + }, |
| 43 | + description: { |
| 44 | + type: "string", |
| 45 | + label: "Description", |
| 46 | + description: "A description of the new entity", |
| 47 | + optional: true, |
| 48 | + }, |
| 49 | + hasActivities: { |
| 50 | + type: "boolean", |
| 51 | + label: "Has Activities", |
| 52 | + description: "Set to `true` if the new entity has activities", |
| 53 | + default: false, |
| 54 | + optional: true, |
| 55 | + }, |
| 56 | + hasNotes: { |
| 57 | + type: "boolean", |
| 58 | + label: "Has Notes", |
| 59 | + description: "Set to `true` if the new entity has notes", |
| 60 | + default: false, |
| 61 | + optional: true, |
| 62 | + }, |
| 63 | + }, |
| 64 | + methods: { |
| 65 | + parseAttributes() { |
| 66 | + return this.additionalAttributes |
| 67 | + ? typeof this.additionalAttributes === "string" |
| 68 | + ? JSON.parse(this.additionalAttributes) |
| 69 | + : this.additionalAttributes |
| 70 | + : []; |
| 71 | + }, |
| 72 | + removeSpaces(str) { |
| 73 | + return str.replace(/\s+/g, ""); |
| 74 | + }, |
| 75 | + buildLocalizedLabelArray(label) { |
| 76 | + return [ |
| 77 | + { |
| 78 | + "@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel", |
| 79 | + "Label": label, |
| 80 | + "LanguageCode": this.languageCode, |
| 81 | + }, |
| 82 | + ]; |
| 83 | + }, |
| 84 | + }, |
| 85 | + async run({ $ }) { |
| 86 | + const solution = await this.microsoft.getSolution({ |
| 87 | + $, |
| 88 | + solutionId: this.solutionId, |
| 89 | + }); |
| 90 | + |
| 91 | + const { customizationprefix } = await this.microsoft.getPublisher({ |
| 92 | + $, |
| 93 | + publisherId: solution._publisherid_value, |
| 94 | + }); |
| 95 | + |
| 96 | + const attributes = this.parseAttributes(); |
| 97 | + |
| 98 | + const { headers } = await this.microsoft.createCustomEntity({ |
| 99 | + $, |
| 100 | + returnFullResponse: true, |
| 101 | + headers: { |
| 102 | + "MSCRM.SolutionUniqueName": solution.uniquename, |
| 103 | + }, |
| 104 | + data: { |
| 105 | + "@odata.type": "Microsoft.Dynamics.CRM.EntityMetadata", |
| 106 | + "Attributes": [ |
| 107 | + { |
| 108 | + "@odata.type": "Microsoft.Dynamics.CRM.StringAttributeMetadata", |
| 109 | + "DisplayName": { |
| 110 | + "@odata.type": "Microsoft.Dynamics.CRM.Label", |
| 111 | + "LocalizedLabels": this.buildLocalizedLabelArray(this.primaryAttribute), |
| 112 | + }, |
| 113 | + "IsPrimaryName": true, |
| 114 | + "SchemaName": `${customizationprefix}_${this.removeSpaces(this.primaryAttribute)}`, |
| 115 | + "MaxLength": 100, |
| 116 | + "FormatName": { |
| 117 | + "Value": "Text", |
| 118 | + }, |
| 119 | + "RequiredLevel": { |
| 120 | + "Value": "None", |
| 121 | + "CanBeChanged": true, |
| 122 | + "ManagedPropertyLogicalName": "canmodifyrequirementlevelsettings", |
| 123 | + }, |
| 124 | + }, |
| 125 | + ...attributes, |
| 126 | + ], |
| 127 | + "DisplayName": { |
| 128 | + "@odata.type": "Microsoft.Dynamics.CRM.Label", |
| 129 | + "LocalizedLabels": this.buildLocalizedLabelArray(this.displayName), |
| 130 | + }, |
| 131 | + "DisplayCollectionName": { |
| 132 | + "@odata.type": "Microsoft.Dynamics.CRM.Label", |
| 133 | + "LocalizedLabels": this.buildLocalizedLabelArray(pluralize(this.displayName)), |
| 134 | + }, |
| 135 | + "Description": this.description && { |
| 136 | + "@odata.type": "Microsoft.Dynamics.CRM.Label", |
| 137 | + "LocalizedLabels": this.buildLocalizedLabelArray(this.description), |
| 138 | + }, |
| 139 | + "HasActivities": this.hasActivities, |
| 140 | + "HasNotes": this.hasNotes, |
| 141 | + "SchemaName": `${customizationprefix}_${this.removeSpaces(this.displayName)}`, |
| 142 | + "PrimaryNameAttribute": `${customizationprefix}_${this.removeSpaces(this.primaryAttribute)}`, |
| 143 | + "OwnershipType": "UserOwned", |
| 144 | + }, |
| 145 | + }); |
| 146 | + |
| 147 | + const entityId = headers["odata-entityid"].substring(headers["odata-entityid"].lastIndexOf("/") + 1); |
| 148 | + const response = await this.microsoft.getEntity({ |
| 149 | + $, |
| 150 | + entityId, |
| 151 | + }); |
| 152 | + |
| 153 | + $.export("$summary", `Successfully created custom entity with ID: ${entityId}`); |
| 154 | + |
| 155 | + return response; |
| 156 | + }, |
| 157 | +}; |
0 commit comments