Once in Code view, if you generate PHP, you should see the tasks_maint_events.php file, with the following lines of code:
//Custom Code @28-73254650
//-------------------------
//Write your own code here.
//-------------------------
//End Custom Code
Replace the text:
// Write your own code here.
with the following:
global $tasks;
global $DBIntranetDB;
if ($tasks->EditMode)
{
$tasks->user_id_assign_by->SetValue(CCDLookUp("emp_name", "employees", " emp_id=".
$DBIntranetDB->ToSQL($tasks->user_id_assign_by->GetValue(), ccsInteger), $DBIntranetDB));
}
else
{
$tasks->user_id_assign_by->SetValue(CCDLookUp("emp_name", "employees", " emp_id=".
$DBIntranetDB->ToSQL(CCGetUserID(), ccsInteger), $DBIntranetDB));
}
The above code consists of the following elements:
- tasks -the name of the record form on the page
- EditMode - property of the form, which specifies if the record is being edited. Depending on the value of this property, we either display the name of the person who originally submitted the task (Edit mode), or the person who is currently submitting the task (Insert mode).
- user_id_assign_by - the name of the Label within the Grid, and at the same time the name of the database field that was used to create this Label and which is now its data source.
- SetValue - a method of an object (in this case the Label), which can be used to modify the object's value.
- $tasks->user_id_assign_by->SetValue - fully qualified SetValue method, which tells the program which object it refers to. In other words, it is the SetValue method that affects user_id_assign_by field, which in turn belongs to the tasks grid.
- CCDLookup -CodeCharge function that supports retrieving a database value based on a field name, table name, and a condition. Here, this function retrieves the Employee Name (emp_name) from the employees table using the condition that the key (emp_id) equals the current value of the Label.
- ToSQL - CodeCharge function that converts a value into the format supported by the database. This function requires a parameter that tells it if a value should be converted to a number (Integer) or text. In this case, this function converts the current Label value to a number that can be used with the CCDLookup function. It is advisable to always use this function together with CCDLookup.
- $DBIntranetDB - the name of the object that defines the database connection that you want to use in CCDLookup function.
- CCGetUserID() - CodeCharge function that returns the ID of the user that is currently logged in.
The whole code reads approximately as follows:
- If a record is being edited: Assign the name of the person who originally submitted the issue to the user_id_assign_by Label, by looking up employee's name from employees table using CCDLookup function that uses the IntranetDB connection and the value of the user_id_assign_by Label.
- If a new record is being created: Assign current user to the user_id_assign_by Label by retrieving his/her name from employees table using CCDLookup function that uses IntranetDB connection and CCGetUserID function that obtains current user's ID.