Using the logic hook `after_relationship_add` & `after_relationship_delete` to send an email

I have two custom modules Cases and ProductInvestigation that shares a one to many relationship. I would like to use logic hook to send an email once the relationship is added. This is what I have so far as it relates to relationship aspect I’m not sure.

function createPIEmailwithCases(&$email,$action_array,$bean,$xtpl){

	/* Get PI Number */

	$xtpl->assign('PI_Number', $bean->pinum_c);
	
	/* Get PI Subject */
	$xtpl->assign('PI_Subject', $bean->name);
	
	/* Get Product Name */
	$xtpl->assign('Product_Name', $bean->product_name_c);
	
	/* Get Product Type */
	$xtpl->assign('Product_Type', $bean->type_of_product_c);
	
	/* Get Batch # */
	$xtpl->assign('Batch_Number', $bean->batch_c);
	
	/* Get Size(s) */
	$xtpl->assign('Size', $bean->size_c);
	
	/* Get Fill Date */
	$xtpl->assign('Fill_Date', $bean->filldate_c);
	
	/* Get Batch # */
	$xtpl->assign('BestBefore', $bean->bestbefore_c);

	/* Get PI Description */
	$xtpl->assign('PI_Desc', $bean->description);
	
			
	/* Get Reviewer Name */
	$xtpl->assign('Reviewed_By',$bean->reviewer_c);
	$xtpl->assign('Review_Date',$bean->review_date_c);

	
	 if ($arguments['related_module'] =='Cases'){
	$bean->new_case_c =$arguments['related_id'];
      //do something

    }
	
	
	/* Create email message using email template and data above */
	$xtpl->parse('block');
	$email->Body = from_html($xtpl->text('block'));
	
	return $email;
}

Check samples here:

One of them is about sending emails.

The one with the email is not using after relationship add and after relationship delete. It would be the same procedure?

I will believe the process is the same.

So how do get the values from the related module I don’t understand that part

Read the chapter on Beans

Particularly, the part about load_relationship. And search Google for examples of usages of that function.

/*Get Case for PI*/
		$action_array = $bean->get_linked_beans('pi_productinvestigation_cases_1','Cases');

$doc = array();
		foreach($action_array as $action){	
			
			 
			/* Get Case Details */
			$casenum =$action->getFieldValue('case_number');
			$doc['Case_Number']= $casenum;
			
			$accname =$action->getFieldValue('account_name');
			$doc['Account_Name']= $accname;
			
			$subject =$action->getFieldValue('name');
			$doc['Case_Subject']= $subject;
			
			//$actiondetail=$action->getFieldValue('description_c');
			//$doc['Details']= $actiondetail;
			
			$xtpl->insert_loop('block.action',array('DOC'=>$doc));
		}

I have added it like that to email and that seemed to work but what if the user remove that one of the items, how would I send an email for that

To be honest I don’t understand one thing here

Sorry, my bad

That answer was meant for another post!