logo

Reassigning all tasks from one user to another

Sometimes you may need to reassign all the tasks from one user to another, for example, when dismissing an employee. This article provides an example of a script which reassigns multiple tasks. Before executing the script, you need to unlock the account of the retired employee.

Script with PublicAPI

Note
Documentation on PublicAPI is available here
Attention
The script below is relevant for ELMA up to 3.12.1 inclusive.

Namespace:

using EleWise.ELMA.API;

Script text:

var tasks = PublicAPI.Portal.TaskBase.Filter().Executor(context.User).Find(); //create the list of user tasks 
foreach (var task in tasks)
{
    task.Executor = context.Usernew; // assign the tasks to the new user 
}

Script without PublicAPI

Namespaces:

using EleWise.ELMA.Model.Services;
using EleWise.ELMA.Tasks.Models;
using EleWise.ELMA.Tasks.Managers;

Script text:

var zdch=InterfaceActivator.Create<TaskBaseFilter>(); //create a task filter 
zdch.Executor=context.User; //find the user's task 
var spzdch=TaskBaseManager.Instance.Find(zdch,null); //create a list of user tasks 
spzdch.ForEach(odzdch=>odzdch.Executor=context.Usernew);// assign the tasks to the new user 

One execution of this script is enough. Either in the emulation mode without rollback or in a business process.

This way you can reassign only user tasks created in the web part. For this, use TaskFilter and TaskManager, or for leads use LeadFilter and LeadManager.