Skip to main content

Notifications

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 312
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);
    }
}

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,304 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans