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
Silpi roy 16Silpi roy 16 

Apex Debugging

Hi All!

Hope everyone is doing well.
Please help me denugging the following code.
public class ForecaseScheduleDuplicateController {
    
    public static void beforeFSController(list<ForecastSchedule__c> vlstFS)
    {
        set<string> vsetAccs = new set<string>();
        set<string> vsetOpps = new set<string>();
        set<string> vsetProds = new set<string>();
        set<string> vsetStype = new set<string>();
        set<string> vsetSegment = new set<string>();
        set<string> vsetSubSegment = new set<string>();
        set<string> vsetSalesLocation = new set<string>();
        set<date> vsetDate = new set<date>();
        for(ForecastSchedule__c fdn : vlstFS)
        {
            vsetaccs.add(fdn.Account__c);
            vsetOpps.add(fdn.Opportunity__c);
            vsetProds.add(fdn.Product2__c);
            vsetStype.add(fdn.SourceType__c);
            vsetDate.add(fdn.Date__c);
            vsetSegment.add(fdn.Segment__c);
            vsetSubSegment.add(fdn.SubSegment__c);
            vsetSalesLocation.add(fdn.SalesLocation__c);
        }
        list<ForecastSchedule__c> vlstFSinsert = new list<ForecastSchedule__c>();
        map<string,ForecastSchedule__c> vmapunikeys = new  map<string,ForecastSchedule__c>();
        for(ForecastSchedule__c fsnew : [select id,name,Opportunity__c,OpportunityOwnerFormula__c,Product2__c,Account__c,Date__c,SourceType__c,Segment__c,SalesLocation__c,SubSegment__c from ForecastSchedule__c 
                                         where 
                                         Opportunity__c IN :vsetOpps 
                                         and Product2__c IN : vsetProds
                                         and Account__c IN : vsetaccs
                                         and Date__c IN : vsetDate
                                         and SourceType__c IN : vsetStype
                                         and SubSegment__c IN :vsetSubSegment
                                         and Segment__c IN : vsetSegment
                                         and SalesLocation__c IN :vsetSalesLocation ])
        {
            system.debug('forecastschedule========'+fsnew);
            if(fsnew.SourceType__c == '4 - Budget' || fsnew.SourceType__c == '1 - Forecast')
            {
                string uniqrec = string.valueof(fsnew.Opportunity__c) + string.valueof(fsnew.Product2__c) + string.valueof(fsnew.Account__c) + string.valueof(fsnew.Date__c) + string.valueof(fsnew.SourceType__c) + string.valueof(fsnew.SalesLocation__c) + string.valueof(fsnew.Segment__c) + string.valueof(fsnew.SubSegment__c);
                
                vmapunikeys.put(uniqrec, fsnew);
            }
        }
        system.debug('keys========'+vmapunikeys.keyset());
        system.debug('values========'+vmapunikeys.values());
        for(ForecastSchedule__c fs : vlstFS)
        {
            if(fs.SourceType__c == '4 - Budget' || fs.SourceType__c == '1 - Forecast')
            {
                string uniqrec = string.valueof(fs.Opportunity__c) + string.valueof(fs.Product2__c) + string.valueof(fs.Account__c) + string.valueof(fs.Date__c) + string.valueof(fs.SourceType__c + string.valueof(fs.SalesLocation__c) + string.valueof(fs.Segment__c) + string.valueof(fs.SubSegment__c));
                
                ForecastSchedule__c fsold = vmapunikeys.get(uniqrec);
               
                 string duplicrec;
                if(fsold != null)
                 duplicrec = string.valueof(fsold.Opportunity__c) + string.valueof(fsold.Product2__c) + string.valueof(fsold.Account__c) + string.valueof(fsold.Date__c) + string.valueof(fsold.SourceType__c) + string.valueof(fsold.SalesLocation__c) + string.valueof(fsold.Segment__c) + string.valueof(fsold.SubSegment__c);
                
                system.debug('uniqrec ========'+uniqrec);
                if(uniqrec != duplicrec)
                {
                    vlstFSinsert.add(fs);
                    system.debug('vlstFSinsert========'+vlstFSinsert);
                }
                else
                {
                    
                    system.debug('entering else condition========');
                    //ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.error,'Duplicate record found');
                    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
                    opportunity opp = [select id,ownerid,owner.email from opportunity where id=:fsold.Opportunity__c limit 1];
                    
                     SendEmail(opp.id);
                     fs.adderror('Duplicate record found record' +  + fsold.Name + +'already having the same values');
                  }
            }
            
            
        }
        
        
    }
    @future
    public static void SendEmail(id oppid)
    {
      
               
                    system.debug('entering else condition========');
                    //ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.error,'Duplicate record found');
                    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
                    opportunity opp = [select id,ownerid,owner.email , owner.name from opportunity where id=:oppid limit 1];
                    Messaging.SingleEmailMessage mail = 
                        new Messaging.SingleEmailMessage();
                    list<string> tomails = new list<string>();
                    tomails.add(opp.owner.email);
                     tomails.add('chandraiah.cherukuru@atos.net');
                    system.debug('tomails====='+tomails);
                     mail.setToAddresses(tomails);
                    string body = 'Hi '+ opp.owner.name;
                    body += 'There is a record already exist with the above information';
                    body += 'we are not processing your request.<br/>';
                    body += 'Thanks,<br/>';
                    body += 'Support Team';
                    mail.setSubject('Duplicate Record');
                   
                    mail.setTargetObjectId(opp.ownerid); 
                    mail.setSaveAsActivity(false);
                    mail.setHtmlBody(body); 
                    mails.add(mail);
                    system.debug('before sending===='+mails);
                    try
                    {
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                     system.debug('After sending===='+ Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }));
                    }
                    catch(DMLexception e)
                    {
                       system.debug('error======'+e.getStackTraceString()); 
                    }
                
            
    }
    public static void beforeFSControllerUpdate(list<ForecastSchedule__c> vlstFS,Map<id,ForecastSchedule__c> vmapold)
    {
        set<string> vsetAccs = new set<string>();
        set<string> vsetOpps = new set<string>();
        set<string> vsetProds = new set<string>();
         set<string> vsetStype = new set<string>();
        set<date> vsetDate = new set<date>();
          set<string> vsetSegment = new set<string>();
        set<string> vsetSubSegment = new set<string>();
        set<string> vsetSalesLocation = new set<string>();
        
        for(ForecastSchedule__c fdn : vlstFS /*vmapold.values()*/)
        {
            vsetaccs.add(fdn.Account__c);
            vsetOpps.add(fdn.Opportunity__c);
            vsetProds.add(fdn.Product2__c);
            vsetStype.add(fdn.SourceType__c);
            vsetDate.add(fdn.Date__c);
            vsetSegment.add(fdn.Segment__c);
            vsetSubSegment.add(fdn.SubSegment__c);
            vsetSalesLocation.add(fdn.SalesLocation__c);
            
        }
       
        list<ForecastSchedule__c> vlstFSinsert = new list<ForecastSchedule__c>();
        map<string,ForecastSchedule__c> vmapunikeys = new  map<string,ForecastSchedule__c>();
        for(ForecastSchedule__c fsnew : [select id,name,Opportunity__c,OpportunityOwnerFormula__c,Product2__c,Account__c,SalesLocation__c,Segment__c,Date__c,SubSegment__c,SourceType__c from ForecastSchedule__c 
                                          where Opportunity__c IN :vsetOpps 
                                         and Product2__c IN : vsetProds
                                         and Account__c IN : vsetaccs
                                         and Date__c IN : vsetDate
                                         and SourceType__c IN : vsetStype
                                         and SubSegment__c IN :vsetSubSegment
                                         and Segment__c IN : vsetSegment
                                         and SalesLocation__c IN :vsetSalesLocation ])
        {
            system.debug('forecastschedule========'+fsnew);
            if(fsnew.SourceType__c == '4 - Budget' || fsnew.SourceType__c == '1 - Forecast')
            {
                string uniqrec = string.valueof(fsnew.Opportunity__c) + string.valueof(fsnew.Product2__c) + string.valueof(fsnew.Account__c) + string.valueof(fsnew.Date__c) + string.valueof(fsnew.SourceType__c) + string.valueof(fsnew.SalesLocation__c) + string.valueof(fsnew.Segment__c) + string.valueof(fsnew.SubSegment__c);

                vmapunikeys.put(uniqrec, fsnew);
            }
        }
        system.debug('keys========'+vmapunikeys.keyset());
        system.debug('values========'+vmapunikeys.values());
        for(ForecastSchedule__c fs : vlstFS)
        {
            if(fs.SourceType__c == '4 - Budget' || fs.SourceType__c == '1 - Forecast')
            {
               // string uniqrec = string.valueof(fs.Opportunity__c) + string.valueof(fs.Product2__c) + string.valueof(fs.Account__c) + string.valueof(fs.Date__c) + string.valueof(fs.SourceType__c);
                string uniqrec = string.valueof(fs.Opportunity__c) + string.valueof(fs.Product2__c) + string.valueof(fs.Account__c) + string.valueof(fs.Date__c) + string.valueof(fs.SourceType__c) + string.valueof(fs.SalesLocation__c) + string.valueof(fs.Segment__c) + string.valueof(fs.SubSegment__c);

                
                system.debug('uniqrec========'+uniqrec);
               ForecastSchedule__c fsold = vmapold.get(fs.id);
                 string duplicrec;
                string duplicrec1;
              //  
                  ForecastSchedule__c fsold1 = vmapunikeys.get(uniqrec);
                if(fsold1 != null)
                
                   duplicrec1 = string.valueof(fsold1.Opportunity__c) + string.valueof(fsold1.Product2__c) + string.valueof(fsold1.Account__c) + String.valueof(fsold1.Date__c) +string.valueof(fsold1.SourceType__c) + string.valueof(fsold.SalesLocation__c) + string.valueof(fsold.Segment__c) + string.valueof(fsold.SubSegment__c);

                if(fsold != null)
                 duplicrec = string.valueof(fsold.Opportunity__c) + string.valueof(fsold.Product2__c) + string.valueof(fsold.Account__c) + String.valueof(fsold.Date__c) +string.valueof(fsold.SourceType__c) + string.valueof(fsold.SalesLocation__c) + string.valueof(fsold.Segment__c) + string.valueof(fsold.SubSegment__c);
                system.debug('duplicrec========'+duplicrec);
                system.debug('duplicrec1 ========'+duplicrec1);
                if((uniqrec == duplicrec &&  uniqrec != duplicrec1) || (uniqrec != duplicrec &&  uniqrec == duplicrec1))
                {
                       fs.adderror('Duplicate record found record' +  + fsold.Name + +'already having the same values');  
                    
                   
                }
            }
            
            
        }
        
        
    }
   }

Thanks,
Silpi