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
Karthik Sundara RajKarthik Sundara Raj 

What is the right solution for following the Trigger?

Hi All,
What is the right solution for following the Trigger?
trigger TestSLTV on Service_Work_Order__c (after insert, after update)
{
    
    if (Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate))
    {
        Set<String> setStrVINNumber = new Set<String>();
        Set<String> setStrVINNumber1 = new Set<String>();
         Set<String> setStrVINNumber2 = new Set<String>();
        // Set<Date> setStrVINNumber3 = new Set<Date>();
        Map<String, Decimal> mapStrVINNumToTotal = new Map<String, Decimal>();
        List<Vehicle__c> lstVehicleToUpdate = new List<Vehicle__c>();
        List<Vehicle_Ownership__c> lstAVOToUpdate = new List<Vehicle_Ownership__c>();
        List<Contact_Vehicle_Ownership__c> lstCVOToUpdate = new List<Contact_Vehicle_Ownership__c>();
        // iterate over SWOs and collect VIN numbers
          if (trigger.isAfter && trigger.isUpdate) 
    {
       system.debug('------------1');
        for(Contact Objcont: Trigger.New)
        {
               setStrVINNumber.add(Objcont.Id);
            
           for( Contact_Vehicle_Ownership__c ObjCVO : [select VIN__c,Name,Contact_name__c,Relationship_Start_Date__c,
                                                       Relationship_End_Date__c
                                                       from Contact_Vehicle_Ownership__c
                                                       where Contact_name__c IN :setStrVINNumber  AND Relationship_End_Date__c=Null ])
            {
                 setStrVINNumber1.add(ObjCVO.VIN__c);
                            
               // for( Vehicle__c ObjVeh: [select Name from Contact_Vehicle_Ownership__c where Name IN:setStrVINNumber1 ])
                                   for( Service_Work_Order__c ObjSWO1 : [SELECT sum(Test_Total_Work_Order_Cost_ExGST__c)activesum
                                                                FROM Service_Work_Order__c 
                                                                WHERE Workorder_VIN_Number__c IN:setStrVINNumber1])
                                       
                                   {
                                       
                                       integer abc='activesum';
                                   }
            }                       
                
            
            
            
            
             for( Contact_Vehicle_Ownership__c ObjCVO : [select VIN__c,Name,Contact_name__c,Relationship_Start_Date__c,
                                                       Relationship_End_Date__c
                                                       from Contact_Vehicle_Ownership__c
                                                       where Contact_name__c IN :setStrVINNumber  AND Relationship_End_Date__c!=Null ])
            {
                
                mapStrVINNumToTotal.put((String)objCVO.get(VIN__c),(Date)objCVO.get(Relationship_Start_Date__c),(Date)objCVO.get(Relationship_End_Date__c));
                // setStrVINNumber4.add(ObjCVO.VIN__c);
                // setStrVINNumber2.add(ObjCVO.Relationship_Start_Date__c);
                 //setStrVINNumber3.add(ObjCVO.Relationship_Start_Date__c);
                
               // for( Vehicle__c ObjVeh: [select Name from Contact_Vehicle_Ownership__c where Name IN:setStrVINNumber1 ])
                                   for( Service_Work_Order__c ObjSWO1 : [SELECT SUM(Test_Total_Work_Order_Cost_ExGST__c)
                                        FROM Service_Work_Order__c 
                                        WHERE Workorder_VIN_Number__c IN:mapStrVINNumToTotal.get(ObjCVO.VIN__c) AND  Service_Date__cIN:mapStrVINNumToTotal.get(ObjCVO.Relationship_Start_Date__c) AND Service_Date__cIN:mapStrVINNumToTotal.get(ObjCVO.Relationship_End_Date__c)] )
                                       
                                   {
                                       
                                       integer xyz='activesum';
                                   }
            }  
            
            
        }
        Objcont.Service_life_time= abc + xyz;
        
        
    }}}
            

Please ask me for more detail about this operation.

Thanks,
Karthik
Mahesh DMahesh D
Hi Karthik,

First of all, use the above button (< >) to copy the code so that it will be easy to understand the code.

Second, I didnt understand the code as you are writing the trigger on:

Service_Work_Order__c object and iterating it using the Contact object:

for(Contact Objcont: Trigger.New) {

Provide me your actual requirement so that it will be easy to help you.

Regards,
Mahesh