Non-db Field via SQL Query

Iā€™m relatively new to SuiteCRM and trying to figure out if something is possible and if so, how to do it. In the system Iā€™m developing, I have added a module called ā€œHospitalsā€ similar to Accounts that is related to Calls. Iā€™ve been asked to add a field to the Calls module called ā€œSignificant Contactā€. Users will check that based on whatever criteria the project staff comes up with. All that is simple enough.

The next piece, however, is to display on the Hospital detail view the date of the most recent significant contact for that hospital. Basically, I need to get the max(date_start) from the calls table where calls_cstm.significant_contact_c = 1 for the particular hospital. Iā€™ve found some code that looks similar to what I want to do, which is create a non-db field (i.e. most_recent_sig_contact) with the SQL necessary to get the appropriate value. What Iā€™m not entirely sure of is where this code goes in my custom module and what, exactly, it needs to look like.

If anyone can provide an example of this or give me some pointers to this sort of thing in the docs, I would be much obliged.

Hello,
I think you should make a code in view.detail.php file. First get your expected result from query you used in your above comment.Here is some code sample which may help you.

  1. view.detail.php in custom directory of your custom module.

In display function.

// make a code to get your result
$query = ā€œYour query should go hereā€;
$date = $db->fetchByAssoc($db->query($query));
$maxDate = $date[ā€˜date startā€™];

  1. Then Assign this field like below
    $this->dv->ss->assign(ā€˜maxDateā€™, $maxDate);
    // Assign this before parent display call.

  2. Use this in detailviewdefs.php in custom
    =>
    array(
    ā€˜nameā€™ => ā€˜your nondb fieldā€™,
    ā€˜labelā€™ => ā€˜LBL_Lableā€™,
    ā€˜customCodeā€™ => ā€˜{$maxDate}ā€™
    ),

Hope this will you.
Thanks.

Thanks. I think Iā€™m getting close and this is very helpful.

One question I have is about the view.detail.php. That file did not already exist. I assumed I need to make one. I copied one from another module putting it in modules/hosp_Hospital/view/view.detail.php. Is that the right place for it? Then I edited it, removing everything except the __construct() and display() functions and changed the names to use ā€œhosp_Hospitalā€ in place of the names that were there. I added your four lines to the display function, with the only other line in that function being ā€œparent::display()ā€ at the end. Does that sound right?

The field is being displayed with the label I assigned but itā€™s not being populated, so clearly Iā€™ve done something wrong. It isnā€™t clear to me that the view.detail.php file that I created is actually being run or loaded. Do I need to do something so that it will be?

This will probably be obvious to others but isnā€™t to me: Should I create a field first, and then assign it this value or just modify the code without creating a field through any other means?

Hello there,

One question I have is about the view.detail.php. That file did not already exist. I assumed I need to make one. I copied one from another module putting it in modules/hosp_Hospital/view/view.detail.php. Is that the right place for it?

ā€“ Yes because this is your own module so this can be right place but in SuiteCRM standard method is to put this in custom dir. but thatā€™s ok.As I assumed your module name is ā€œhosp_Hospitalā€ I have make a code sample for it. Just copy and paste into your view.detail.php and put your sql query into right place.

require_once(ā€˜include/MVC/View/views/view.detail.phpā€™);

class hosp_HospitalViewDetail extends ViewDetail {

function display() {
    global $db;

    // make a code to get your result
    $query = "Your query should go here";
    $runQ = $db->query($query);
    $date = $db->fetchByAssoc($runQ);
    $maxDate = $date;
    $this->dv->ss->assign('maxDate', $maxDate);
    parent::display();
}

}

And about non-db field if you have not created then please first create.rest please follow my recent post.

If still getting any doubts you can share your view.detail.php and detailviewdefs.php file.So I will make changes in that.

Thanks.

Once again, thank you. The code you just posted is pretty close to what I had. The only significant difference is that I hadnā€™t included the ā€œrequire_onceā€ line. The bigger error, however, turns out to be where I put the file. I had this in a sub-directory called ā€œviewā€ but it was supposed to be ā€œviewsā€ (plural). Once I fixed that, the code actually ran. Thank you very much for your time and explanation.