function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kondru sampreethkondru sampreeth 

when lead status changed to"closed-but not converted" then task should be assign to a manager

when lead status changed to"closed-but not converted" then task should be assign to a manager , 
please write a triiger how it will do?
 
KaranrajKaranraj
If you want to update the old task owner when the lead status is changed to 'closed-but not converted' then you can to do that using apex trigger.

1. Create a after update trigger on Lead object
2. Then check lead status is 'closed-but not converted' and old value of status is not 'closed-but not converted'
3. Then store all the lead id in list varibale
4. Using that list variable query all the task records in the system (non closed task)
5. Then then update all task owner into Manager user [ You need to query the user record and check the manager as per your logic and assign into owner id of task]

To get start with apex code, check the Salesforce trailhead module https://developer.salesforce.com/trailhead/trail/force_com_programmatic_beginner
 
Admin User 853Admin User 853
If you want to task should be assign to a manager,I am providing apex trigger. I wrote apex trigger for after insert only:-

trigger triggerForLeadStatuswithTask on Lead (After Insert) {
    Set<Id>objSet = new Set<Id> ();
    for(Lead objLead : Trigger.New) {
        if(objLead.Status == 'Closed' && objLead.IsConverted == False) {
            objSet.add(objLead.Id);  
        }      
    }
    objSet.remove(null);
    List<Task>lstTask = new List<Task> ();
    if(!objSet.isEmpty()) {
        for(Lead itrLead : [SELECT Id,Name FROM Lead WHERE Id IN : objSet LIMIT 10000]) {
            for(User objUser : [SELECT Id,UserRoleId FROM User WHERE UserRole.Name='Manager' LIMIT 10000]) {
                Task objTask = new Task ();
                objTask.WhoId = itrLead.Id;
                objTask.OwnerId = objUser.Id;   
                objTask.Subject = 'Other';
                lstTask.add(objTask);
            }
        }
    }
    if(!lstTask.isEmpty ()) {
        insert lstTask;    
    }
}

If you are stisfied with this, please comment.

Thanks