1

I am using https://github.com/delegateas/XrmDefinitelyTyped. I created the following script for a form that has a field called Program Year, Start Date, End Date. I would like this to be generic so that if in the Form Properties I set an onChange event for ProgramYear I can point it to TI.Forms.EventHandlers.onProgramYearChange then it will execute the script.

However, when I pass the field's execution context I cannot access the execution context of the other attributes as far as I know and I don't see any ways to get the other attributes of Start Date and End Date. Even if I cast form to any there is no getAttribute function defined. What is the correct way to approach this?

namespace TI.Forms.EventHandlers {
    export function onProgramYearChange(executionContext: Xrm.ExecutionContext<any>) {
        var form = executionContext.getFormContext();

        form.getAttribute("ti_programyear").addOnChange((context) => {
            const updatedProgramYear = context.getEventSource().getValue()[0].name;

            XrmQuery.retrieveMultiple(x => x.ti_program_years)
                .select(x => [x.ti_start_date, x.ti_end_date])
                .filter(x => Filter.equals(x.ti_name, updatedProgramYear))
                .execute(programYears => {
                    const startDateField = form.getAttribute("ti_start_date");
                    const endDateField = form.getAttribute("ti_end_date");

                    if (!startDateField.getValue() && !endDateField.getValue()) {
                        startDateField.setValue(programYears[0].ti_start_date);
                        endDateField.setValue(programYears[0].ti_end_date);
                    } 
                });
        });
    }
}

Can you access other attributes from the executionContext of an attribute?

uioporqwerty
  • 1,068
  • 3
  • 12
  • 30

1 Answers1

0

Can you access other attributes from the executionContext of an attribute?

Yes you can. In fact CRM will pass the whole execution context to the event handler, when you check the Pass execution context as the first parameter.

executionContext.getFormContext() will provide you the whole formcontext similar to earlier Xrm.Page.

This is what we have as working when onChange is attached on form load. Cleaned for brevity.

var standing = formContext.getAttribute("new_standing");

if (standing !== null) {
    standing.addOnChange(this.validateStatusChanged);
}

Below is native web api call & accessing formContext from success callback method.

validateStatusChanged: function (executionContext) {
var formContext = executionContext.getFormContext();

Xrm.WebApi.retrieveMultipleRecords("new_testEntity", query).then(
    function success(result) {
        if (result.entities.length > 0) {
            var limitField = formContext.getAttribute('new_limit');

            if (limitField.getValue() !== 1) {
                limitField.setValue(1);
            }
        }
    },
    function (error) {
        //
    }
);
}