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
Lokanath Gowd EdigaLokanath Gowd Ediga 

Multiple Tasks are created. Need only one

Hello all,

I have a scenario,where Business Development Manager(BDM) will conduct promotions on company's products. The members attened for that promotions are added as Promotion members(PMM)
Promotion is Master object
Promotion member is child.

Call centre guys verifies each promotion by calling PMM's on their Mobile No's.
If verification status(this is a picklist with Suggestion,Complaint,Others) is Complaint, then a task has to be created for concerned BDM and a task for his Manager.

I have this functionality built already, but the problem is, the tasks are getting created two times instead of one.
that is it is creating 4 tasks instead of 2(one for BDm , one for Manager)

i am posting my code here

Class:

public class PromotionmemberTaskHandler{
  
    Public static void createPromotionTasks(Map<Id,Promotion_Member__c> mapTriggerNew,Map<Id,Promotion_Member__c> mapTriggerOld){
        Map<id,Id> mapUserIdVsMgrId = new Map<id,Id>();
        List<Task> lstTask = new List<Task>();
         
        for(user u :[Select id,email,ManagersId__c from User where isActive = true and ManagersId__c <> null]){
            mapUserIdVsMgrId.put(u.id, u.ManagersId__c);  
        }
        
        for(Promotion_Member__c p: mapTriggerNew.Values()){
            if(p.Verified__c = true && p.Verification_Status__c == 'Complaint' && p.Verification_Status__c <> mapTriggerOld.get(p.Id).Verification_Status__c){
                if(mapUserIdVsMgrId.containsKey(p.CreatedById)){
                 Task t = new task();
                  t.ownerid = p.CreatedById;
                 t.Call_Center_Comments__c = p.Call_Center_Remarks__c;
                        t.WhatId = p.id;
                        t.Suggestion__c = p.Suggestion__c;
                        t.Complaint__c = p.Complaints__c;
                        t.ActivityDate = system.today();
                        t.Priority = 'Normal';
                        t.Subject = 'PromotionMember Complaint';
                      
                       insert t;
                       Task t1 = new task();
                 t1.Call_Center_Comments__c = p.Call_Center_Remarks__c;
                  t1.ownerid = mapUserIdVsMgrId.get(p.CreatedById);
                        t1.WhatId = p.id;
                        t1.Suggestion__c = p.Suggestion__c;
                        t1.Complaint__c = p.Complaints__c;
                        t1.ActivityDate = system.today();
                        t1.Priority = 'Normal';
                        t1.Subject = 'PromotionMember Complaint';
                      
                       insert t1;
              
                }
            }
        }
     
    }
}


Trigger:

Trigger TaskCreatepromotionmember on Promotion_Member__c (before update) {  
  if(Trigger.isUpdate && Trigger.isBefore){
      PromotionmemberTaskHandler.createPromotionTasks(trigger.newMap,trigger.oldMap);   
    }                                     
}


I am unable to traceout what the error in this code, so im posting here for your help

Thanks in advance.
Elad Kaplan 3Elad Kaplan 3
Your'e iterating over User's managers, If a manager has more than 1 employee, you will get multiple tasks..
kiranmutturukiranmutturu
Basic issue first.. Not to issue any DML inside the loop 

Change the code some thing like this

public class PromotionmemberTaskHandler{
  
    Public static void createPromotionTasks(Map<Id,Promotion_Member__c> mapTriggerNew,Map<Id,Promotion_Member__c> mapTriggerOld)
	{
        Map<id,Id> mapUserIdVsMgrId = new Map<id,Id>();
        List<Task> lstTask = new List<Task>();
         
        for(user u :[Select id,email,ManagersId__c from User where isActive = true and ManagersId__c <> null]){
            mapUserIdVsMgrId.put(u.id, u.ManagersId__c);  
        }
        
        for(Promotion_Member__c p: mapTriggerNew.Values())
		{
            if(p.Verified__c = true && p.Verification_Status__c == 'Complaint' && p.Verification_Status__c <> mapTriggerOld.get(p.Id).Verification_Status__c)
			{
                if(mapUserIdVsMgrId.containsKey(p.CreatedById))
				{
                 Task t = new task();
                  t.ownerid = p.CreatedById;
                 t.Call_Center_Comments__c = p.Call_Center_Remarks__c;
                        t.WhatId = p.id;
                        t.Suggestion__c = p.Suggestion__c;
                        t.Complaint__c = p.Complaints__c;
                        t.ActivityDate = system.today();
                        t.Priority = 'Normal';
                        t.Subject = 'PromotionMember Complaint';
                      lstTask.add(t);
                      
					  Task t1 = new task();
                 t1.Call_Center_Comments__c = p.Call_Center_Remarks__c;
                  t1.ownerid = mapUserIdVsMgrId.get(p.CreatedById);
                        t1.WhatId = p.id;
                        t1.Suggestion__c = p.Suggestion__c;
                        t1.Complaint__c = p.Complaints__c;
                        t1.ActivityDate = system.today();
                        t1.Priority = 'Normal';
                        t1.Subject = 'PromotionMember Complaint';
                      lstTask.add(t1);
                       
              
                }
            }
        }
		
		insert lstTask;
     
    }
}

Also in the if condition better to check if the mapTriggerOld.get(p.Id).Verification_Status__c != 'Complaint'  than p.Verification_Status__c <> mapTriggerOld.get(p.Id).Verification_Status__c as  a check....


Lokanath Gowd EdigaLokanath Gowd Ediga
The code which i've mentioned earlier and code modified by kiran is working fine in sandbox. when it comes to production, it is again having same problem. Creating two times.

any pointers?