web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Error message when defaulting field values

(0) ShareShare
ReportReport
Posted on by 70

I currently get the below error message when attempting to populate default values from an external validation API.  I'm merely attempting to update specific fields, running the below code in the init() of the Contract Entry form, and then doing a BRKContractHeader_ds.executeQuery() in the init() of the datasource on the form to get these passed in default values to populate the respective fields.  Any suggestions on what is causing the error?
8713.pastedimage1598358508863v1.png

Below is the code I'm using to implement these default values, specifically line 23.

public void ApplyInstructions(BRKFormDataInstructionCollection instructions)
    {
        BRKFormDataInstruction fieldInstruction;
        DictTable dictTable;
        DictField dictField;

        dictTable = new DictTable(_tableNumber);
        var fieldId = dictTable.fieldNext(0);

        while(fieldId)
        {
            dictField = dictTable.fieldObject(fieldId);
            if(!dictField.isSystem())
            {
                fieldInstruction = instructions.getInstructionForRecordField(_tableNumber, fieldId);
                if(fieldInstruction != null)
                {
                    _dataSource.object(fieldId).mandatory(fieldInstruction.isRequired());
                    _dataSource.object(fieldId).visible(!fieldInstruction.isHidden());

                    if(fieldInstruction.getDefaultValue() != null)
                    {
                        _dataSource.object(fieldId).setValue(fieldInstruction.getDefaultValue());
                    }

                }
            }
            fieldId = dictTable.fieldNext(fieldId);
        }
    }

I have the same question (0)
  • Martin Dráb Profile Picture
    239,449 Most Valuable Professional on at

    The first step should be isolating the problem. Use the debugger to find out when exactly your code fails and with which data.

    I suspect that you're setting a value of a wrong type, but you're the only one who can find details.

  • delarosaau Profile Picture
    70 on at

    Thanks for the quick response, Martin.  Unfortunately when I debug I'm not getting any errors, even with all CLR exceptions turned on.  The data appears to insert fine and no errors are thrown.

  • Verified answer
    Martin Dráb Profile Picture
    239,449 Most Valuable Professional on at

    The error must come from somewhere.

    Try putting a breakpoint Info.add() (make sure you don't have ticked "Load symbols only for items in the solution) or - my favourite solution - break on Microsoft.Dynamics.Ax.Xpp.ErrorException (you can find instructions here).

  • delarosaau Profile Picture
    70 on at

    In any case, I had an intuition that it could be one of the date values that was being passed in; I did a str2Date() on that particular value and the error is now resolved.  Thanks for your patience.

  • Martin Dráb Profile Picture
    239,449 Most Valuable Professional on at

    If you tried to put a string into a date field, it's obviously wrong.

    Isn't the right solution returning a date value from getDefaultValue() (the return type would be declared as anytype) instead of returning a string representation of a date and converting it to an actual date? Using the right data type would also prevent all potential issues with string formatting.

  • delarosaau Profile Picture
    70 on at

    That is absolutely correct, Martin.

    This is what I've been attempting to do:

    while(fieldId)
            {
                dictField = dictTable.fieldObject(fieldId);
                if(!dictField.isSystem())
                {
                    fieldInstruction = instructions.getInstructionForRecordField(_tableNumber, fieldId);
                    if(fieldInstruction != null)
                    {
                        var field = _dataSource.object(fieldId);
                        field.mandatory(fieldInstruction.isRequired());
                        field.visible(!fieldInstruction.isHidden());
    
                        switch (field)
                        {
                            case FormDateControl: //find type comparison
                                field.setValue(str2Date(fieldInstruction.getDefaultValue(), 213));
                                break;
                            default:
                                field.setValue(fieldInstruction.getDefaultValue());
                                break;
                        }
                    }
                }
                fieldId = dictTable.fieldNext(fieldId);
            }

    The custom C# client and API that communicate back and forth both maintain the integrity of the Date type but unfortunately there is an intermediary method that transforms that Date into a string, mainly because it's intended to be a generic method that can deliver messages for any field across our various types on this form (Contract Header, Contract Line, Shipping Scenario).

    What I'm now attempting to decipher is (at line 13) how I might be able to read the type of the field I'm attempting to populate and switch the insertion method based upon that particular Field Type, since we don't have a generic type in X (I read your article on the subject!).  Thoughts and/or suggestions?

  • Verified answer
    Martin Dráb Profile Picture
    239,449 Most Valuable Professional on at

    You can use SysDictField.baseType() method to get the type of a field.

    The X++ way of returning different types from the method is declaring the return type as anytype and then return values of various types. If needed, you can then get the type of the value from typeOf().

    But if fieldInstruction() is a method defined in a C# library, then you can't use anytype. The way of doing it in C# before generics was returning an object (using implicit boxing of primitive values). You can use 'is' operator in C# to check if the value is of a particular primitive type. I don't know if the 'is' operator in X++ behaves the same.

  • delarosaau Profile Picture
    70 on at

    Final solution worked like a charm.  Thanks again Martin.

    while(fieldId)
            {
                dictField = dictTable.fieldObject(fieldId);
                Types fieldType = dictField.baseType();
                if(!dictField.isSystem())
                {
                    fieldInstruction = instructions.getInstructionForRecordField(_tableNumber, fieldId);
                    if(fieldInstruction != null)
                    {
                        var field = _dataSource.object(fieldId);
                        field.mandatory(fieldInstruction.isRequired());
                        field.visible(!fieldInstruction.isHidden());
    
                        switch (fieldType)
                        {
                            case Types::Date:
                                field.setValue(str2Date(fieldInstruction.getDefaultValue(), 213));
                                break;
                            default:
                                field.setValue(fieldInstruction.getDefaultValue());
                                break;
                        }
                    }
                }
                fieldId = dictTable.fieldNext(fieldId);
            }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 586 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 551

#3
CP04-islander Profile Picture

CP04-islander 408

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans