How to use get_linked_beans and relationship

I have written the logic hook to send email notification but keeps giving this error message from the php file “Could not find Users within the department NC is issued to”, I don’t understand why it to saying that $dept_array is equivalent to zero(0). This is the name of the relationship (dept_department_nccas_nc_case)’; not sure if my syntax is incorrect or what the case maybe.

$dept_array= $bean->get_linked_beans(‘dept_department_nccas_nc_case’,‘dept_Department_sugar’);

if(count($dept_array)==0){
/log error/
$GLOBALS[‘log’]->fatal("Could not find the department NC was issued to: ". $bean->name);
}else{
/* Get email of users within the Department NC is issued to */
$user_array = $dept_array[0]->get_linked_beans(‘dept_department_users’,‘User’);
if(count($user_array)==0){
/log error/
$GLOBALS[‘log’]->fatal("Could not find Users within the department NC is issued to: ". $bean->name);
}else{
$address=getDepartmentUsersEmail($user_array);
}

/*Get the Department NC was raised by */
$raised_dept_array= $bean->get_linked_beans('dept_department_nccas_nc_case_1','dept_Department_sugar'); 
if(count($raised_dept_array)==0){       
    /*log error*/
    $GLOBALS['log']->fatal("Could not find the department NC was raised by: ". $bean->name);
}else {
    /*Get email of users within the Department NC is raised by*/
    $raised_user_array = $raised_dept_array[0]->get_linked_beans('dept_department_users','User');
    if(count($raised_user_array)==0){       
        /*log error*/
        $GLOBALS['log']->fatal("Could not find Users within the department NC is raised by: ". $bean->name);
    }
    else {
        $r_user_address = getDepartmentUsersEmail($raised_user_array);              
    }
}

Does the second cll produce any result?

I see that you are using two different relationships:
. dept_department_nccas_nc_case
. dept_department_nccas_nc_case_1

Are they supposed to be different?
Are those both the correct names of the relationships

Is the name of the module (dept_Department_sugar) correct?

Is the name of the bean object ($bean) correct?

Can you try to output to screen the $bean object (then exit; otherwise the flow will continue) to see its contents. I see that $bean->name appears to be empty in the message output.

dept_Department_sugar is the name of a php file with the fields and $bean->name is not empty; it printed out the subject of the nc case. Yes they are no different relationships

Have you had a look here:
https://docs.suitecrm.com/developer/working-with-beans/

Particularly at this portion:

I’m not understanding because the same relationship that I used that’s the link definition in cached vardefs that I used; but some how the count is still zero. Yes I did look at that forum

But in your code you seem to be using two different relationships:
. dept_department_nccas_nc_case
. dept_department_nccas_nc_case_1

I have removed one but it still produces the error "Could not find the department NC was issued to: ". $bean->name

If you create a relationship, and then delete it and then recreate it you may end up in trouble not finding the correct relationship that you have to use.

Please try your code first with dept_department_nccas_nc_case and check if it works. Then modify the cde and try with dept_department_nccas_nc_case_1 (the difference being the “_1” at the end)

Please check also your logs and report any errors

That didn’t work either. And the only error is this
Mon Apr 15 07:08:57 2019 [6960][e19f62c5-9f87-02bc-204f-5a57c41e1cd1][FATAL] Could not find the department NC was issued to: hjggggf
Mon Apr 15 07:08:57 2019 [6960][e19f62c5-9f87-02bc-204f-5a57c41e1cd1][FATAL] Could not find the department NC was issued to: hjggggf
" hjggggf" is the subject of the nc case that I entered and that’s the result showed suitecrm.log

Check also the webserver/PHP log

I’ll recommend to find the right relationship name. You can find a file containing it if you go to:

custom/Extension/modules/YourMODULE/Ext/Vardefs

You can find the name from From Studio as well

Now when you have it you can print the result into a file to check what the array is returning.


$file = 'custom/MyfileNAme.txt';
file_put_contents($file, 'This the returned content on the array: '.var_export(TheArrayYouWant, true).';\n?\>');

This is not the solution to your problem but at least will give you more inside on what is wrong.

Thanks,

AlxGr

Saying that the array is empty

Thanks everyone for your assistance but the relationship name was the tricky that caused the error; eberything is working ok now. Only issue is that email notifications are coming twice

I am glad to hear that you have managed to sort it out.

In my first post and also after I concentrated on the name of the relationship: