12341234
12341234

Reputation: 404

Javascript / MS Dynamics CRM 2016: Changing value of option set field using confirm box

I have an option set field in Dynamics CRM that has two values: "In Progress" (default) and "Completed". Using JavaScript, I want to issue a confirm box that triggers during the field event OnChange. The confirm box warns the user that if the user has selected "Completed" it will lock all the other fields in the record.

Anyway, I wrote my code such that the confirm box will set the value of the option set. For some reason, it is not changing the values of the field. If the user clicks "Completed" and when the user clicks 'Cancel' in the confirm box to confirm and validate, it would still set the field value to "Completed". Any reason why it would not set the field values? Here is my code:

function confirmTaskStatus() {
if (Xrm.Page.getControl("moc_taskstatus").getDisabled()){
    var taskStatusValue;
    var message = "Do you want to set this Task to Completed? 
                  You cannot edit, change or add anything to the Project Task fields 
                  once it is set to Completed";

  if (confirm(message) == true) {

      taskStatusValue = 223770000; // Display Label = "Completed" 
      Xrm.Page.getControl("moc_taskstatus").setDisabled(true);

      } else {

      taskStatusValue = 223770001; // Display Label = "In Progress"

    }

    Xrm.Page.getAttribute("moc_taskstatus").setValue(taskStatusValue);


}  
}


function saveTaskStatus() {
window.setTimeout(confirmTaskStatus, 1000);
}

Have mercy on me; I'm still quite new to scripting and Dynamics CRM.

Upvotes: 2

Views: 3795

Answers (1)

dynamicallyCRM
dynamicallyCRM

Reputation: 2990

It looks like the control is disabled (by looking at your code snippet). Disabled attributes SubmitMode is set to false, meaning, CRM will ignore any updates to the attribute unless you force CRM to save it by calling SetSubmitMode after the value is updated.

Xrm.Page.getAttribute("moc_taskstatus").setValue(taskStatusValue);
Xrm.Page.getAttribute("moc_taskstatus").setSubmitMode('always');

Upvotes: 6

Related Questions