Skip to main content
Solved

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

  • August 24, 2023
  • 14 replies
  • 290 views
  • Translate All

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

14 replies

Naveen Boga
Captain II
Forum|alt.badge.img+20

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

Translate

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

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)

 

Translate

Naveen Boga
Captain II
Forum|alt.badge.img+20

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

        }

 

Translate

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

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

 

Translate

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

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

 

Translate

Forum|alt.badge.img
  • Author
  • Freshman I
  • August 24, 2023

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

Translate

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

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.

Translate

Leonardo Justiniano
Jr Varsity II
Forum|alt.badge.img+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?

Translate

Forum|alt.badge.img
  • Author
  • Freshman I
  • August 24, 2023

 

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

Translate

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

Cast it as a nullable boolean (bool?)

Translate

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

Have you tried this?
 

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

 

Translate

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)

 

 

Translate

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

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

 

Translate

Forum|alt.badge.img
  • Author
  • Freshman I
  • August 24, 2023

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

Translate