0

I know the experts will laugh at this question, but I'm really struggling. Please take a look at this snippet. I am very new to CRM and JavaScript.

So the issue is that it doesn't matter what I pick from the lookup list, it pops on the Alert. I can see where "incomplete" doesn't match the "complete" but it still fires the Alert.

Do I need to write an ODATA query to address this issue?

Function MPMain_OnChange() {
    try {
        //get the work order status.
        var wostatus = new Array();
        wostatus =Xrm.Page.getAttribute("workorderstatus").getValue();

        if (wostatus !=null) {
            var name = wostatus[0].name;

            if (name =="Completed") {
                alert("Order Complete");
            }
        }
    } catch {
        //there is an error.
    }
}

2 Answers2

1

In CRM 2016 you can use getSelectedOption() or simply getText(). Documentation can be found here. In your code, you would write:

var workOrderStatus = Xrm.Page.getAttribute("wordorderstatus").getSelectedOption();

This would return an object with a text and a value property, for example { text: 'Complete', value: 1 }.

I've tested and both work in my Dynamics CRM 2016 development environment. Here's a screenshot from my Chrome browser's console window:

enter image description here

You should then be able to evaluate the selected option's text and trigger an alert:

if (workOrderStatus === 'Completed') {
    alert('Order Complete.);
}

Notice I've used === not ==. See this SO answer for more information.

If that doesn't work you should try debugging. Press F12 in your browser, locate your script and enter a breakpoint at the start of your function. If you have difficulty doing this, put the following line at the top of your function (and publish your changes), press F12 top open your developer console, and then trigger the onChange event:

debugger;
Community
  • 1
  • 1
Dave Clark
  • 2,243
  • 15
  • 32
  • Thank you so much for responding and for the detail. I am using the debugger and the crazy thing is that I can see the value changing in the variable from "Completed" to "Awaiting" to "Cancel" but that If statement still fires. If I pass the variable to the IF it even displays the selected item when it does not match 'Completed'. I will try your solution and thank you and check as solution. – Christine Lee May 17 '17 at 15:15
1

Re-wrote the entire thing. This works. Not all JavaScript library items are available inside of Dynamics CRM, for example getSelectedOption.

function MPMain_OnChange() {
    try {
        // Get the work order status.
        var entityLabel, lookupFieldObject;
        lookupFieldObject = Xrm.Page.data.entity.attributes.get('worksorderstatus');

        if (lookupFieldObject.getValue() != null) {
            entityLabel = lookupFieldObject.getValue()[0].name;

            if (entityLabel != null && entityLabel == 'Completed')
                Xrm.Page.getAttribute('datecompleted').setValue(new Date());
        }
    }
    catch (error) {

    }
}

Sharing for anyone else who might come this way. Thank you!

Dave Clark
  • 2,243
  • 15
  • 32
  • Glad you got it working. However `getSelectedOption` and `getText` are not 'JavaScript library items', they are functions defined in the Xrm.Page namespace and _only_ work inside of Dynamics CRM. [Here's the link to Microsoft's documentation](https://msdn.microsoft.com/en-us/library/gg334409.aspx#BKMK_getText). – Dave Clark May 19 '17 at 14:41