How to add a calls_users with code

Hi everyone,
To save time for my users, I added a date field on the Leads module, and in an after save hook that creates a new Call if a date is entered.
Everything works fine, except that the created Call does not appear in the calendar. After research, it is because an entry is missing in the “calls_users” table.
So, how do I properly populate the calls_users table when saving my call bean ?

//bean here is a Lead Object
$call = BeanFactory::newBean('Calls');
$call->name = "Rdv " . $bean->first_name . " " . $bean->last_name;
$call->date_start = $bean->mir_dateheurerdv_c;
$call->created_by = $current_user->id;
$call->assigned_user_id = $bean->assigned_user_id;
$call->duration_minutes = 0;
$call->duration_hours = 1;
$call->parent_type = "Leads";
$call->status = "attenteAppel";
$call->direction = "Outbound";
$call->parent_id = $bean->id;
$call->mir_leadsource_c = $bean->lead_source;
$call->save();

Thank you for your help :slight_smile:

Assuming it’s a relationship, you should be able to find commands to navigate that here

Hi,
Thank you for your answer, but i read this page so many times, i’ve tried without success. Maybe i didn’t have the right syntax , resulted to an error 500 with this code :

$call->load_relationship('calls_users'); 
$call->calls_users->add($assigned_user_id);

OR

 $call->load_relationship('users'); 
 $call->users->add($assigned_user_id);

I don’t know, then.

Two possible ways forward:

  • use a debugger to go into the load_relationship code and see what is happening in there

  • give up on the beans and use direct SQL, you’ll find examples online specifically for calls_users

Hi,

The name of the relationship is users, so, this should be;

$call->load_relationship('users');
$call->users->add($assigned_user_id);
1 Like

Hi,
Thank you for your answer but i’ve already try and gave me ERROR 500 …

After further research i ended up finding this other syntax that works well :

$relate_values = array('user_id'=>$call->assigned_user_id,'call_id'=>$call->id);
$data_values = array('accept_status'=>'none');
$call->set_relationship($call->rel_users_table, $relate_values, true, true, $data_values);

source : https://github.com/salesagility/SuiteCRM/pull/7543/files

it would be nice if this were present in the official documentation. Also, I did not find in the documentation, an official list of all accessible beans objects and their descriptions of fields and functions.

Thank you @blqt and @pgr for your help :slight_smile:

1 Like

If you had a 500 error, you should check your PHP error log.

I just noticed you’re giving an id to the Add function, but that is wrong, it is expecting a full bean.

aaaaaah yes obviously ! thank you i’ll retry the bean syntax :slight_smile: