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

Community site session details

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

Validate a field while modifying another

(0) ShareShare
ReportReport
Posted on by 5

Hi,

I have a forum with two text fields, field A of size 1000 chars and field B of size 255 chars.

I am duplicating/mirroring content of field A to field B while field A is being modified.

I want to show an error message and mark field B in red when it exceeds 255 chars while field A is being modified.

How do I go about that?

I have tried validateWrite(), and tried to call validateField() method while field A is being modified, it kind of works, I get the correct error message, however the wrong field is marked in red (field A is marked in read instead of field B)

My code (it works, only the wrong field is marked in red):

0755.img-1.PNG

3301.img-2.PNG

0564.img-3.PNG

I have the same question (0)
  • GirishS Profile Picture
    27,825 Moderator on at
    RE: Validate a field while modifying another

    Hi Hi Sar Sa,,

    Have you tried the above code?

    Is it working for your scenario?

    Thanks,

    Girish S.

  • GirishS Profile Picture
    27,825 Moderator on at
    RE: Validate a field while modifying another

    Hi Sar Sa,

    Please follow the below code and check whether its work for your scenario.

    1. Override the field A validate method of the form control and add the below code. So, you want the user to save the record and throw only warning.

    if(strLen(Sample.FieldA) > 255)
    {
        warning("Content of field b canoot bo largr then 255");
    }

    2. Override the field B validate method of the form control and add the below code. So, you want the user to save the record and throw only warning.

    if(strLen(Sample.FieldB) > 10)
    {
        warning("Content of field b canoot bo largr then 255");
    }

    3. Override the form DataSource active method and add the below code for changing the background color of the text box.

    if(strLen(Sample.FieldA) > 255)
    {
        Sample_FieldB.colorScheme(FormColorScheme::RGB);
        Sample_FieldB.backgroundColor(WinAPI::RGB2int(136,8,8));
    
    }
    else
    {
        Sample_FieldB.colorScheme(FormColorScheme::RGB);
        Sample_FieldB.backgroundColor(WinAPI::RGB2int(255,255,255));
    }

    Thanks,

    Girish S.

  • Sar Sa Profile Picture
    5 on at
    RE: Validate a field while modifying another

    Hi Alex, "the string length and all that is maintained through extended data type", that is not true in my case. I allow both fields to accept 1000 characters, I don't want to prevent the user from entering more than 255 chars, they can enter as much as they want, but I want them to get an error when they exceed 255 and not prevent them from entering more than 255.

  • Alex Panait Profile Picture
    170 on at
    RE: Validate a field while modifying another

    You need to move this code on the validation method. If you modified it, it is already modified so the user can just save it, even if the value is wrong. As others mentioned, the string length and all that is maintained through extended data type.

  • Sar Sa Profile Picture
    5 on at
    RE: Validate a field while modifying another

    I am completely new to this, so this is the only way I know of, please guide me if you have a solution.

  • GirishS Profile Picture
    27,825 Moderator on at
    RE: Validate a field while modifying another

    Hi Sar Sa,

    can you tell me why you are following extension concepts?

    I think "TableX" you have mentioned in the code is custom table I guess.

    So in that case why you are following COC?

    Thanks,

    Girish S.

  • Sar Sa Profile Picture
    5 on at
    RE: Validate a field while modifying another

    Thanks for the answer Martin, but your answer does not solve the issue I am having unfortunately, it only refactors my code to be better. The code validates Field B correctly and I get the correct error message, however field A is marked in red instead of field B when this code is called (on change of field A):

    this.validateField(fieldNum(TableX, Field_B));

  • Martin Dráb Profile Picture
    236,160 Most Valuable Professional on at
    RE: Validate a field while modifying another

    Regarding your code... You seem to want to run your logic when modifying the field, not when saving the record, right?

    Your call of validateWrite() in modifiedField() looks weird to me. You also ingore the return value, so does nothing exept of adding one more infolog message. I guess we can ignore this call, can't we?

    Also, did you consider doing the validation just on record saving? Would it be a problem?

    By the way, the following piece of code is over-complicated and it contains a field name hard-coded at text. That's wrong, because such texts aren't validate by compiler and the usage of the field wouldn't appear in References.

    DictTable dt;
    fieldId fId;
    dt = new DictTable(tableNum(TableX));
    if (dt)
    {
        fId c= dt.fieldName2Id("Field_B");
    	this.validateField(fId);
    }

    It can be replaced with something much simpler, and safer:

    this.validateField(fieldNum(TableX, Field_B));

    By the way, working with your code would be much easier if you provided it as text and not as pictures. Next time, please copy your code and paste it with Insert > Code (in the rich formatting view). That's what I did.

  • Martin Dráb Profile Picture
    236,160 Most Valuable Professional on at
    RE: Validate a field while modifying another

    It's a pity that you again failed to tell us anything about the version, but your screenshots show that it's not about Dynamics AX at all. You're using D365FO. I'm going to move this thread to the right forum.

  • Sar Sa Profile Picture
    5 on at
    RE: Validate a field while modifying another

    Updated my post with the code

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 2,370

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 675 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans