Choice types

Of the data types used for the Dynamics GP service, choice types require the most additional work to reference from your web service application. The "choice" type allows a single parameter to contain one of several different types of data. For instance, the FinanceCharge property for a customer is of type MoneyPercentChoice. It can contain either a money value or a percent value.

When you look at the MoneyPercentChoice in the generated proxy, you will see that that it apprears with a single Item property, which has the type Object.

Cc508720.WS_ChoiceTypeBrowse(en-us,MSDN.10).gif

When your code reads the value of a parameter that uses the "choice" type, it must determine what type of value to read. When your code writes a choice parameter, it must specify which type of data it is writing. You can get this type information from the generated proxy code, or from the WSDL file for the Dynamics GP service.

In Visual Studio, you can double-click the Item property in the Object Browser to view its implementation details in the generated proxy. The following is the generated code for the MoneyPercentChoice type in the proxy for the legacy endpoint of the Dynamics GP service:

public partial class MoneyPercentChoice {

private object itemField;

[System.Xml.Serialization.XmlElementAttribute("Amount", typeof(MoneyAmount))]
[System.Xml.Serialization.XmlElementAttribute("Percent", typeof(Percent))]
    public object Item {
        get {
            return this.itemField;
        }
        set {
            this.itemField = value;
        }
    }
}

Notice in the XML serialization that the type for the Item property can be MoneyAmount or Percent, which are standard types for the Dynamics GP service.

Reading a choice type

The following C# example shows how you would retrieve the value of the FinanceCharge property for a customer object. The example will find out the type of the Item property for the MoneyAmount type, and then retrieve the underlying type.

DynamicsGPService.MoneyPercentChoice financeCharge;
DynamicsGPService.MoneyAmount financeChargeAmount;
DynamicsGPService.Percent financeChargePercent;

// Examine the finance charge for the customer
financeCharge = customer.FinanceCharge;

if (financeCharge.Item.GetType() == typeof(DynamicsGPService.MoneyAmount))
{
    financeChargeAmount = (DynamicsGPService.MoneyAmount)financeCharge.Item;
    MessageBox.Show("Finance charge is a money amount of: " +
    financeChargeAmount.Value.ToString());
}

if (financeCharge.Item.GetType() == typeof(DynamicsGPService.Percent))
{
    financeChargePercent = (DynamicsGPService.Percent)financeCharge.Item;
    MessageBox.Show("Finance charge is a percentage: " +
    financeChargePercent.Value.ToString());
}

Writing a choice type

The following C# example shows how you would write the value of the FinanceCharge property for a customer object. The value being written has the type MoneyAmount.

MoneyAmount financeCharge = new MoneyAmount();

financeCharge.Currency = "USD";
financeCharge.DecimalDigits = 2;
financeCharge.Value = 5000.00;
customer.FinanceCharge.Item = financeCharge;