From 72de7cf64d69194f6adba8b5115be2c8ca1a900b Mon Sep 17 00:00:00 2001 From: Gavin Canon-Phratsachack <37220586+gncnpk@users.noreply.github.com> Date: Tue, 8 Apr 2025 21:03:30 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#72432=20[xrm]=20Ad?= =?UTF-8?q?d=20getAttributeType=20method=20to=20specific=20attribute=20int?= =?UTF-8?q?erfaces,=20changes=20getControlType=20method=20to=20use=20templ?= =?UTF-8?q?ate=20literal=20instead=20of=20string=20by=20@gncnpk?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gavin Canon-Phratsachack --- types/xrm/index.d.ts | 46 +++++++++++++++++++++++++++++++++++------- types/xrm/xrm-tests.ts | 15 ++++++++++++-- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/types/xrm/index.d.ts b/types/xrm/index.d.ts index e89eee0f61e0d3..f33d6bc8e080cb 100644 --- a/types/xrm/index.d.ts +++ b/types/xrm/index.d.ts @@ -2753,6 +2753,11 @@ declare namespace Xrm { * @see {@link Attribute} */ interface NumberAttribute extends Attribute { + /** + * Get the attribute type. + * @returns The attribute type. + */ + getAttributeType(): "integer" | "decimal" | "double" | "money"; /** * Gets the attribute format. * @returns The format of the attribute. @@ -2797,6 +2802,11 @@ declare namespace Xrm { * @see {@link Attribute} */ interface StringAttribute extends Attribute { + /** + * Get the attribute type. + * @returns The attribute type. + */ + getAttributeType(): "string"; /** * Gets the attribute format. * @returns The format of the attribute. @@ -2846,17 +2856,17 @@ declare namespace Xrm { * @see {@link EnumAttribute} */ interface BooleanAttribute extends EnumAttribute { - /** - * A collection of all the controls on the form that interface with this attribute. - * @see {@link https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/collections External Link: Collections (Client API reference)} - */ - controls: Collection.ItemCollection; - /** * Gets the attribute format. * @returns the string "boolean" */ getAttributeType(): "boolean"; + + /** + * A collection of all the controls on the form that interface with this attribute. + * @see {@link https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/collections External Link: Collections (Client API reference)} + */ + controls: Collection.ItemCollection; } /** @@ -2865,6 +2875,12 @@ declare namespace Xrm { * @see {@link Attribute} */ interface DateAttribute extends Attribute { + /** + * Gets the attribute type. + * @returns The attribute type. + */ + getAttributeType(): "datetime"; + /** * Gets the attribute format. * @@ -2885,6 +2901,12 @@ declare namespace Xrm { * @see {@link EnumAttribute} */ interface OptionSetAttribute extends EnumAttribute { + /** + * Gets the attribute type. + * @returns The attribute type. + */ + getAttributeType(): "optionset"; + /** * Gets the attribute format. * @returns The format of the attribute. @@ -2946,6 +2968,11 @@ declare namespace Xrm { * @see {@link EnumAttribute} */ interface MultiSelectOptionSetAttribute extends EnumAttribute { + /** + * Gets the attribute type. + * @returns The attribute type. + */ + getAttributeType(): "multiselectoptionset"; /** * Gets the attribute format. * @returns The format of the attribute. @@ -3008,6 +3035,11 @@ declare namespace Xrm { * @see {@link Attribute} */ interface LookupAttribute extends Attribute { + /** + * Gets the attribute type. + */ + getAttributeType(): "lookup"; + /** * Gets a boolean value indicating whether the Lookup is a multi-value PartyList. * @returns true the attribute is a PartyList, otherwise false. @@ -3275,7 +3307,7 @@ declare namespace Xrm { * * customcontrol: . (A custom control for mobile phone and tablet clients). * * customsubgrid: . (A custom dataset control for mobile phone and tablet clients). */ - getControlType(): ControlType | string; + getControlType(): ControlType | `${string}.${string}`; /** * Gets the name of the control on the form. diff --git a/types/xrm/xrm-tests.ts b/types/xrm/xrm-tests.ts index a04a5648da3ce0..f45e734e20e46b 100644 --- a/types/xrm/xrm-tests.ts +++ b/types/xrm/xrm-tests.ts @@ -224,8 +224,8 @@ if (attribute !== null) { attribute.setSubmitMode(submitModeString); // Works if the string is a const attribute.setRequiredLevel(requirementLevel); attribute.setRequiredLevel(requirementLevelString); // Works if the string is a const - - const isMulitselect = attribute.getAttributeType() === "multiselectoptionset"; + // @ts-expect-error + const isMultiSelectAttribute = attribute.getAttributeType() === "multiselectoptionset"; // This will return false because the attribute is a LookupAttribute } /// Demonstrate v8.2 quick form controls @@ -758,3 +758,14 @@ function GetAll(context: Xrm.FormContext) { throw new Error("Will return an empty array if no controls are present."); } } + +function testAttributeType(formContext: Xrm.FormContext) { + const attribute = formContext.getAttribute("myattribute"); + if (attribute === null) { + return; + } + const attributeType = attribute.getAttributeType(); + // @ts-expect-error + const isNumberAttribute = attributeType === "number"; // This errors because the attribute is a StringAttribute, not a NumberAttribute + const isStringAttribute = attributeType === "string"; // This works because the attribute is a StringAttribute +}