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
Vidya H 4Vidya H 4 

hii all......plz help me with this code

 If installed products and locations don't have preferred technicians, a check is
performed on the Primary Territory of Work order to get the technicians who
have the same territory mentioned in their Service Territory and assign that technician to preferred technician field in work order.

Primary Territory is field in work order (look up to territory)
Service Territory is field in Technician (look up to territory)
installed products and locations are look up fields in work order
preferred technician field in installed product and work order (look up to technician)

i have tried this 

helper class

public class WorkOrderHelper {

     public static void populateWorkOrderFields(list<SVMXC__Service_Order__c> WorkOrders){

         Set<Id> installedProductIds =new Set<Id>();
         Set<Id> locationIds =new Set<Id>();
         Set<Id> technicianIds =new Set<Id>();
         
         
        // retriving Installed product ids from work order
        for(SVMXC__Service_Order__c wo : WorkOrders) {
            installedProductIds.add(wo.SVMXC__Component__c);
            locationIds.add(wo.SVMXC__Site__c);
            technicianIds.add(wo.SVMXC__Preferred_Technician__c);
        }
        
        // getting all installed product with all required info 
        List<SVMXC__Installed_Product__c> listOfIP = [select id, SVMXC__Preferred_Technician__c
                                                         from  SVMXC__Installed_Product__c where ID in :installedProductIds];
        Map<Id,SVMXC__Installed_Product__c> mapProducts = new Map<Id,SVMXC__Installed_Product__c>();
         for(SVMXC__Installed_Product__c IP : listOfIP){
             mapProducts.put(IP.id,IP);
         }
                                                        
          System.debug('inside for loop');
         List<SVMXC__Site__c> listOFloc = [select id, location_technician1__c
                                                         from  SVMXC__Site__c where ID in :locationIds];

         Map<Id,SVMXC__Site__c> mapLocations = new Map<Id,SVMXC__Site__c>();
                 for(SVMXC__Site__c LOC : listOFloc){
             mapLocations.put(LOC.id,LOC);
         }                                        
         
         List<SVMXC__Service_Group_Members__c> listOfTechnicians = [select id, SVMXC__Service_Territory__c,SVMXC__Service_Group__c
                                                         from  SVMXC__Service_Group_Members__c where ID in :technicianIds];
         Map<Id,SVMXC__Service_Group_Members__c> mapTechnicians = new Map<Id,SVMXC__Service_Group_Members__c>();
                    
                 for(SVMXC__Service_Group_Members__c TECHNICIAN : listOfTechnicians){
             mapTechnicians.put(TECHNICIAN.id,TECHNICIAN);
         }                                                                        
        
         
        for(SVMXC__Service_Order__c wo : WorkOrders){
            
            SVMXC__Installed_Product__c prd = mapProducts.get(wo.SVMXC__Component__c);
            SVMXC__Site__c location = mapLocations.get(wo.SVMXC__Site__c);
            SVMXC__Service_Group_Members__c technician = mapTechnicians.get(wo.SVMXC__Preferred_Technician__c);
            System.debug('inside for loop');
            System.debug('product '+prd);
            System.debug('location '+location);
            System.debug('technician'+technician);
            if(prd != null && location != null && technician != null){
                System.debug('if cond 1');
                if(prd.SVMXC__Preferred_Technician__c == null){
                    System.debug('if cond 2');
                    if(wo.SVMXC__Primary_Territory__c == technician.SVMXC__Service_Territory__c){
                        System.debug('if cond 3');
                        wo.SVMXC__Service_Group__c = technician.SVMXC__Service_Group__c;
                        wo.SVMXC__Preferred_Technician__c = technician.Id;
                    }    
                }
                // compare primary territory with installed product Technician territory
            }
        }
     }
}

trigger

trigger PopulateTechnician on SVMXC__Service_Order__c (before insert,before update) {
    if(Trigger.isBefore){
        if(Trigger.isInsert || Trigger.isUpdate){
            WorkOrderHelper.populateWorkOrderFields(Trigger.New);
        }
    }
}
SubratSubrat (Salesforce Developers) 
Hello ,

It seems like you are attempting to populate the SVMXC_Preferred_Technicianc field in the SVMXCService_Orderc object based on the SVMXCPreferred_Technicianc field in the SVMXCInstalled_Productc object and the location_technician1c field in the SVMXCSite_c object.

Your code fetches the required data by querying SVMXC_Installed_Productc, SVMXCSitec, and SVMXCService_Group_Membersc objects and puts them in maps. Then, it loops through each SVMXCService_Orderc record in the trigger context and tries to populate the SVMXCPreferred_Technician_c field based on the provided conditions.

Overall, your approach looks reasonable, but there are some areas where you can improve your code:

Query optimization - You are querying all SVMXC_Installed_Product_c records that have the provided installedProductIds in a single query. While this approach works for small sets of records, it can quickly become inefficient for larger sets. Consider using a more selective query or breaking the query into smaller batches.

Duplicate records - If there are multiple SVMXC_Service_Orderc records in the trigger context with the same SVMXCComponentc or SVMXCSitec values, your code could potentially create duplicate records in the mapProducts and mapLocations maps. To avoid this, consider using a Map<Id, List<SVMXCInstalled_Productc>> and Map<Id, List<SVMXCSite_c>> instead and looping through the lists to find the corresponding records.

Logic for assigning the technician - Your code currently assigns the technician to the SVMXC_Preferred_Technicianc field in the SVMXCService_Orderc object if the technician's SVMXCService_Territoryc matches the SVMXCPrimary_Territory_c of the order.

However, this approach assumes that the preferred technician for the installed product or location is the same as the technician for the primary territory. Consider adding more specific conditions to ensure that the correct technician is assigned.

Code structure - Consider breaking your helper class into smaller, more specific methods. This can make your code more readable and easier to maintain.

I hope this helps!