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
NK ShaNK Sha 

Report generation Test class

Can any one help me for the Test class

public class ReportGenerationApexClass{
    public static void reportToDeal(){
        String htmlBody = '';
        Set<id> dealidSet = new Set<id>();
        Set<id> conIdSet = new Set<id>();
        htmlBody = '<table border="1" style="border-collapse: collapse"><caption>Request Summary Data</caption><tr><th>Name</th><th>Phone</th></tr>';
        List<Daily_Report_EmailIds__c> emailListCS=[Select Name,Email__c from Daily_Report_EmailIds__c];
        system.debug('###emailListCS: '+emailListCS);
        Map<id, String> taskMap=new Map<id,String>();
        
        
        
        Map<id,Integer> countMap = new Map<id,Integer>();
        Map<Id, String> conIdTaskMap = new Map<Id, String>();
        List<Task> taskListNew = new List<task>();
        
        
        
        List<Contact> conList= new List<Contact>();
        
        List<ascendix__Deal__c> deallist = [Select name,Priority__c,createdDate ,ascendix__Status__c,
                                            ascendix__ClientContact__r.Name, ascendix__ClientContact__r.Last_Attempt__c,
                                            ascendix__ClientContact__r.Last_Reach__c,
                                            ascendix__ClientContact__r.Last_Meeting__c, ascendix__ClientContact__r.Relationship_Manager__r.Name,
                                            (Select Description,CreatedBy.Name,CreatedDate from Events),
                                            (Select who.Name,Description,CreatedBy.Name,CreatedDate  from Tasks)
                                            from ascendix__Deal__c where ascendix__SalesStage__c = 'Proposal/Presentation' AND
                                            RecordTypeId in (SELECT Id FROM RecordType WHERE RecordType.Name IN ('Buyer Representation', 'Seller Representation'))];
        
        system.debug('###dealList: '+dealList);
        string header = 'Priority \t Created Date \t Status \t Contact \t Last Attempt \t Last Reach \t Last Meeting \t Relationship Manager \t Last Meeting Note \t Last Call Note 1 \t Last Call Note 2 \t Last Call Note 3 \t Notes \n';
        system.debug('###header: '+header);
        string finalstr = header ;
        system.debug('###finalstr : '+finalstr);
        for(ascendix__Deal__c d1: deallist)
        {
            conIdSet.add(d1.ascendix__ClientContact__c);
        }
        system.debug('###conIdSet: '+conIdSet);
        
        conList=[Select id,Name,(Select id,description,who.Id,CreatedBy.Name,CreatedDate from Tasks where type='Call' AND  IsDeleted = false order by LastModifiedDate DESC ) from Contact where Id IN:conIdSet];
        system.debug('###!conList: '+conList);
        Map<Id,String> contIdStringMap = new Map<Id,String>();
        for(Contact c : conList){
            String newDescup='';
            
            integer i=0;
            system.debug('###tasks: '+c.Tasks);
            for(Task t: c.Tasks){
                
                
                if(t.description<>null && t.description<>'' && i<3){
                    System.debug('$$$$i'+i);
                    //String Cname=t.CreatedBy.Name;
                    System.debug('$$$$t.CreatedDate;'+t.CreatedDate);
                    DateTime createdD=t.CreatedDate;
                    System.debug('$$$$createdD.day()'+createdD.month());
                    String dt = DateTime.newInstance(createdD.year(),createdD.month(),createdD.day()).format('MM/dd/yyyy');
                    dateTime dtmonth = (DateTime.newInstance(createdD.month()));
                  //  DateTimedtdate = (DateTime.newInstance(createdD.day()));
                    System.debug('$$$$dt'+dt);
                   // String dtnew= dt.format('dd/mm/yyyy');
                 //   System.debug('$$$$dtnew'+dtnew);
                    newDescup +='['+t.CreatedBy.Name+' '+ dt+']'+' '+ t.description.remove('\n')+'\t';
                    system.debug('###newDescup: '+newDescup);
                    i=i+1;
                }
                
            }
            contIdStringMap.put(c.id,newDescup);
        }
        
        for(ascendix__Deal__c d: deallist)
        {
            String finalstrnew= contIdStringMap.get(d.ascendix__ClientContact__c);
            System.debug('$$$$finalstrnew'+finalstrnew);
            dealidSet.add(d.id);
            List<String> desList=contIdStringMap.values();
            List<Event> eventList=new List<Event>();
            
            String Descrip;
            String newDescup1='';
            eventList=d.Events;
            system.debug('###eventList: '+eventList);
            if(eventList.size()>0){
                for(Event e : eventList){
                    if(e.Description==Null){
                        
                    }
                    else{
                        DateTime createdD=e.CreatedDate;
                    System.debug('$$$$createdD.day()'+createdD.month());
                    String dt = DateTime.newInstance(createdD.year(),createdD.month(),createdD.day()).format('MM/dd/yyyy');
                    dateTime dtmonth = (DateTime.newInstance(createdD.month()));
                    System.debug('$$$$dt'+dt);
                    newDescup1 +='['+e.CreatedBy.Name+' '+ dt+']'+' '+ e.description;
                    system.debug('###newDescup1: '+newDescup1);
                        //Descrip=e.Description;
                        break;
                    }
                }
            }
            List<Task> taskList=new List<Task>();
            List<Task> taskListD=new List<Task>();
            String DescripT;
            taskList=d.Tasks;
            system.debug('###taskList: '+taskList);
            if(taskList.size()>0){
                for(Task t : taskList){
                    if(t.Description==Null){
                        
                    }
                    else{
                        taskListD.add(t);    
                    }
                    system.debug('##taskwithDescription: '+taskListD);
                }
            }
            String comm;
            String comm2;
            String comm3;
            String RMName;
            String DName;
            String priorityD;
            String dateC;
            String statusD;
            String ContactName;
            String dateA;
            String dateR;
            String dateM;
            String Url1 ;
            
            if(taskListD.size()>0){
            }
            system.debug('###Hi for');
            if(d.Name==null){
                Dname='';
            }
            else{
                Dname=d.Name;
            }
            if(d.Priority__c==null){
                priorityD='';
            }
            else{
                priorityD=d.Priority__c;
            }
            if(d.createdDate==null){
                dateC='';
            }
            else{
                DateTime dT = d.createdDate;
                Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
                dateC=String.valueOf(myDate);    
            }
            if(d.ascendix__Status__c==null){
                statusD='';
            }
            else{
                statusD=d.ascendix__Status__c;
            }
            if(d.ascendix__ClientContact__r.Name==null){
                Url1='';
            }
            else{
                string baseUrl = String.valueOF(System.URL.getSalesforceBaseUrl().toExternalForm());    
                String datafile = baseUrl+'/' + d.ascendix__ClientContact__c;
                String cName=d.ascendix__ClientContact__r.Name;
                Url1 = '"=HYPERLINK(""'+datafile+'"",""'+cName+'"")"';
                system.debug('###Url: '+datafile);
                system.debug('###Url1: '+Url1);
                
            }
            if(d.ascendix__ClientContact__r.Last_Attempt__c==null){
                dateA='';
            }
            else{
                dateA=String.valueOf(d.ascendix__ClientContact__r.Last_Attempt__c);
            }
            if(d.ascendix__ClientContact__r.Last_Reach__c==null){
                // d..ascendix__ClientContact__r.Last_Reach__c='';
                dateR='';
            }
            else{
                dateR=String.valueOf(d.ascendix__ClientContact__r.Last_Reach__c);
            }
            
            if(d.ascendix__ClientContact__r.Last_Meeting__c==null){
                dateM='';
            }
            else{
                dateM=String.valueOf(d.ascendix__ClientContact__r.Last_Meeting__c);
            }
            
            if(d.ascendix__ClientContact__r.Relationship_Manager__r.Name==null){
                RMName='';
            }
            else
            {
                RMName=d.ascendix__ClientContact__r.Relationship_Manager__r.Name;
            }
            if(Descrip==null){
                Descrip='';
            }
            if(comm==null){
                comm='';
            }
            if(comm2==null){
                comm2='';
            }
            if(comm3==null){
                comm3='';
            }
            if(finalstrnew==null){
                finalstrnew='';
            }
            
            string recordString =
                priorityD+'\t'+dateC+'\t'+statusD+'\t'+Url1+
                '\t'+dateA+'\t'+dateR+
                '\t'+dateM+'\t'+RMName+
                '\t'+newDescup1+'\t'+finalstrnew+'\n';
            system.debug('###recordString: '+recordString);
            
            finalstr = finalstr +recordString;
            finalstr.replaceAll('null','');
            system.debug('###finalstr2 : '+finalstr);
            
        }
       
        htmlBody += '</table>';
        
        system.debug('Email Body: ' + htmlBody);   
        
        for(Daily_Report_EmailIds__c e : emailListCS){
            Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
            
            blob csvBlob = Blob.valueOf(finalstr);
            system.debug('###csvBlob : '+csvBlob);
            //string csvname= 'Deals';
            // system.debug('###csvname: '+csvname);
            csvAttc.setFileName('csvname.xls');
            //csvAttc.setContentType('application/vnd.ms-excel');
            csvAttc.setBody(csvBlob);
            Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
            String[] toAddresses = new list<string> {e.Email__c};
                //@gmail.com
                String subject ='CSV Deal';
            email.setSubject(subject);
            email.setToAddresses( toAddresses );
            email.setPlainTextBody(htmlBody);
            email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
            system.debug('###r : '+r);
        }
    }