Custom Entry Point - Case creation not sending email to contact

I hope I’m missing something simple here…

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

  1. Check if those functions (add, save) are returning any meaningful values that can help differentiate success from failure

  2. 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?

1: Here are the returns for each of those functions:


Fri Mar 30 17:13:11 2018 [2960][-none-][FATAL] contactBean->save() returns: 5a24271b-2a2e-a99e-c63d-5abea8e57fd9
Fri Mar 30 17:13:11 2018 [2960][-none-][FATAL] contactBean->accounts->add() returns: 1
Fri Mar 30 17:13:11 2018 [2960][-none-][FATAL] caseBean->save() returns: 794f6615-13ea-5eae-69a6-5abea84a6fa5
Fri Mar 30 17:13:11 2018 [2960][-none-][FATAL] caseBean->contacts->add() returns: 1

2: When using the web form, here are the hooks that are called: https://pastebin.com/Ab51H4bZ
When adding the contact to the case, here are the hooks called: https://pastebin.com/CQCXrZRv

BTW: No, but I’m sure I can get xdebug set up. I’m using Kate on Linux, not really an IDE.

That’s a lot of information for the middle of a Easter Holiday… :slight_smile:

I’m afraid you’ll have to look into it yourself. Adding debugging support will take you a long way in understanding the internals of SuiteCRM.

Ok… to add to the weirdness of this…

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 pretty flabbergasted at this point.

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

I am glad you got it working, and thanks for sharing the code :slight_smile:

When I am flabbergasted at what is happening, I normally use this technique to see what SuiteCRM is doing:

https://pgorod.github.io/Audit-File-Accesses/

For example, watching what happens Studio changes something, or during a Quick Repair and rebuild, normally is a good way to learn some more SuiteCRM.

Happy Easter, if that means anything to you! :slight_smile: