Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Unanswered

how to make the visibility of a DataMember in DataContract dependant on a boolean variable X++

Posted on by 8
Hello everyone,

my problem is that I want to have a dataMember to be ommitted if a certain condition is verified, so I can't just use true or false in SysOperationControlVisibility() since it's variable, i tried using a vaible, calling a bollean method, using if condition but it always returns an error like a parathese is missing (which i verified multiple times that it wasn't the case).

This is my code:
 
    public void contactExist(boolean _exist)
    {
        if(_exist)
        {
            primaryContactExist = true;
        }
        else
        {
            primaryContactExist = false;
        }
    }

    [DataMemberAttribute("Contacts"), SysOperationControlVisibility(primaryContactExist),
        AifCollectionType("_contactsResponse", Types::Class, classStr(MAN_HMCVendTableContactsResponse)),// Line
    AifCollectionType("return", Types::Class, classStr(MAN_HMCVendTableContactsResponse))]//Line
    public List parmContacts(List _contactsResponse = contactsResponse)
    {
        contactsResponse = _contactsResponse;
        return contactsResponse;
    }
EDIT: even after testing with the values True and False for example SysOperationControlVisibility(false) it didn't solve my problem of ommiting this dataMember in the response, so I guess this won't help me since "Contacts" still exist in the json payload response


and I couldn't use if else for this dataMember like I mentioned before, any ideas please?
  • FaresBenMiled Profile Picture
    FaresBenMiled 8 on at
    how to make the visibility of a DataMember in DataContract dependant on a boolean variable X++
    I am using D365FO, the receiver API had issues validating the "Contacts" List ( the List have few elements: LastName, FirstName, Email, PhoneNumber), LastName is mundatory in their structure so sending a payload that have this field empty blocked the creation/update of the vendor sent, my initial solution was to declare the list when the contact info exists
    if(contactPerson)
                {
                    ContactsResponse contactsResponse = new ContactsResponse();
                    response.parmContacts().addEnd(contactsResponse);
    
    this gave me the result shown in the first screenshot which created an empty List, this is as far as I could progress and I don't know if it suffice or that they might demand that the "Contacts" List must totally disappear if there is no primaryContact since we didn't have our meeting to test yet.

    I just wanted to know if there is a way to do this (just to be prepared if the requirement demands this) or is the creation of two contract class and use the one most suited is a viable solution.
    Thanks Martin again for your help.
  • Martin Dráb Profile Picture
    Martin Dráb 228,089 Super User 2024 Season 2 on at
    how to make the visibility of a DataMember in DataContract dependant on a boolean variable X++
    I understand you want to omit it, but it's a keeping it there a business problem? If not, just let's not waste time with it. And if it's a problem, is it really a problem in F&O. Maybe it's a bug on the receiver side, because they should be able to process this valid JSON object.

    My understanding is that you're talking about AX 2012. Is that correct?
  • FaresBenMiled Profile Picture
    FaresBenMiled 8 on at
    how to make the visibility of a DataMember in DataContract dependant on a boolean variable X++
    Hello Martin and thanks for your clarification, I created an a point to point api that send serialized data in json format using the class RetailWebRequest and RetailWebResponse, my requirement is that I want to completely ommit the "Contacts" object in my json payload when the vendor doesn't have an assigned primaryContact.
    The payload structure is dependant on the contract class so I tried to create logic there but as you clarified the attributes are static metadata of a class, the only option I see now is to create two contract classes and call from the service class the one with the design I want (add an if(contactExist) in the service and call on the right structure to populate the response).
    Is this method ok to be used or is there a more direct way to solve my problem.

    Thank you in advance.
  • Martin Dráb Profile Picture
    Martin Dráb 228,089 Super User 2024 Season 2 on at
    how to make the visibility of a DataMember in DataContract dependant on a boolean variable X++
    This is impossible. The attributes are static metadata of a class; there is no way how to execute code there or to refer to data of a particular instance.
     
    Also, the attribute says whether the value should be displayed in the dialog created by the automatic UI builder, while you seem to want something else.
     
    To be able to suggest a solution that can be implemented, we would need to know what business problem you're trying to address. I guess you're using the contract for a custom service, but I have no idea what problem you have with the empty property.

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,252 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,089 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans