Skip to content

Generic formContext.getAttribute() method returns undefined instead of e.g. Xrm.Attribute<any> #313

@aalzim

Description

@aalzim

Is your feature request related to a problem? Please describe.
If you try to call getAttribute() on a FormContext which inherits from Xrm.PageBase, you basically need to provide a fixed string argument for a given attribute, eg formContext.getAttribute("accountnumber").
This is fine and would return an object of the type Xrm.Attribute

However, if you have a list of attribute names and you want to use that for any action on the corresponding attribues you get a return value of 'undefined'.
As a simple example, consider this:

const accountnumberfields = ["accountnumber", "new_someOtherAccountnumber", "new_aThridAccountNumber"];
accounnumberfields.forEach(accNr =>{
  const accNrVal = formContext.getAttribute(accNr).getValue() // here we get an error because we cannot call getValue() on undefined
  if(accNrVal){
    formContext.getControl(accNr).setDisabled(true);  // similar as above, we cannot call setDisabled() on undifined
  }
}

as per the generatedd.ts file:

getAttribute(attributeName: string): undefined;
getAttribute(delegateFunction: Xrm.Collection.MatchingDelegate<Xrm.Attribute<any>>): Xrm.Attribute<any>[];
....
getControl(controlName: string): undefined;
getControl(delegateFunction: Xrm.Collection.MatchingDelegate<Xrm.Control<any>>): Xrm.Control<any>[];

Describe the solution you'd like
I think it would make sense to have getAttribute(attributeName: string) have a return type of Xrm.Attribute | undefined instead of just undefined, similar to the getAttribute(delegate) method.
Same with the getControl(controlName: string) method.

Describe alternatives you've considered
I've set up a wrapper method like this:

static GetStringAttribute(formContext: Form.account.Main.AccountForm, attrName: string): Xrm.Attribute<string> | null {
  let stringAttr: Xrm.Attribute<string> | undefined = undefined;
  stringAttr = formContext.getAttribute(attrName);
  if (stringAttr) {
    return stringAttr;
  }
  console.log(`Attribute ${attrName} not found as a string attribute!`)
  return null;
}

But this seems tedious since there should be an easier solution, especially considering that this specific example would only work for this single account form. I'd actually would need to pass a Xrm.PageBase<...> context as parameter to be more generic.

Additional context
If I'm just being stupid here and overlooking the obvious intended solution, please let me know :)
Otherwise I think this would be a sensible change

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions