Kanini
Kanini

Reputation: 1993

CRM 2011 - JavaScript - Where to set Business Required

In a form, we want the Reject Reason to be set as Required if the value in the Option Set field Decision is selected as Reject. I wrote a JS function which is then associated with OnChange of field Decision and I set the Reject Reason field to be Required.

if (decisionOptionSetValue == 100000006 && decisionOptionSetText == "Reject") {
    Xrm.Page.getAttribute("new_rejectionreason").setRequiredLevel("required");
}

Now, the above worked fine with no issues.

The problem is if I open the same record, I can happily remove the value from the Reject Reason field (new_rejectionreason) and it will not throw an error because, my code fires only when we have a Change of value of the Decision option set which, in this case, has not happened.

Now, where do check to prevent these?

Option 1: Do I have an OnChange on the new_rejectionreason field so that I check if the value has been changed?

Option 2: Do I do the check as part of OnSave before I save the form and prevent the form from saving (?) and set this field as Required

Option 3: Do I do the check as OnLoad and set the field new_rejectionreason as Required

Any other option?

Upvotes: 3

Views: 1987

Answers (3)

Konrad Viltersten
Konrad Viltersten

Reputation: 39068

I would simply prevent saving of the entity under condition that the status is set to rejected but the reason is empty. Somehow, I feel uneasy setting a field as business required or not depending on the value in another field.

Upvotes: 0

glosrob
glosrob

Reputation: 6715

Why not make that field read only if you would rather not have the user set the value? So when they change the value of the decision field, add the following:

Xrm.Page.ui.controls.get('new_rejectionreason').setDisabled(true);

So now the user cannot edit the value without changing the decision field again.

Edit: I think understand now based on your comment. I think the simplest is adding an extra check on load.

function OnLoad() {
    var decisionValue = Xrm.Page.getAttribute('new_decision').getValue();
    if (decisionValue !== null) {
        Xrm.Page.getAttribute('new_rejectionreason').setRequiredLevel('required');
    }
}

So when your page loads, it checks the Decision field. If it finds a value, it knows that the Reject Reason field is required and marks it as such.

All you need to do then is to modify your OnChange handler for Decision to unset the required value if they remove the value.

if (decisionOptionSetValue === 100000006 && decisionOptionSetText === 'Reject') {
    Xrm.Page.getAttribute('new_rejectionreason').setRequiredLevel('required');
}
else {
    Xrm.Page.getAttribute("new_rejectionreason").setRequiredLevel('none');
}

Upvotes: 3

Pedro Azevedo
Pedro Azevedo

Reputation: 2507

In my opinion the best choice is Option 3. Because you warning user that the field new_rejectionreason is required and don't wait for a save form to warning that. For option 1 you have to force a onchange in new_rejectionreason and this way is not simple to understand later why force the onchange.

Another option that i don't recommend but is another way is doing this verification in plugin and launch a InvalidPluginExecutionException this stops the execution of plugin and send a message to user. This is only for your information don't use this methods for this case.

Upvotes: 3

Related Questions