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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

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

(1) ShareShare
ReportReport
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?
I have the same question (0)
  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at
    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.
  • FaresBenMiled Profile Picture
    8 on at
    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
    237,801 Most Valuable Professional on at
    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
    8 on at
    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans