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.
<?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?
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);
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.
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.
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.
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?