Skip to Content
Menu
This question has been flagged
4 Replies
10371 Views

HI, 


I am trying to create an automated action in order to automatically grant portal access to a new contact. 

As right now if you create a new contact from the POS they don't have access to the website so they have to create a new account and then contacts are duplicated.

Maybe there is better to do it than automated action?

thanks, 

Avatar
Discard
Best Answer

Hi Chris:

Using an automated action is the simplest and most elegant way of doing this.

EDIT:

Create an automated action on the Contact (res.partner) model with a Trigger Condition of On Creation and Action To Do of Execute Python Code

Put the following code in the Python Code field:

# Ensure an email address has been entered so that it can be used for the login
if not record.email:
    raise Warning("Please enter an email address.")

x_group_portal_user = env.ref('base.group_portal')
env['res.users'].create([{'name': record.name,
                          'login': record.email,
                          'partner_id': record.id,
                          'company_id': env.company.id, 
                          'groups_id': [(6, 0, [x_group_portal_user.id])],
                          }],)

Avatar
Discard
Author

thanks for your message. I have tried to create this automated action but it still does now work. I am not sure about the loving there and python code I should add ?

any chance u can help?

thanks

Hi Chris: I have edited my post and added the details.

Author

awesome, thanks a lot, it works perfectly. I just add to remove the 'company_id': env.company.id,

but all good.

Author

Actually I realized that with this script it also publish the contact on the website which I don't want to.

I want to grant them the access but don't want to publish the contact on the website if that make sense.

I had a look at my group and it looks like my grant port is id = 9 and not 6. I changed it to 9 but then it does now work.

I think this is not the same group, what am i missing here?

sorry, thanks a lot

Hi Chris:

The (6,0,...) part is the syntax for populating one2many/many2many fields. The "6" indicates that the existing groups assigned to this user need to be removed and replaced with the portal user group. If you need more groups to be assigned to the user, you will need to add their ref ids to the array of group ids on the right.

Here's a link to the documentation (you will have to scroll down a bit to the one2many.many2many part):

https://www.odoo.com/documentation/13.0/reference/orm.html#create-update

Regarding the question about the Contact getting published to the website, when I try this out in my environment, the Contact is not getting automatically published. There must be something else in your environment that is triggering off the publish.

You can try to get around this by adding the following line at the end of the code (after the create).

record['is_published'] = False

This will reset the published flag to False even if it gets set to True somewhere else.

NOTE: You may want to use this as a last resort only if you cannot find what is automatically triggering the publish.

Author

ok I see, this is really helpful thanks a lot. I will try to understand where is this coming from

thanks ,

hi can you add a condition, wherein portal access will be granted only if the contact has that information. otherwise, it will skip. example: a custom field "portal_user" is true before granting portal access upon creation? and does it work if odoo bot created the contact via website?

Best Answer

I'm using this ina  slightly different context - ie on sales order confirmation, so I need to check if theuser has already been granted portal access.

I've tried a few things, like:

if not record.partner_id.has_group('base.portal_users'):

Which doesn't work, because it's addressing the partner, not the user but I can't see how to get from record to the user group. How would you add a conditional to exclude existing portal users from this code? 

Avatar
Discard
Best Answer

Just do simplify,

In wizard just replace your apply button with following button

<button name="1174" type="action" string="Apply"/>

here name is the id of your server action. 

In server action you can add the code like,

for rec in records:

  for userin rec.user_ids:

    user['in_portal'] = True

 rec.action_apply()

Video : https://youtu.be/Mjr-RpITSC0

Avatar
Discard

Hi Muhammad,

I am new to Odoo dev. Looking at your video and in V15. Did you create a new server action or updated an existing one ?

Thanks you !

Diego

Best Answer

Paresh's solution works, and will automatically create portal access for all contacts, so you might want to add "Apply On": Customer Rank is set

 [["customer_rank","!=",False]]
One other suggestion.  If you create customers through the normal Odoo user interface (and not POS), there is an action to "add portal access" (you can do this for multiple customers from the list view or for individual customers using the Form View).


This image has an empty alt attribute; its file name is grant-portal-access-list-view.jpg

This image has an empty alt attribute; its file name is grant-portal-access.jpg
Avatar
Discard
Author

great thanks a lot for your help:) all good

Related Posts Replies Views Activity
12
Apr 24
13201
6
Mar 24
9774
2
Jul 19
5621
3
Aug 21
3572
3
Aug 19
9256