How to include beans function in custom code

Hi to all, i have made a script cos i need to skedule a task. that assign the security group of a user to the project that are assigned to the account assigned to the user. in other word:
i have to check all the project, get the account id assigned to the project, get the assigned_user_id of the account, take the group id of the assigned_user_id and assign the group id to the project. This task has to be scheduled. I’ve put this task in custom/service/schedulers/script.php

I’ve made raw sql to get all, but now, i have the problem in the insert in securitygroups_records cause i can’t generate the “id” as unique. How could i make this?

Ids in SuiteCRM just need to be unique, they don’t have to follow that format like xxxx-xxxx-xxxxxxxx-xxxx-xxx. So if you just generate a large random number, it should work fine. There is a function to generate a proper UID, I don’t know what it’s called.

1 Like

Thanks! so i will use the default funcion of sqlserver, but for future use, there is a way to call bean in my custom file?

Exactly what problem are you getting calling the Bean?

Try something like

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

So actually here is my code:


<?php
include "db_connect.php";
$bean = BeanFactory::getBean('Cases');


$query_id = "SELECT account_id,project_id FROM projects_accounts";
$query_id = sqlsrv_query($connectionInfo_CRMSuite,$query_id);

while($row_id = sqlsrv_fetch_array($query_id,SQLSRV_FETCH_ASSOC)){
	$account_id = $row_id['account_id'];
	$project_id = $row_id['project_id'];
	$query_group_id = "SELECT securitygroups_users.securitygroup_id from securitygroups_users,accounts WHERE securitygroups_users.user_id=accounts.assigned_user_id 
	and accounts.id='$account_id'
	and securitygroups_users.deleted=0";
	$query_group_id = sqlsrv_query($connectionInfo_CRMSuite,$query_group_id);
	$group_id = sqlsrv_fetch_array($query_group_id,SQLSRV_FETCH_ASSOC);
	$security_group_id= $group_id['securitygroup_id'];
	if($security_group_id!=""){
		$chech_query = "SELECT id FROM securitygroups_records WHERE module='Project' 
			and securitygroup_id='$security_group_id'
			and record_id='$project_id'";
			$chech_query = sqlsrv_query($connectionInfo_CRMSuite,$chech_query);
			$chech_id = sqlsrv_fetch_array($chech_query,SQLSRV_FETCH_ASSOC);
			
			if(empty($chech_id)){
				$id = generateRandomString(8)."-".generateRandomString(4)."-".generateRandomString(4)."-".generateRandomString(12);
				echo $id."-----".$group_id['securitygroup_id']."-----".$project_id;
			echo "</br>";
			print_r($chech_id);
			}
			
			
			echo "</br>";
	}
	
}




function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

All works fine but i don’t want to use my function generateRandomString because could not be unique. So, i’ve tryed to include data/BeanFactory.php but it show me Not A Valid Entry Point.

Now i’ve added as you say $bean = BeanFactory::getBean(‘Cases’); but shows me:

Fatal error: Uncaught Error: Class ‘BeanFactory’ not found in C:\SITI\delbarbaCRM\custom\service\schedulers\sync_progect_users.php

So, what do i have to do to include beanfactory correctly?

Make sure your own file is defined as a Custom Entry point, I think after that everything will work without any extra includes.

https://docs.suitecrm.com/developer/entry-points/

Yea!! worked with the entry point!

So last question, how can i put the data in [securitygroups_records]? because isn’t a module. what i have to call?

Really? :slight_smile:

Don’t think in terms of database tables, think higher level, in terms of objects.

Here is an example of assigning a security group to a Contact bean called $person:


            // now set Security so accesses are granted to appropriate users:
            $securityTeam = new SecurityGroup();
            $securityTeam->retrieve_by_string_fields(array('name' => $theGroupName ));
            if ( $securityTeam->id == null)
                $GLOBALS['log']->fatal("Group with the name '" . array('name' => $theGroupName ) . "' wasn't found!");
            $person->load_relationship('SecurityGroups');
            $person->SecurityGroups->add($securityTeam->id);

Really? :slight_smile:

Thanks a lot, i will use an edit of this code cause i have a secgroup for every user, and i need to create relationship between project and the security group.
so i will try something.

thanks a lot

Really? :slight_smile:

I know, you starting to hate me but i have still some problem. With this code:

			$project = BeanFactory::getBean("Project",$project_id);
			$project->load_relationship('SecurityGroups');
			print_r($project);

i got:
Call to a member function load_relationship() on boolean
And i don’t get why, because if i print_r($project) before the load_relationship it show me the project.

Shouldn’t the GetBean use “Projects”, plural, instead of “Project”?

For my suite seems It wants Project instead of Projects. If i use Projects show me a error

So, seems i have to use function retrive to get te project and load the relationship. but now with this code:

if(empty($chech_id)){
			//$id = generateRandomString(8)."-".generateRandomString(4)."-".generateRandomString(4)."-".generateRandomString(12);

//			$securityTeam = new SecurityGroup();
//			$securityTeam->retrieve_by_string_fields(array('id' => $group_id['securitygroup_id'] ));
			//echo $project_id;
			$project = SugarModule::get("Project")->loadBean();
			$project->retrieve($bean->$project_id);
			//$project = BeanFactory::getBean("Projects",$project_id);
			$project->load_relationship('SecurityGroups');
			$project->SecurityGroups->add($group_id['securitygroup_id']);
			
			echo $group_id['securitygroup_id']."-----".$project_id;
			echo "</br>";
			echo "fatto";
		}

nothing happens. i mean it goes all well, no error shown, but in the securityGroup_records nothing change. And if i do $project->save() the script goes to create new empty project in the project table. I’m stuck

You commented out my code that retrieves the SecurityGroup object. So you don’t have that object, and when you try to “add” the relationship, you give it an id, you should be giving it the object fetched by that id.

Remember you can check the return of functions such as “add” to know if they were successful.

I don’t get a thing. Why i have to find the securitygroup object, and then pass securitygroup->id if i have already the id?

//echo $project_id;
			$project = SugarModule::get("Project")->loadBean();
			$project->retrieve($bean->$project_id);
			
			
			$securitygroup = SugarModule::get("SecurityGroups")->loadBean();
			$securitygroup->retrieve($group_id['securitygroup_id']);

			//$project = BeanFactory::getBean("Projects",$project_id);
			$project->load_relationship('SecurityGroups');
			if($project->SecurityGroups->add($securitygroup->id))
				echo "yes";
			else
				echo "no";
			//print_r($securitygroup->id);
			die();
			echo $group_id['securitygroup_id']."-----".$project_id;
			echo "</br>";

nothing changed. the output is “no”

It should work the same if your Id is correct - but if you try the retrieve you can make sure it is. You can test if there’s an object there.

I know my code works, at least for Contacts. If it isn’t working for you my advice is to start checking every step - so see what the functions are returning, make sure you have the correct objects, ids, etc.

Are you using XDEBUG to step through your code in a debugger?

You should :slight_smile:

I was mading a stupid mistake. now it works. thanks a lot