Skip to main content
Solved

Set custom field to current date when a custom checkbox is clicked


Forum|alt.badge.img

Hello,

I have 2 extensions fields on the Project page. One is a checked box and the other is a date field. I am trying to set it up so that when the checkbox is checked, the current date is populated into the date field. When the checkbox is cleared, I want the date field to be cleared as well. I have this functionality working on another form but it is custom tables and not extensions. Here is my code

 

protected void _(Events.FieldUpdated<ContractExtDS.usrInstalled> e) {
        var row = e.Row;
         //I don’t think that it is getting past this line, but I can’t figure it out   
        if(!(bool)(e.Cache.GetValueExt<ContractExtDS.usrInstalled>(row)))
        {
          e.Cache.SetValueExt<ContractExtDS.usrInstallationCompletedDate>(row, DateTime.Now);
        }
        else
        {
          e.Cache.SetValueExt<ContractExtDS.usrInstallationCompletedDate>(row, null);
        }

}

Best answer by Keith Richardson

Cast the newvalue set to a bool? and try that. 


    protected void _(Events.FieldUpdated<ContractExtDS.usrInstalled> e)
    {
        bool? installed = (bool?)e.NewValue;

        if (installed == true)
        {
            e.Cache.SetValueExt<ContractExtDS.usrInstallationCompletedDate>(e.Row, PX.Common.PXTimeZoneInfo.Now);
        }
        else
        {
            e.Cache.SetValueExt<ContractExtDS.usrInstallationCompletedDate>(e.Row, null);
        }
    }

 

View original
Did this topic help you find an answer to your question?

14 replies

Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • 3404 replies
  • August 24, 2023

@DoShawhan  Code looks good to me and it should work like when we uncheck it should be NULL as per the code.


Zoltan Febert
Jr Varsity I
Forum|alt.badge.img+3

ContractExtDS.ursInstalled is bool? (nullable), but you try to cast it to bool.

Try to check it in this way:

if (e.Cache.GetValueExt<ContractExtDS.usrInstalled>(row) as bool? == true)

 


Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • 3404 replies
  • August 24, 2023

@DoShawhan Little bit modified but nothing much difference. Below code will help you debug and check what value we are fetching.

 

protected void _(Events.FieldUpdated<ContractExtDS.usrInstalled> e)
        {
            var row = e.Row;

            if (row != null)
            {
                ContractExtDS rowExt = row.GetExtension<ContractExtDS>(); 

                //I don’t think that it is getting past this line, but I can’t figure it out   
                if (rowExt.usrInstalled == true)
                {
                    e.Cache.SetValueExt<ContractExtDS.usrInstallationCompletedDate>(row, DateTime.Now);
                }
                else
                {
                    e.Cache.SetValueExt<ContractExtDS.usrInstallationCompletedDate>(row, null);
                }
            }

        }

 


Keith Richardson
Semi-Pro I
Forum|alt.badge.img+2

I assume you do not want it based on the business date, but the current date. DateTime.Now does not modify based on timezone, and can get messed up in the database when loading. Instead of DateTime.Now and DateTime.Today in Acumatica, you should use PX.Common.PXTimeZoneInfo.Now or PX.Common.PXTimeZoneInfo.Today

So the line would be:
 

e.Cache.SetValueExt<ContractExtDS.usrInstallationCompletedDate>(row, PX.Common.PXTimeZoneInfo.Now);

 


darylbowman
Captain II
Forum|alt.badge.img+13

@DoShawhan - I must be reading this wrong because no one else has mentioned it, but your expression looks backwards to me.

I think if you remove the ‘!’ it would be correct, according to your description of wanting to set the value when the checkbox is checked.

if((bool)(e.Cache.GetValueExt<ContractExtDS.usrInstalled>(row)))

 


Forum|alt.badge.img
  • Author
  • Freshman I
  • 14 replies
  • August 24, 2023
darylbowman wrote:

@DoShawhan - I must be reading this wrong because no one else has mentioned it, but your expression looks backwards to me.

I think if you remove the ‘!’ it would be correct, according to your description of wanting to set the value when the checkbox is checked.

if((bool)(e.Cache.GetValueExt<ContractExtDS.usrInstalled>(row)))

 

I tried this, but I don’t think that the event is being triggered, because I put

throw new PXException("here");

and the exception was never thrown. Also, when I save the project, the checkbox reverts back to how it was before I checked it.


darylbowman
Captain II
Forum|alt.badge.img+13
DoShawhan wrote:

I tried this, but I don’t think that the event is being triggered, because...the exception was never thrown.

Did you place the exception here, so that it would fire no matter what the condition evaluated to?

protected void _(Events.FieldUpdated<ContractExtDS.usrInstalled> e) 
{
    var row = e.Row;
    throw new PXException("here");

    //I don’t think that it is getting past this line, but I can’t figure it out   
    if((bool)(e.Cache.GetValueExt<ContractExtDS.usrInstalled>(row)))
    {
        e.Cache.SetValueExt<ContractExtDS.usrInstallationCompletedDate>(row, DateTime.Now);
    }
    else
    {
        e.Cache.SetValueExt<ContractExtDS.usrInstallationCompletedDate>(row, null);
    }
}

If that’s the case, and it’s still not firing, perhaps try unpublishing all packages, republishing, and restarting the application, or maybe someone else has an idea.


Leonardo Justiniano
Jr Varsity II
Forum|alt.badge.img+4
DoShawhan wrote:
darylbowman wrote:

@DoShawhan - I must be reading this wrong because no one else has mentioned it, but your expression looks backwards to me.

I think if you remove the ‘!’ it would be correct, according to your description of wanting to set the value when the checkbox is checked.

if((bool)(e.Cache.GetValueExt<ContractExtDS.usrInstalled>(row)))

 

I tried this, but I don’t think that the event is being triggered, because I put

throw new PXException("here");

and the exception was never thrown. Also, when I save the project, the checkbox reverts back to how it was before I checked it.

 

Hi @DoShawhan 

 

Did you set the checkbox “CommitChanges” property to true?


Forum|alt.badge.img
  • Author
  • Freshman I
  • 14 replies
  • August 24, 2023
Leonardo Justiniano wrote:

 

Hi @DoShawhan 

 

Did you set the checkbox “CommitChanges” property to true?

No, I had not. However, the false statement is working but I cannot get the true part of the if statement to run when the checkbox is checked. I have got an error that says “object cannot be converted to Boolean.“ Does anyone know what the object looks like that this 

e.Cache.GetValueExt<ContractExtDS.usrInstalled>(row)

returns?

 

Thanks


darylbowman
Captain II
Forum|alt.badge.img+13

Cast it as a nullable boolean (bool?)


Zoltan Febert
Jr Varsity I
Forum|alt.badge.img+3

Have you tried this?
 

if (e.Cache.GetValueExt<ContractExtDS.usrInstalled>(row) as bool? == true)

 


Leonardo Justiniano
Jr Varsity II
Forum|alt.badge.img+4

Hi @DoShawhan 

 

As everybody you can also try this way:

if((e.Cache.GetValueExt<ContractExtDS.usrInstalled>(row) ?? false) == true)

 

 


Keith Richardson
Semi-Pro I
Forum|alt.badge.img+2

Cast the newvalue set to a bool? and try that. 


    protected void _(Events.FieldUpdated<ContractExtDS.usrInstalled> e)
    {
        bool? installed = (bool?)e.NewValue;

        if (installed == true)
        {
            e.Cache.SetValueExt<ContractExtDS.usrInstallationCompletedDate>(e.Row, PX.Common.PXTimeZoneInfo.Now);
        }
        else
        {
            e.Cache.SetValueExt<ContractExtDS.usrInstallationCompletedDate>(e.Row, null);
        }
    }

 


Forum|alt.badge.img
  • Author
  • Freshman I
  • 14 replies
  • August 24, 2023
Keith Richardson wrote:

Cast the newvalue set to a bool? and try that. 


    protected void _(Events.FieldUpdated<ContractExtDS.usrInstalled> e)
    {
        bool? installed = (bool?)e.NewValue;

        if (installed == true)
        {
            e.Cache.SetValueExt<ContractExtDS.usrInstallationCompletedDate>(e.Row, PX.Common.PXTimeZoneInfo.Now);
        }
        else
        {
            e.Cache.SetValueExt<ContractExtDS.usrInstallationCompletedDate>(e.Row, null);
        }
    }

 

This worked!

Thanks


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings