Simple doubt and recommended practice

So let’s say we have an employee who works in sales, there will be a lot of data created by them.

What is the recommended process to be followed when the employee leaves the company. How are the leads opportunities etc transferred over to another employee/Manager?

Hi Shinzu,

Use the Sales Territory feature to give people access. This will make it easier from an administration perspective. Get the new Sales Person in, assign the same territory to her/him and the new arrival should be good to go. Or split the territory among your other sales people - this is slightly more complicated as you will have to change the territory of the Leads/Customers from the old territory to the territory that you want it to be assigned to.

Meaning let’s say you have three sales people A, B & C. A manages Territory X, B manages Y and C manages Z.

Now A is quitting and rather than get a new sales person, you want to split A’s Leads and Customers to B & C. Then you change the Territory of Leads and Customers from X to Y and from X to Z for the Leads and Customers that you want to reassign to B & C.

Hope this helps.

Thanks

Jay

1 Like

Hi Jay, To avoid confusion,
1.I spun up a new instance
2.created Test Employee A , Test Employee B and
3. allocated territories as Area 1, and Area 2 to them respectively.

Now from the admin account i allocated new leads LeadA and LeadB to them and set the territory accordingly. But this does nothing to give access to the leads to the respective territory managers.

Am I missing something?

There will be a few things you may need to do to make it work. First use User Permissions and allow Area 1 to A and Area 2 to B. Now check if A can only see Area 1 and B Area 2?

Let me know if you’ve done this and it still isn’t working and I will try on my instances and see if I missed something here.

Thanks

Jay

Tried this
went to user permission, selected user, selected territory, allocated the territory to them. But no use.

So went into role Permission Manager and the only for creator option was checked. Removed this and it worked without the unintended consequence of other leads being visible to the user.

Thanks Jay. Thinking of creating a custom button to assign leads to territories.

Ideally shouldn’t this be automatic, where when i assign a territory to an agent via CRM/Selling>Territory and the user permissions are automatically created.

Thanks a lot Jay. If you could just a take a look at the below.

To make things easy added the following server side code
Whenever a territory is assigned to a sales user , this automatically sets and removes permissions based on territory.

from __future__ import unicode_literals
import frappe
from frappe.permissions import add_user_permission, remove_user_permission, \
	set_user_permission_if_allowed, has_permission, get_doc_permissions

def remove_user_permissionsmain(ter):
    doc = frappe.get_doc('Territory', ter)
    upermdoc=frappe.get_doc('User Permission', {"allow": "Territory","for_value":doc.name})
    # tmanid=frappe.get_doc('Employee', {"employee_name": upermdoc.user})
    tmanid=upermdoc.user
    remove_user_permission("Territory", doc.name,tmanid)


def update_user_permissionsmain(ter,tmanager):
    doc = frappe.get_doc('Territory', ter)
    tmanid=frappe.get_doc('Employee', {"employee_name": tmanager})
    # if not doc.create_user_permission: return
    if not has_permission('User Permission', ptype='write', raise_exception=False): return
    employee_user_permission_exists = frappe.db.exists('User Permission', {
		'allow': 'Territory',
		'for_value': doc.name,
		'user': tmanid.user_id
	})
    if employee_user_permission_exists: return
    employee_user_permission_exists = frappe.db.exists('User Permission', {
		'allow': 'Territory',
		'for_value': doc.name,
		'user': tmanid.user_id
	})
    if employee_user_permission_exists: return
    add_user_permission("Territory", doc.name, tmanid.user_id)
	# set_user_permission_if_allowed("Company", self.company, self.user_id)
# add territory permission when a territory is assigned
@frappe.whitelist()
def update_user_permissions(ter,tmanager):
	# called via User hook
    # doc = frappe.get_doc('Territory', ter)
    # print(doc.territory_manager)
    if tmanager:
        # if not has_permission('User Permission', ptype='write', raise_exception=False): return
        # territory = frappe.get_doc("Territory", {"user_id": doc.name})
        # territory.update_user_permissions()
        update_user_permissionsmain(ter,tmanager)
@frappe.whitelist()
def remove_user_permissions(ter):

    remove_user_permissionsmain(ter)

And this client side code for Territory

`frappe.ui.form.on('Territory', {
after_save(frm) {
    if (frm.doc.territory_manager)
    {
frappe.call({
	method: 'custapplocation.crmimprovements.update_user_permissions',
	args: {
	ter: frm.doc.name,
    tmanager:frm.doc.territory_manager
	},
	callback: function(r) {
		frappe.msgprint(  
			"Territorry permission added to Territory Manager" );
	}
});
        
    }
     if (!frm.doc.territory_manager)
     {
         frappe.call({
	method: 'custapplocation.custom.crmimprovements.remove_user_permissions',
	args: {
	ter: frm.doc.name
	},
	callback: function(r) {

	}
});	     }
}
 });`
1 Like