Add custom action button in module to create a case

We have custom module and we want to add a functionality where users could go to ACTIONS dropdown and select option ā€˜Create a Case’,

image

then it should create a case record by pulling data from this module.

How to do this in SuiteCRM 7.13?

You can add the ā€œCreate a Caseā€ option to the ACTIONS dropdown using JavaScript by injecting a custom menu item and passing the current record ID.

When the user clicks this action:

  • Trigger a custom AJAX call.
  • In your backend handler, retrieve the current record data using the ID.
  • Map the required fields to the Case module.
  • Programmatically create and save a new Case record (Cases bean).

This approach gives you full control over how data is transferred and how the case is created.

Do you think this will work?

Add a new action button inside the buttons array

custom/modules/< YourModule >/metadata/detailviewdefs.php

array (
    'customCode' => '<input type="button" class="button" value="Create a Case" onclick="createCase();" />',
),


Create a new JavaScript file

custom/modules/< YourModule >/js/create_case.js

function createCase() {
    var recordId = document.getElementsByName('record')[0].value;
    if (!recordId) {
        alert("Record ID not found.");
        return;
    }

    var url = "index.php?entryPoint=createCaseFromModule&record=" + recordId;
    window.location.href = url;
}

Link this script in detailviewdefs.php

'includes' => array (
    array ('file' => 'custom/modules/<YourModule>/js/create_case.js'),
),

Register an entry point

custom/Extension/application/Ext/EntryPointRegistry/create_case.php

$entry_point_registry['createCaseFromModule'] = array(
    'file' => 'custom/modules/<YourModule>/create_case.php',
    'auth' => true
);

Add new code file:

custom/modules/< YourModule >/create_case.php

<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

global $db, $current_user;
require_once('modules/Cases/Case.php');

$recordId = $_GET['record'];
if (empty($recordId)) {
    die('No record ID provided');
}

// Fetch data from the custom module
$query = "SELECT name, description FROM <your_custom_module_table> WHERE id = '$recordId' AND deleted = 0";
$result = $db->query($query);
$row = $db->fetchByAssoc($result);

if (!$row) {
    die('Record not found.');
}

// Create a new Case record
$case = new aCase();
$case->name = "Case for: " . $row['name'];
$case->description = $row['description'];
$case->assigned_user_id = $current_user->id;
$case->save();

// Redirect to the new Case record
header("Location: index.php?module=Cases&action=DetailView&record=" . $case->id);
exit();
?>

Run Admin > Quick Repair and Rebuild

1 Like

I usually add the button using JS by appending an <li> element following SuiteCRM’s structure, but yes, using custom button code should work as well.

For creating a Case, I prefer using BeanFactory instead of the Case class directly.

I’d recommend running the code and testing it to ensure everything works as expected.

1 Like

I have update code for create_case.php and it worked. :partying_face:

custom/modules/<YOUR_MODULE>/create_case.php

<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

global $db, $current_user;
require_once('data/BeanFactory.php');

$recordId = $_GET['record'];
if (empty($recordId)) {
    die('No record ID provided');
}

// Fetch data from the custom module
$query = "SELECT name, description FROM <YOUR_TABLE_NAME> WHERE id = '$recordId' AND deleted = 0";
$result = $db->query($query);
$row = $db->fetchByAssoc($result);

if (!$row) {
    die('Record not found.');
}

// Create a new Case using BeanFactory
$case = BeanFactory::newBean('Cases');
$case->name = $row['name'];
$case->description = $row['description'];
$case->assigned_user_id = $current_user->id;
$case->save();

// Redirect to the new Case record
header("Location: index.php?module=Cases&action=DetailView&record=" . $case->id);
exit();

Follow-up question,

how to get relate fields data like account & opportunity and store in the case?

@rsp , You can use load_relationship to get the related account or any other modules data.

I tried it out!

//Load the relationship
$accountBean->load_relationship('contacts');
//Can now call methods on the relationship object:
$contactIds = $accountBean->contacts->get();

But I am getting the below error.

PHP Fatal error: Uncaught Error: Call to a member function load_relationship() on null


How to find relationship name between custom module and Accounts? :thinking: :thinking: :thinking:


This is confusing :face_with_peeking_eye:

Before accessing a relationship you must use the load_relationship call to ensure it is available. This call takes the link name of the relationship (not the name of the relationship).

You can get the relationship name from studio > modulename > relationship
Also you have to define $accountBean and then use load_relationship

Weird! :upside_down_face: I am still getting an 500 error when I write the below code in the file.

$bean = BeanFactory::getBean('Accounts');

$accountBean->load_relationship('accounts_custom_module_1');

Your code is still incorrect.

Try this

$accountBean = BeanFactory::getBean('Accounts', $accountId); // Make sure you pass the Account ID

if ($accountBean && $accountBean->load_relationship('accounts_custom_module_1')) {
    $relatedBeans = $accountBean->accounts_custom_module_1->getBeans();
    
    foreach ($relatedBeans as $relatedBean) {
        // Process each related bean
    }
}

Try Contacts or Accounts for the relationship,
While using the API was going insane just to figure out the first letter must be Uppercase for Default Modules, to use Custom Module Relationships needed to be the full Relationship Name

Does it pull multiples beans? How to check these beans and their values?

I am trying it with Accounts now. I did not try with Contacts.

So, I have Accounts relate field in both Cases & customModule modules. I just want to carry forward this account id to Cases from customModule.

Next step is to carry forward opportunity id.

Hi @rsp , I have similar kind of requirement to create cases from my test receipts module. I added ā€˜Create Case’ action on the detail page of my custom module. To link the account from the custom module with the created case, the challenge would be finding the account_id field name. I found the field from the database table. Here are the steps

  1. Create Action in the custom/modules/RCP_Receipts/metadata/detailviewdefs.php added as last array element with ā€˜customCode’ property.

0 => 'EDIT',
          1 => 'DUPLICATE',
          2 => 'DELETE',
          3 => 'FIND_DUPLICATES',
		  4 => array (
    'customCode' => '<input type="button" class="button" value="Create a Case" onclick="window.location=\'index.php?module=RCP_Receipts&action=create_case&record={$fields.id.value}\'"/>',
),
        ),
  1. Add action in the controller file at custom/modules/RCP_Receipts/controller.php
<?php
 require_once('include/MVC/Controller/SugarController.php');
class RCP_ReceiptsController  extends SugarController
{
	public function action_create_case(){
		$module_id=urldecode($_REQUEST['record']);
		$bean = BeanFactory::getBean('RCP_Receipts', $module_id);
		
		$case = BeanFactory::newBean('Cases');
		$case->name = "Case for: " . $bean->name;
		$case_id=create_guid();
		$case->assigned_user_id = $current_user->id;
		$case->id=$case_id;
		$case->new_with_id = true;
		//getting account_id
		$account_id=$bean->accounts_rcp_receipts_1accounts_ida;;
		$case->account_id=$account_id;
		$accountBean = BeanFactory::getBean('Accounts',$account_id);
	
		$case->description = 'Test description for a case for account'.$accountBean->name;
		$case->save();

		SugarApplication::Redirect('index.php?module=RCP_Receipts&action=DetailView&record='.$module_id);
	}
}

Please change module name and account_id field as per your custom module fields.

If you have access to check sql queries. The following query may help you find your custom module and account table linking table name. You can get the account_id field name from there to replace in the above code. Hope this solves your problem.

show tables like '%rcp_receipts%';

1 Like