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
Roopa S 1Roopa S 1 

hiiii i have written trigger for below requirement. its not working properly......please someone help me out with this.

Need to write test class also for this
There are 3 objects in the instance:
1. Customer: The fields are:
Total No of Location(Number)-
Total No of LocationClosed(Number)
Total No of Location with Flag Down(Number)
 
2. Location(<SVMXC_Site_c> ): The fields are:
Closed(Checkbox)
Flag Down(Checkbox)
 
3. Customer Location
Customer - look up
Location(<SVMXC_Site_c> ) - look up
 
The scenario is as follows:
1. Whenever a Location is closed, the number should be updated in Customer
2. When a flag is down in a Location, the number should be updated in Customer.
3. If a Location and Customer and de-linked or re-linked, the associated numbers should be updated in the Customer
4. If a Location/ Customer Location is deleted the numbers should be associated automatically in the Customer .

This trigger i have done for 2 cases from scenario
Trigger==========================
trigger customertrigger on Customer_Location__c (after insert,after update) {
    if(trigger.isAfter && trigger.isInsert){
    locationHandler3.lochandler(trigger.new);

    if(trigger.isAfter && trigger.isUpdate){
            locationHandler3.lochandler(trigger.new);

    }
}
==================================

Handler class========================
====================================
public class locationHandler3 {
    
public static void lochandler(list<Customer_Location__c> cls){
    list<customer_c> crs=new list<customer_c>();
    set<id> clIds=new set<id>();
        set<id> clIds1=new set<id>();
    for(Customer_Location__c clss:cls){
        clIds.add(clss.customer__c);
         clIds1.add(clss.Location__c);
        system.debug('customer id'+clIds);
                system.debug('location ids'+clIds1);
    }
    integer closednumber=0;
    integer falgnumber=0;
    
    list<customer_c> customers=[select id,Name,Total_No_of_Locationc,Total_No_of_Location_closedc,Total_No_of_Location_with_Flag_Downc from customer_c where id in:clIds];
    list<SVMXC_Sitec> locs=[select id,Flag_Downc,Closedc from SVMXCSite_c where id in :clIds1]  ;
   
    for(customer__c cstr: customers){
         system.debug('customers idss'+cstr.id);
        for(SVMXC_Site_c sites:locs){
            if(sites.Closed__c==true){
                closednumber++;
                system.debug('location which are closed'+sites.Closed__c);
            }else if(sites.Flag_Down__c==true){
              falgnumber++; 
            }
          //  cstr.Total_No_of_Location_c=Customer_Location_c.size();
            cstr.Total_No_of_Location_closed__c=closednumber;
            cstr.Total_No_of_Location_with_Flag_Down__c=falgnumber;
            crs.add(cstr);
        }
    }
    if(crs.size()>0){
        update crs;}
}
}
Suraj Tripathi 47Suraj Tripathi 47
//trigger on Customer_Location__c
trigger triggerCustomerLocation on Customer_Location__c (after insert, after update) {
    switch on Trigger.operationType{
        when AFTER_INSERT{
            CustomerLoactionHandler.afterInsertChanges(trigger.new);
        }
        when AFTER_UPDATE{
            CustomerLoactionHandler.afterInsertChanges(trigger.new);
        }
    }
}
// triggerhandlerclass of Customer_Location__c
public class CustomerLoactionHandler {
    public static void afterInsertChanges(List<Customer_Location__c> newReacords){
        
        list<Customer__c> crs = new list<Customer__c>();
        set<id> clIds=new set<id>();
        set<id> clIds1=new set<id>();
        
        for(Customer_Location__c each : newReacords){
            clIds.add(each.Customer__c);
            clIds1.add(each.SVMXCSite__c);
            system.debug('customer id'+clIds);
            system.debug('location ids'+clIds1);
        }
        
        integer closednumber=0;
        integer falgnumber=0;
        
        list<Customer__c> customers=[select Id, Name, Total_No_of_Location__c,Total_No_of_LocationClosed__c, Total_No_of_Location_with_Flag_Down__c from Customer__c where id in:clIds];
        list<SVMXCSite__c> locs=[SELECT Id, Name, Flag_Down__c, Closed__c FROM SVMXCSite__c where id in :clIds1]  ;
        
        for(customer__c cstr: customers){
            for(SVMXCSite__c sites:locs){
                if(sites.Closed__c==true){
                    closednumber++;
                    system.debug('location which are closed'+sites.Closed__c);
                }else if(sites.Flag_Down__c==true){
                    falgnumber++; 
                }
                //  cstr.Total_No_of_Location_c=Customer_Location_c.size();
                cstr.Total_No_of_LocationClosed__c = closednumber;
                cstr.Total_No_of_Location_with_Flag_Down__c=falgnumber;
                crs.add(cstr);
            }
        }
        if(crs.size()>0){
            update crs;}
    }
    
}

// trigger on Location__c(SVMXCSite__c)
trigger triggerSVMXSite on SVMXCSite__c (after insert, after update) {
    Switch on Trigger.operationType{
           when AFTER_INSERT{
            LoactionTriggerHandler.AfterInsertChanges(trigger.new);
        }
         when AFTER_UPDATE{
            LoactionTriggerHandler.AfterInsertChanges(trigger.new);
        }
    }
}
// triggerhandlerclass of Location__c(SVMXCSite__c)
public class LoactionTriggerHandler {
    public static void AfterInsertChanges(LIst<SVMXCSite__c> newReacords){
        try{
            set<Id> loactionIds = new set<Id>();
            integer locationClosednumber=0;
            integer locationFalgnumber=0;
            for(SVMXCSite__c eachItem: newReacords){
                loactionIds.add(eachItem.Id);
                if(eachItem.Closed__c == true){
                    locationClosednumber++;
                }
                if(eachItem.Flag_Down__c == true){
                    locationFalgnumber++;
                }
            }
            
            set<Id> customersIds = new set<Id>();
            if(loactionIds.size() > 0){
                List<Customer_Location__c> getcustomerLocationList = [SELECT Id, Customer__c, SVMXCSite__c FROM Customer_Location__c where SVMXCSite__c IN : loactionIds];
                for(Customer_Location__c each : getcustomerLocationList){
                    customersIds.add(each.Customer__c);
                }
                
                update getcustomerLocationList;
            }
            
            if(customersIds.size() > 0){
                List<Customer__c> updateCustomerList = new List<Customer__c>();
                for(Id eachId : customersIds){
                    Customer__c customerObj = new Customer__c();
                    customerObj.Id = eachId;
                    customerObj.Total_No_of_LocationClosed__c =locationClosednumber;
                    customerObj.Total_No_of_Location_with_Flag_Down__c = locationFalgnumber;
                    
                    updateCustomerList.add(customerObj);
                }
                
                update updateCustomerList;
            }
        }
        catch(Exception e){
            system.debug('Message : '+e.getMessage() + ' at ' + e.getLineNumber());
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Suraj