+ Start a Discussion
Cris9931Cris9931 

Count duplications Trigger

Hello, I need some help to write the logic to count dupplication on 2 objects. I will show the relationships between the objects:

User-added image

On Installed Product object I have the field: Duplicate Work Order TaskOn this field I need to update with the number if I have a duplicated Work Order Task. Now, what a duplicated Work Order Task means? It means if a Work Order Task has the same Task Template and Work Order it means it is duplicated, like so:

User-added image

How can I do this? I managed to write the code(and it works) when I have a duplicated Task Template:

 

public static void updateTaskAvailabilitiesDuplication(Map<Id, SVMX_PS_Work_Order_Task__c> newMap) 
     {

        Map<Id, List<id>> duplicateTaskTemplateCountMap = new Map<Id, List<id>>();
        Map<Id, List<id>> duplicateWorkOrderCountMap = new Map<Id, List<id>>();
        
        List<SVMXC__Installed_Product__c> instProductToUpdate = new List<SVMXC__Installed_Product__c>();
        Set<Id> IpIds = new Set<Id>();

        if(newMap != null && !newMap.isEmpty()){

            for(SVMX_PS_Work_Order_Task__c woTask : newMap.values())
            {
                IpIds.add(woTask.SVMX_PS_Installed_Product__c);
                
            }
 
            for(SVMX_PS_Work_Order_Task__c woTasks: [Select SVMX_PS_SIG_Work_Order__c, SVMX_PS_Task_Template_Id__c, SVMX_PS_Installed_Product__c from SVMX_PS_Work_Order_Task__c where SVMX_PS_Installed_Product__c =: IPIds])
            {
                if(duplicateTaskTemplateCountMap.get(woTasks.SVMX_PS_Installed_Product__c) != null){
                    duplicateTaskTemplateCountMap.get(woTasks.SVMX_PS_Installed_Product__c).add(woTasks.SVMX_PS_Task_Template_Id__c);
                } else {
                    List<String> listString = new List<String>();
                    listString.add(woTasks.SVMX_PS_Task_Template_Id__c);
                    duplicateTaskTemplateCountMap.put(woTasks.SVMX_PS_Installed_Product__c, listString);

                }
            }
            if(duplicateTaskTemplateCountMap != null){
                 system.debug('::::duplicateTaskTemplateCountMap:::'+JSON.serialize(duplicateTaskTemplateCountMap));
            }
           
            for(Id ipId : duplicateTaskTemplateCountMap.keySet()) {         
                    Set<Id> templateIdSet = new Set<Id>(duplicateTaskTemplateCountMap.get(ipId));
                    
                    List<Id> templateList = new List<Id>();
                    for(Id templateId : templateIdSet) {
                        templateList = duplicateTaskTemplateCountMap.get(ipId);
                        templateList.remove(templateList.indexOf(templateId));
                    }
                    templateIdSet = new Set<Id>(templateList);
                    system.debug('::::templateList.size():::'+templateIdSet.size());
                    
                    SVMXC__Installed_Product__c instProd = new SVMXC__Installed_Product__c();
                    instProd.Id = ipId;
                    instProd.Duplications_Work_Order_Tasks__c = templateIdSet.size();
                    instProductToUpdate.add(instProd);   
            }

            if(instProductToUpdate.size() > 0){
                update instProductToUpdate;
            }
            
        }
 

But I'm stuck here..