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

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())
            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){
                } else {
                    List<String> listString = new List<String>();
                    duplicateTaskTemplateCountMap.put(woTasks.SVMX_PS_Installed_Product__c, listString);

            if(duplicateTaskTemplateCountMap != null){
            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);
                    templateIdSet = new Set<Id>(templateList);
                    SVMXC__Installed_Product__c instProd = new SVMXC__Installed_Product__c();
                    instProd.Id = ipId;
                    instProd.Duplications_Work_Order_Tasks__c = templateIdSet.size();

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

But I'm stuck here..