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

Contract class handling

(0) ShareShare
ReportReport
Posted on by 482
Hi,

We have an api response that is saved as string in a field in Table1. And let's say we have these contracts to match the saved json string.

 
[DataContractAttribute]
class IndividualDataContract
{
    private str firstName;
    private str lastName;
    private int age;
    private str city;
    private List  hobbies;
 
    [DataMemberAttribute('first_name')]
    public str parmFirstName(str _firstName = firstName)
    {
        firstName = _firstName;
        return firstName;
    }
    
    [DataMemberAttribute('last_name')]
    public str parmLastName(str _lastName = lastName)
    {
        lastName = _lastName;
        return lastName;
    }
 
    [DataMemberAttribute('age')]
    public int parmAge(int _age = age)
    {
        age = _age;
        return age;
    }
 
    [DataMemberAttribute('city')]
    public str parmCity(str _city = city)
    {
        city = _city;
        return city;
    }
 
    [DataMemberAttribute('hobbies')AifCollectionTypeAttribute('_hobbies',Types::Class, classStr(HobbyDataContract)),
AifCollectionTypeAttribute('return',Types::Class, classStr(HobbyDataContract))]]
    public List parmHobbies(List _hobbies = hobbies)
    {
        hobbies = _hobbies;
        return hobbies;
    }
 
 
[DataContractAttribute]
class HobbyDataContract
{
    private str type;
    private str level;
 
    [DataMemberAttribute('type')]
    public str parmType(str _type = type)
    {
        type = _type;
        return type;
    }
 
    [DataMemberAttribute('level')]
    public str parmLevel(str _level = level)
    {
        level = _level;
        return level;
    }
}

//there is more but let's stick to those two for demo purposes

We also have this helper class
-------------------------------------------------------------
public class Helper
{
    str age;
    str lastName;
 
 
    public str getAge()
    {
        age = this.removeUnneededCharacters(age);
        return subStr(age, 1, 4);
    }
 
 
    public str getLastName()
    {
        return this.removeUnneededCharacters(lastName);
    }
 
 
    public str removeUnneededCharacters(str _line)
    {
        int  position = strFind(_line, ':', 1, strLen(_line));
 
        return strDel(_line, 1, position);
    }

    
    public static Helper newFromContract(IndividualDataContract _dataContract)
    {
        Helper helper = new Helper();
 
        helper.age         = _dataContract.parmAge();
        helper.lastName    = _dataContract.parmLastName()
 
        return helper;
    }
}
 
Now we want to process this data and take the values from the json string and save the values in a table. We might need to re-format the values returned using helper class before taking the value.

Please note that the api returns about 30 fields. And only 6 fields out of 30 will need to be reformatted using helper class. Also there are other apis that can return similar response and those 6 fields will still need sameexact reformatting, that's why i created a helper class, as other classes for other apis might call it.
 
 
    protected void getData(str _line)
    {
        individualDataContract  = FormJsonSerializer::deserializeObject(classNum(IndividualDataContract), _line) as IndividualDataContract; 
        helper                  = Helper::newFromContract(individualDataContract);
 
        firstName               = individualDataContract.parmFirstName();
        city                    = individualDataContract.parmCity();
 
        age                     = helper.getAge();
        last Name               = helper.getLastName();
    }

 

1. Do you think the code is readable in getData method? or is there a better way to do it?

2. Also is it a good practice, to remove the helper class  and do all of this in the same contract class?
i think contract class shouldn't include this but i want to double check. I mean, if sth like this is acceptable:
 
[DataContractAttribute]
class IndividualDataContract
{
    private str firstName;
    private str lastName;
    private int age;
    private str city;
    private List  hobbies;
 
    [DataMemberAttribute('first_name')]
    public str parmFirstName(str _firstName = firstName)
    {
        firstName = _firstName;
        return firstName;
    }
    
    [DataMemberAttribute('last_name')]
    public str parmLastName(str _lastName = lastName)
    {
        lastName = _lastName;
        return lastName;
    }
 
    [DataMemberAttribute('age')]
    public int parmAge(int _age = age)
    {
        age = _age;
        return age;
    }
 
    [DataMemberAttribute('city')]
    public str parmCity(str _city = city)
    {
        city = _city;
        return city;
    }
 
    [DataMemberAttribute('hobbies')AifCollectionTypeAttribute('_hobbies',Types::Class, classStr(HobbyDataContract)),
AifCollectionTypeAttribute('return',Types::Class, classStr(HobbyDataContract))]]
    public List parmHobbies(List _hobbies = hobbies)
    {
        hobbies = _hobbies;
        return hobbies;
    }

    
    public str getAge()
    {
        age = this.removeUnneededCharacters(age);
        return subStr(age, 1, 4);
    }
 
 
    public str getLastName()
    {
        return this.removeUnneededCharacters(lastName);
    }
 
 
    public str removeUnneededCharacters(str _line)
    {
        int position = strFind(_line, ':', 1, strLen(_line));
 
        return strDel(_line, 1, position);
    }
}
I have the same question (0)

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 565 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans