Assign Record to Users automatically

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
gatenet
Veteran Member
Posts: 45
Joined: 2016-07-26 09:34
Location: Greece
Contact:

Assign Record to Users automatically

Post by gatenet » 2017-10-09 16:25

Hi there gyus, is there a way to assign new records to users automatically when the admin inputs them?

gatenet
Veteran Member
Posts: 45
Joined: 2016-07-26 09:34
Location: Greece
Contact:

Re: Assign Record to Users automatically

Post by gatenet » 2017-10-09 16:34

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!

gatenet
Veteran Member
Posts: 45
Joined: 2016-07-26 09:34
Location: Greece
Contact:

Re: Assign Record to Users automatically

Post by gatenet » 2017-10-10 16:20

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?

Abeer
Posts: 18
Joined: 2017-03-12 09:19

Re: Assign Record to Users automatically

Post by Abeer » 2017-10-17 08:34

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

Ionut Bocanet
Posts: 28
Joined: 2017-03-12 09:26
Contact:

Re: Assign Record to Users automatically

Post by Ionut Bocanet » 2019-10-01 11:19

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.
Best Regards,
Ionut Bocanet

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Assign Record to Users automatically

Post by jsetzer » 2019-10-01 11:34

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
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

Ionut Bocanet
Posts: 28
Joined: 2017-03-12 09:26
Contact:

Re: Assign Record to Users automatically

Post by Ionut Bocanet » 2019-10-01 11:55

worked like a charm. Thank you Jan
Best Regards,
Ionut Bocanet

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Assign Record to Users automatically

Post by jsetzer » 2019-10-01 11:59

Great! Good to hear!
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

jangle
Veteran Member
Posts: 89
Joined: 2020-01-18 17:41

Re: Assign Record to Users automatically

Post by jangle » 2020-02-05 04:49

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;

}

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Assign Record to Users automatically

Post by jsetzer » 2020-02-05 08:55

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
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

jangle
Veteran Member
Posts: 89
Joined: 2020-01-18 17:41

Re: Assign Record to Users automatically

Post by jangle » 2020-02-05 14:25

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;
}

jangle
Veteran Member
Posts: 89
Joined: 2020-01-18 17:41

Re: Assign Record to Users automatically

Post by jangle » 2020-02-05 14:35

Would the data type matter?

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Assign Record to Users automatically

Post by jsetzer » 2020-02-05 14:45

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
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

jangle
Veteran Member
Posts: 89
Joined: 2020-01-18 17:41

Re: Assign Record to Users automatically

Post by jangle » 2020-02-05 15:19

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;
}

/**

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Assign Record to Users automatically

Post by jsetzer » 2020-02-05 16:04

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.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

jangle
Veteran Member
Posts: 89
Joined: 2020-01-18 17:41

Re: Assign Record to Users automatically

Post by jangle » 2020-02-05 16:45

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;
}

jangle
Veteran Member
Posts: 89
Joined: 2020-01-18 17:41

Re: Assign Record to Users automatically

Post by jangle » 2020-02-05 18:46

I GOT it to work!!!

I'm shocked...

Thank you so much Jan!!

User avatar
zibrahim
Veteran Member
Posts: 137
Joined: 2020-01-28 18:30
Location: Malaysia

Re: Assign Record to Users automatically

Post by zibrahim » 2020-03-08 10:48

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.
Zala.
Appgini 24.10.1579, MacOS 14.3.1 Windows 11 on Parallels.

jangle
Veteran Member
Posts: 89
Joined: 2020-01-18 17:41

Re: Assign Record to Users automatically - Revisit

Post by jangle » 2020-07-10 15:25

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

jangle
Veteran Member
Posts: 89
Joined: 2020-01-18 17:41

Re: Assign Record to Users automatically

Post by jangle » 2020-07-10 16:12

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

jangle
Veteran Member
Posts: 89
Joined: 2020-01-18 17:41

Re: Assign Record to Users automatically

Post by jangle » 2020-07-10 18:38

Please disregard

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

Thanks
Jim

Post Reply