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
mita mukwevhomita mukwevho 

Hi guys, how can i write apex code for test class and i need to schedule every monday morning please help, and see below my code

global class AssignedRequestsWeeklyEmail implements Schedulable {
    
    public List<Internal_Request__c> InternalRequest {get;set;} 
    public List<Request_User__c> RequestUser {get;set;}
    global void execute(SchedulableContext ctx) {
        // awesome code here
        List<Request_User__c> RequestUser = new List<Request_User__c>();
        boolean blnError = false;
        
        // Create a tasklist that includes all the tasks that are not complete 
        List<Request_User__c> taskList = [SELECT MRG_Request_Management__r.name,name,Assigned_To__c,Assigned_To__r.Email, ownerId, Assigned_Name__c, Assigned_Request_Number__c, 
                                          Request_Category__c, Request_Sub_Category__c, Status__c 
                                          from Request_User__c 
                                          Where Status__c !='Complete'];
        system.debug('tasklist: ' + taskList);
        
        String taskId;
        if(taskList != null){
            system.debug('tasklist is not empty');
            
            Map<string, List<Request_User__c>> RequestsuserToEmail = new Map<string, List<Request_User__c>>();
            Map<string, List<string>> userEmailToRequests = new Map<string, List<string>>();
            //Set<Id> ownerSet = new Set<Id>();
            for(Request_User__c item : taskList) {
                if(!userEmailToRequests.containsKey(item.Assigned_To__r.Email))
                {
                    RequestsuserToEmail.put(item.Assigned_To__r.Email,new list<Request_User__c>());
                    RequestsuserToEmail.get(item.Assigned_To__r.Email).add(item);               
                    userEmailToRequests.put(item.Assigned_To__r.Email,new list<string>());
                    userEmailToRequests.get(item.Assigned_To__r.Email).add(item.MRG_Request_Management__r.name+' '+item.Assigned_Request_Number__c);
                } else {
                    RequestsuserToEmail.get(item.Assigned_To__r.Email).add(item);
                    userEmailToRequests.get(item.Assigned_To__r.Email).add(item.MRG_Request_Management__r.name+' '+item.Assigned_Request_Number__c);
                    
                } 
            }
            system.debug('userEmailToRequests');
            system.debug(userEmailToRequests);
            
            EmailTemplate et=[Select id, subject, body from EmailTemplate where id = '00X6E0000012Y2K' limit 1];
            list<Messaging.SingleEmailMessage> emailToSendList = new list<Messaging.SingleEmailMessage>();
            //LOOP OVER THE MAP KEY!
            for (string key : userEmailToRequests.keySet()) {   
                //List<Request_User__c> reqList = RequestsuserToEmail.get(key);
                List<string> reqList = userEmailToRequests.get(key);
                system.debug('for email address '+key);
                system.debug('we have this list of requests');
                system.debug(reqList);
                integer counter = 1;
                
                string neatReqList='';
                for(string myitem : reqList)
                {
                    neatReqList = neatReqList + counter+'-  '+myitem+'\n';                                        
                    counter++;
                    
                } 
                
                system.debug(neatReqList);
                //BUILD EACH EMAIL USING MAP KEY AND CALLING THE MAP WITH REQUEST USER ITEMS
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses = new String[] {'mukwevho.mita1@gmail.com'};
                //mail.setToAddresses(new String[]{key});
                mail.setToAddresses(toAddresses);
                //mail.setSubject('Testing.. Weekly update of open tasks');
                mail.setTargetObjectId(RequestsuserToEmail.get(key)[0].Assigned_To__c);//requires ownerid
                mail.setWhatId(RequestsuserToEmail.get(key)[0].id);//requires id of sobject record
                string emailBody = et.body;
                system.debug('emailBody');
                system.debug(emailBody);
                    emailBody = emailBody.replace('##list##',neatReqList);              
                et.Body=et.Body.replace ('##list##',neatReqList);
                mail.setTemplateId(et.Id);
                //mail.setSenderDisplayName(' Testing.. Assigned Request Weekly Email'); 
               // mail.setSubject('Please find below a list of task assigned to you.');                
                mail.setSaveAsActivity(false);                
                mail.setPlainTextBody(emailBody);               
                emailToSendList.add(mail);
                Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
 

                    }            
                }
            }
    }
Rounak SharmaRounak Sharma
hello Mita,
You have to write a batch class in which in start method you need to the query all the fields from Request_User__c. then in execute method you need to write a email method and in the finish method you have to schedule the batch.

Please let me know if you still have doubt.
thanks 
mita mukwevhomita mukwevho
Hello Rounak Sharma,

Please note that i'm new on salsforce deveper  role that is why i'm asking for any help and i need to schedule it  weekly ... i will appriciate you help
Thank 
mita