axaptax++dynamics-ax-2012dynamics-ax-2012-r3

X++ assign Enum Value to a table column


I am trying to pull the Enum chosen from a dialog and assign the label to a table's column.

For example: Dialog opens and allows you to choose from:

Surface

OutOfSpec

Other

These are 0,1,2 respectively.

The user chooses OutOfSpec (the label for this is Out Of Spec), I want to put this enum's Name, or the label, into a table. The column I'm inserting into is set to be a str.

Here's the code I've tried, without success:

    SysDictEnum dictEnum = new SysDictEnum(enumNum(SDILF_ScrapReasons));

    reason = dialog.addField(enumStr(SDILF_ScrapReasons),"Scrap Reason");
    dialog.run();
    if (!dialog.closedOk())
    {
        info(reason.value());
        return;
    }

    ttsBegin;
    // For now, this will strip off the order ID from the summary fields.
    // No longer removing the Order ID
    batchAttr = PdsBatchAttributes::find(itemId, invDim.inventBatchId, "OrderId");
    orders = SDILF_BreakdownOrders::find(batchAttr.PdsBatchAttribValue, true);
    if (orders)
    {
        orders.BoxProduced -= 1;
        orders.update();
    }
    // Adding a batch attribute that will include the reason for scrapping
    select forUpdate batchAttr;    
    batchAttr.PdsBatchAttribId = "ScrapReason";
    //batchAttr.PdsBatchAttribValue = any2str(dictEnum.index2Value(reason.value()));
    batchAttr.PdsBatchAttribValue = enum2str(reason.value());
    batchAttr.InventBatchId = invDim.inventBatchId;
    batchAttr.ItemId = itemId;
    batchAttr.insert();

Obviously this is not the whole code, but it should be enough to give the issue that I'm trying to solve.

I'm sure there is a way to get the int value and use that to assign the label, I've just not been able to figure it out yet.

EDIT

To add some more information about what I am trying to accomplish. We make our finished goods, sometimes they are out of spec or damaged when this happens we then have to scrap that finished good. When we do this we want to keep track of why it is being scrapped, but we don't want just a bunch of random reasons. I used an enum to limit the reasons. When the operator clicks the button to scrap something they will get a dialog screen pop-up that allows them to select a reason for scrapping. The code will then, eventually, put that assigned reason on that finished items batch attributes so that we can track it later in a report and have a list of all the finished goods that were scrapped and why they were scrapped.


Solution

  • I'm not entirely sure of your question, but I think you're just missing one of the index2[...] calls or you're not getting the return value from your dialog correctly. Just create the below as a new job, run it, make a selection of Open Order and click ok.

    I don't know the difference between index2Label and index2Name.

    static void Job67(Args _args)
    {
        Dialog          dialog      = new dialog();
        SysDictEnum     dictEnum    = new SysDictEnum(enumNum(SalesStatus));
        DialogField     reason;
        SalesStatus     salesStatusUserSelection;
        str             label, name, symbol;
        int             value;
    
        reason = dialog.addField(enumStr(SalesStatus), "SalesStatus");
        dialog.run();
    
        if (dialog.closedOk())
        {
            salesStatusUserSelection = reason.value();
    
            // Label
            label = dictEnum.index2Label(salesStatusUserSelection);
    
            // Name
            name = dictEnum.index2Name(salesStatusUserSelection);
    
            // Symbol
            symbol = dictEnum.index2Symbol(salesStatusUserSelection);
    
            // Value
            value = dictEnum.index2Value(salesStatusUserSelection);
    
            info(strFmt("Label: %1; Name: %2; Symbol: %3; Value: %4", label, name, symbol, value));
        }
    }