Page 1 of 1

Assign Record to Users automatically

Posted: 2017-10-09 16:25
by gatenet
Hi there gyus, is there a way to assign new records to users automatically when the admin inputs them?

Re: Assign Record to Users automatically

Posted: 2017-10-09 16:34
by gatenet
I have an idea how to do this, the senario is that we have a table (customers) with a lookup field with the name of the user (from table users), so in the users table that we have made we just put a field and put in the username of the user that we create in admin area. So if the admin puts in a new record in the table customers and chooses the user then automatically this will take the username from the users table and assign it to the right user. I dont know how to write the code for that, Please Help!

Re: Assign Record to Users automatically

Posted: 2017-10-10 16:20
by gatenet
So i have already proceed with that and what i did was that i created in appgini 2 tables, one named partners and one customers, in the table partners i puted a field username, then in customers, i have 2 fields, one is partner and its lookup field from table partners and has the name of the partner and the second is username that is also lookup from partners and autofill this field i choosed to be hide from table view and detail view also. Then in the hooks file in the customers.php i put this code after insert and after update
if($UserName){
$query = "UPDATE membership_userrecords SET memberID='$UserName' WHERE pkValue=$id AND tableName='customers'";
sql($query, $o);
}
That dosent seem to be working any suggetios?

Re: Assign Record to Users automatically

Posted: 2017-10-17 08:34
by Abeer
Hi gatenet,

I think all you need is to set ownership of the new created record tho the chosen user, you can do this through adding a code like this in customers_after_insert hook in customers.php file:

Code: Select all


$today = time();

/* make the user is the owner of his customer record to */
		sql("insert into membership_userrecords set tableName='customers',
									 	   pkValue='{$data['selectedID']}',
										   memberID='{$UserName}',
										   dateAdded='{$today}',
										   dateUpdated='{$today}',
										   groupID='{$ID_of_user's_group}'", $eo);
										   
										   return FALSE;
you should also return false after setting the ownership, to prevent the

Re: Assign Record to Users automatically

Posted: 2019-10-01 11:19
by Ionut Bocanet
Did somebody succeeded to change the owner of a record by using after_insert hook.
I tried to use this code and it is not working.

Based on a a drop down field, after update, i want to have the owner changed to the value of that drop down.

Re: Assign Record to Users automatically

Posted: 2019-10-01 11:34
by jsetzer
Hi,

try calling the built-in set_record_owner()-function instead and return FALSE afterwards. Otherwise AppGini will overwrite your ownership-definition by current user.

Code: Select all

set_record_owner("customers", $data["selectedID"], $UserName);
return FALSE;
Hope this helps!
Regards,
Jan

Re: Assign Record to Users automatically

Posted: 2019-10-01 11:55
by Ionut Bocanet
worked like a charm. Thank you Jan

Re: Assign Record to Users automatically

Posted: 2019-10-01 11:59
by jsetzer
Great! Good to hear!

Re: Assign Record to Users automatically

Posted: 2020-02-05 04:49
by jangle
I need to change ownership on insert as described above, but I cannot get the function to work. This is what I have.

assignments is the table, and UserName is based on an autofill from a selection on a dropdown.

What am I missing....

Thanks in advance

Jim

function assignments_after_insert($data, $memberInfo, &$args) {

set_record_owner("assignments", $data["selectedID"], $UserName);
return FALSE;

}

Re: Assign Record to Users automatically

Posted: 2020-02-05 08:55
by jsetzer
Hi,

what is the value of $UserName? It is not declared in the code you have posted.

Try this:

Code: Select all

function assignments_after_insert($data, $memberInfo, &$args) {
    set_record_owner('assignments', $data['selectedID'], $memberInfo['username']);
    return FALSE;
}
Best,
Jan

Re: Assign Record to Users automatically

Posted: 2020-02-05 14:25
by jangle
Thanks a lot Jan.

Still does not work....function assignments_after_insert($data, $memberInfo, &$args) {

set_record_owner('assignments', $data['selectedID'], $memberInfo['username']);
return FALSE;
}

Re: Assign Record to Users automatically

Posted: 2020-02-05 14:35
by jangle
Would the data type matter?

Re: Assign Record to Users automatically

Posted: 2020-02-05 14:45
by jsetzer
Please give us a little more information:
Still does not work
What exactly does not work? Insert? ownership?
Would the data type matter?
Datatype of what?

Recommended next steps:
  1. Please ensure that $data['selectedID'] is populated correctly. I'm afraid there may be no selectedID array key.
    To find out: Dump that variable for example using var_dump($_data['selectedID']); exit();
  2. Try with $data['YOUR_PRIMARY_KEY_COLUMN_NAME'], for example $data['id'] or $data['ID'] (according to you model)
  3. Check exact spelling (including letter case!) of your array keys
If this still does not help: Please post your code.

Regards,
Jan

Re: Assign Record to Users automatically

Posted: 2020-02-05 15:19
by jangle
Thanks for your help!!!

What exactly does not work? Insert? ownership? - The ownership does not change.
Datatype of what? UserName is of integer type as it is a lookup field would that matter?

Where would this code be placed? ump that variable for example using var_dump($_data['selectedID']); exit();
Tried this, $data['YOUR_PRIMARY_KEY_COLUMN_NAME'], for example $data['id'] or $data['ID'] (according to you model)
Check exact spelling (including letter case!) of your array keys

still no change in user name????

/**
* Called after executing the insert query (but before executing the ownership insert query).
*
* @param $data
* An associative array where the keys are field names and the values are the field data values that were inserted into the new record.
* For this table, the array items are:
* $data['ProjectId'], $data['Project_ID'], $data['Project_Manager'], $data['Project_Type'], $data['ProjectDuration'], $data['Task'], $data['ResourceId'], $data['Commitment'], $data['StartDate'], $data['EndDate'], $data['Writing'], $data['Total_Hours'], $data['UserName']
* Also includes the item $data['selectedID'] which stores the value of the primary key for the new record.
*
* @param $memberInfo
* An array containing logged member's info.
* @see https://bigprof.com/appgini/help/workin ... memberInfo
*
* @param $args
* An empty array that is passed by reference. It's currently not used but is reserved for future uses.
*
* @return
* A boolean TRUE to perform the ownership insert operation or FALSE to cancel it.
* Warning: if a FALSE is returned, the new record will have no ownership info.
*/

function assignments_after_insert($data, $memberInfo, &$args) {


set_record_owner('assignments', $data['Id'], $memberInfo['UserName']);
return FALSE;
}

/**

Re: Assign Record to Users automatically

Posted: 2020-02-05 16:04
by jsetzer
Hi,

if I got you right, you have a column named "UserName" which is a lookup (to a different table).

You must not use a (non declared) variable $UserName (nor UserName as you have written).

I now assume this is a column of your model. You can get the value of that field from the $data array like this:

Code: Select all

$username_id = $data['UserName'];
Attention:
array-key is case-sensitive! So please double check the spelling with capital "U" and capital "N".


Then you will have to fetch the memberID (which is as string) based on the seleted lookup-value (which is an integer) from your additional table like this:

Code: Select all

$memberID = sqlValue("SELECT `my_memberId_column_name` FROM `my_additional_table` WHERE `my_pk_column_name`='{$username_id}' LIMIT 1");
As soon as you have $memberID (=string) you can pass it into set_record_owner function as last parameter.

Hope this helps. I'm sorry, I'm confused and don't know your model in details. Sorry if I cannot help you.

PS:
Ensure all users available in your dropdown have a matching "real" member in AppGini's membership_users table. Also, you will need a way to deny deleting or banning or un-approving users as long as they have tasks assigned to. Otherwise only admin will be able to re-assign them.

Re: Assign Record to Users automatically

Posted: 2020-02-05 16:45
by jangle
Thanks very much again..... I will work on this....

Where does all of the code you have provided go? After the function, but before the set record yes?

function assignments_after_insert($data, $memberInfo, &$args) {

HERE??

set_record_owner('assignments', $data['Id'], $memberInfo['UserName']);
return FALSE;
}

Re: Assign Record to Users automatically

Posted: 2020-02-05 18:46
by jangle
I GOT it to work!!!

I'm shocked...

Thank you so much Jan!!

Re: Assign Record to Users automatically

Posted: 2020-03-08 10:48
by zibrahim
Hi Jangle,
I am glad that the codes working good with your project.
As I am not good with codes, do you mind sharing with me the codes and where to place them as I am having the same problem and situation.
Thanking you in advance.

Zala.

Re: Assign Record to Users automatically - Revisit

Posted: 2020-07-10 15:25
by jangle
Hello, I had this working great in one of my applications. I am trying to add to a different applcation now and i get the error that the record cannot be inserted.

Anybody see anything wrong?

Code: Select all

$username_id = $data['User'];
		$emailname_id = $data['Email'];
		$memberID = sqlValue("SELECT `User` FROM `Member_Information` WHERE `Id`='{$username_id}' LIMIT 1");
		$emailname_id = sqlValue("SELECT `Email` FROM `Member_Information` WHERE `Id`='{$emailname_id}' LIMIT 1");
		
	 
		sendmail(array(
			'to' => $emailname_id,
			'name' => '',
			 
			'subject' => 'PHFR Overtime Report',
			 
			'message' => $mail_body
		));
		 
	set_record_owner('Overtime_Submission', $data['Id'], $memberID);
	
		return FALSE;
		
	}
Thanks

Jim

Re: Assign Record to Users automatically

Posted: 2020-07-10 16:12
by jangle
Note, the email part works great.... if I use return true.

When I return false I get the error that the records cannot be inserted.

Thanks
Jim

Re: Assign Record to Users automatically

Posted: 2020-07-10 18:38
by jangle
Please disregard

I had placed the code in the before_insert function instead of the the after_insert.

Thanks
Jim