Solved

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

  • 24 August 2023
  • 14 replies
  • 173 views

Userlevel 1
Badge

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

}

icon

Best answer by Keith Richardson 24 August 2023, 21:49

View original

14 replies

Userlevel 7
Badge +17

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

Userlevel 6
Badge +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)

 

Userlevel 7
Badge +17

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

        }

 

Userlevel 4
Badge +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);

 

Badge +11

@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)))

 

Userlevel 1
Badge

@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.

Badge +11

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.

Userlevel 6
Badge +4

@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?

Userlevel 1
Badge

 

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

Badge +11

Cast it as a nullable boolean (bool?)

Userlevel 6
Badge +3

Have you tried this?
 

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

 

Userlevel 6
Badge +4

Hi @DoShawhan 

 

As everybody you can also try this way:

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

 

 

Userlevel 4
Badge +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);
}
}

 

Userlevel 1
Badge

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


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved