This topic is locked

How to insert date in a field depending on value of another field

1/29/2009 8:58:13 PM
PHPRunner General questions
hichem author

Here's my challenge:

On the list page I have a number of field including "DateTimeClosed" and "Status" fields. Status is a dropdown list with values (open, closed, pending)
How can I autoupdate the "DateTimeClosed" when the value of 'Status" is manually set to closed?
I checked on the visual editor and not sure how to do this and if the custom field would allow to put such a check?
Any ideas are much appreciated.

Many thanks in advance

J
Jane 1/30/2009

Hi,
use custom format on the "View as" settings dialog on the Visual Editortab.

Here is a sample:

global $data;

if ($data["Status"]=="closed")

$value = date("d/m/Y");

hichem author 2/2/2009

Hi,

use custom format on the "View as" settings dialog on the Visual Editortab.

Here is a sample:


Thanks Jane.

I am still having the issue that the $value = date("d/m/Y") will be applied to all closed records in my list.

Is there a way to prevent updating all records?

when I refresh the page all records with closed date now have the same date? is there a way to adjust your code please to avoid such a situation? (i.e apply the date only to the currently updated record)

J
Jane 2/2/2009

Hi,
use Before record added/Before record updated events to update this field.

Here is a sample:

if ($values["Status"]=="closed")

$values["DateTimeClosed"] = now();

N
nickditrolio 2/3/2009

What is the difference between $data["status"] and $values["status"]? When do you use each?

Hi,

use Before record added/Before record updated events to update this field.

Here is a sample:

hichem author 2/3/2009

What is the difference between $data["status"] and $values["status"]? When do you use each?


Works now Jane. Thanks ever so much for your help.

It might help others so here's what I had to do to make it work

if ($values["StatusId"]==1)

{$values["DateTimeClosed"] = now();}

else

{$values["DateTimeClosed"] ='';}

return true;



Note that I had to reference the StatusId instead as I am using a lookup table for that field.

You need the else statement otherwise the column will remain with old data if you change the value from closed to open (after having set it to closed). The column is DateTimeCLosed so should only show a value if the status is set to closed.

Also found out if you don't use return true; in the end you won't be able to update the record at all.

I think that the difference between $data["status"] and $values["status"] depends on the function used.

The function BeforeEdit has a set of parameters that you can see in event.

Jane, am I right in understanding that some logic can be used to compare for example the $values["DateTimeClosed"] to $oldvalues["DateTimeClosed"] and use that to only update records if some critera is fulfilled?

J
Jane 2/3/2009

The function BeforeEdit has a set of parameters that you can see in event.



Yes. Please check list of available variables in the PHPRunner help:

http://www.xlinesoft.com/phprunner/docs/table_events.htm

Jane, am I right in understanding that some logic can be used to compare for example the $values["DateTimeClosed"] to $oldvalues["DateTimeClosed"] and use that to only update records if some critera is fulfilled?



Yes, you're right.

You can compare these values in the Before record added/Before record updated events:

if ($values["DateTimeClosed"]!=$oldvalues["DateTimeClosed"])

{

//field value was changed

//... your code here

}