I created an entry point to accept data from a web form and create a case w/ a contact. I’ve gotten all that working great. However, it’s not ending a Case creation email to the contact.
If I remove contact relation in the UI, and then add it back, BAM it sends the email. Does anyone know why this is? Did I do something wrong? Here is the Case and Contact creation code:
// create a new cases bean
$caseBean = BeanFactory::newBean('Cases');
// create a contact for the Case/Account
$contactBean = BeanFactory::newBean('Contacts');
$contactBean->salutation = $_POST['salutation'];
$contactBean->first_name = $_POST['first_name'];
$contactBean->last_name = $_POST['last_name'];
$contactBean->email1 = $_POST['email'];
$contactBean->phone_work = $_POST['phone'];
$contactBean->save();
// attach contact to account
$contactBean->load_relationship('accounts');
$contactBean->accounts->add($accountBean->id);
// attach the case to the account
$caseBean->account_id = $accountid;
// set case name from subject
$caseBean->name = $_POST['subject'];
// add the description
$caseBean->description = $_POST['description'];
// save the case
$caseBean->save();
// attach contact to case
$caseBean->load_relationship('contacts');
$caseBean->contacts->add($contactBean->id);
Can anyone point me in the right direction to get this working?
I’m no specialist in this area of code customizations, but a few ideas
Check if those functions (add, save) are returning any meaningful values that can help differentiate success from failure
Try to get a way to know if the hooks are firing. So, the hooks that fire when you use the UI, which are they, and are they firing with your code, when it saves? You can probably do this with some clever inspection of the logs, and possibly adding a few logging messages.
BTW, do you have XDEBUG working, and are you using a proper IDE and debugger?
I found that the email is triggered via the after_relationship_add logic_hook. It executes the creationNotify function in modules/AOP_Case_Updates/CaseUpdatesHook.php
So, I added some simple log output lines to that function just to make sure it was firing, and to see what data it was being fed.
As soon as I added that, and did a repair+rebuild it started sending out the email. I have no idea what’s going on. Even after I commented out the debug log commands, it still sent the email. I’ve done many repair+rebuild’s before this, and it had no effect. Does the repair+rebuild look for modified files only? Could this file have been changed and the repair+rebuild was missing it because it didn’t realize it was modified?
The only issue I have now is that the case number is missing from the email. I’ll try to figure that out next.
I’m able to get the case id by pulling the number from the SQL database after the record is created, then adding it to the bean. I can’t just re-pull the bean because I get a cached result, and the case_number hasn’t been created in it yet. Anyway, once I set the case_number in the bean it gets sent out in the email.
Is there a better way then pulling it via an SQL query?
While still hoping to find a better way, here is my working code for posterity:
// create a new cases bean
$caseBean = BeanFactory::newBean('Cases');
// create a contact for the Case/Account
$contactBean = BeanFactory::newBean('Contacts');
$contactBean->salutation = $_POST['salutation'];
$contactBean->first_name = $_POST['first_name'];
$contactBean->last_name = $_POST['last_name'];
$contactBean->email1 = $_POST['email'];
$contactBean->phone_work = $_POST['phone'];
$contactID = $contactBean->save();
// attach contact to account
$contactBean->load_relationship('accounts');
$contactBean->accounts->add($accountBean->id);
// attach the case to the account
$caseBean->account_id = $accountid;
// set case name from subject
$caseBean->name = $_POST['subject'];
// add the description
$caseBean->description = $_POST['description'];
// save the case
$caseID = $caseBean->save();
// We need to get the case_number from the DB, because even retreving the bean again gets cached results (no case_number)
// This means the case creation email is missing the case number.
global $db;
$query = "SELECT case_number FROM suitecrm_db.cases WHERE id = '" . $caseID . "'";
$result = $db->query($query);
$value = mysqli_fetch_object($result);
$caseBean->case_number = $value->case_number;
// attach contact to case
$caseBean->load_relationship('contacts');
$caseBean->contacts->add($contactBean->id);