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
Trif Cristian 7Trif Cristian 7 

send email notification - batch class

Hi all,

i'm trying to figure it out what I'm doing wrong here, when I'm looking for my Debog Logs I see this error:

User-added imageUser-added imageand my part of code where is the error.

Can anyone help me with this error?

Best Answer chosen by Trif Cristian 7
Max H. GoldfarbMax H. Goldfarb
Use this:
return Database.getquerylocator([SELECT Id,OwnerId,Name,Owner.Email,SIG_Service_Request_Escalation_Email, (Select Id,OwnerId,Name,Owner.Email from SVMXC__Work_Orders__r where SIG_Due_Date__c =:dt and SVMXC__Order_Status__c NOT IN ('Closed','Completed')), (Select Id,OwnerId,Name,Owner.Email from Administrative_Tasks__r where SIG_Due_Date__c =:dt and SIG_Status__c Not IN ('Closed','Completed')) From SVMXC__Service_Request__c WHERE SVMXC__Status__c = 'Open' and SVMX_SIG_Due_Date__c =: dt]);

All Answers

Max H. GoldfarbMax H. Goldfarb
Seems like you have not included "SVMXC__Service_Request__c.SIG_Service_Request_Escalation_Email' in your Service Request query (which also does not seem to be included in your snapshot)
Trif Cristian 7Trif Cristian 7

SIG_Service_Reuqest_Escalation_email is in fact the name of the email template to which I'm sending to the email.

 

User-added image

What would you suggest to do, in order to fix this problem? 

Max H. GoldfarbMax H. Goldfarb
Instead of just storing the Id of the template store the template itself and include the Name of the template in the query. Subsequently, use templateVariable.Id where you need the Id of the template and templateVariable.Name where you need the name (i,e. where the erorr is being thrown).

If that does not work, try setting the body of your email in Apex and ditch the email template entirely.
Trif Cristian 7Trif Cristian 7

Sorry, I don't get it..

My error is related to this: 

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: SVMXC__Service_Request__c.SIG_Queue_Members_Email_Address__c

So the problem is not on this line?

User-added image
Max H. GoldfarbMax H. Goldfarb
I'm assuming you are using a query to populate userSRMap. In that wuery, you need to include SVMXC__Service_Request__c.SIG_Service_Request_Escalation_Email otherwise you'll receive that error any time you try and reference that variables value for that field because it does not exist unless you qyery it, hence the reason why you see the error on that line as opposed to the query itself.
Trif Cristian 7Trif Cristian 7
This is my whole code, ignore some variables.. they are not used..
 
global class sendEmailBatchClass implements Database.Batchable < sobject > {
   
    global Database.QueryLocator start(Database.BatchableContext bc) {
        Date dt = date.today().addDays(1);
        //System.debug('scope ' + dt);
        return Database.getquerylocator([SELECT Id,OwnerId,Name,Owner.Email,
        (Select Id,OwnerId,Name,Owner.Email from SVMXC__Work_Orders__r where SIG_Due_Date__c =:dt and SVMXC__Order_Status__c NOT IN ('Closed','Completed')),
        (Select Id,OwnerId,Name,Owner.Email from Administrative_Tasks__r where SIG_Due_Date__c =:dt and SIG_Status__c Not IN ('Closed','Completed')) 
        From SVMXC__Service_Request__c  WHERE SVMXC__Status__c = 'Open' and SVMX_SIG_Due_Date__c  =: dt]);
    }
    
    global void execute(Database.BatchableContext bc, List <SVMXC__Service_Request__c> scope) {
  //   
        String userStringVal = '005';
        List<SVMXC__Service_Order__c> emailUsers = new List<SVMXC__Service_Order__c>();
        Map<Id,set<SVMXC__Service_Request__c>> srMap = new Map<Id, set<SVMXC__Service_Request__c>>();
     
        Map<Id, Set<SVMXC__Service_Order__c>> woMap = new Map<Id, Set<SVMXC__Service_order__c>>();
      
        Map<Id, Set<SIG_Administrative_Task__c>> atMap = new Map<Id, Set<SIG_Administrative_Task__c>>();
      
        Map<String, set<SVMXC__Service_Request__c>> userSRMap = new Map< String, set<SVMXC__Service_Request__c>>();
     
        Map<String, set<SVMXC__Service_Order__c>> userWoMap = new Map< String, set<SVMXC__Service_Order__c>>();
       
        Map<String, set<SIG_Administrative_Task__c>> userATMap = new Map< String, set<SIG_Administrative_Task__c>>();
       
        OrgWideEmailAddress[] owea = [select Id,DisplayName,Address from OrgWideEmailAddress where DisplayName = 'SIG OrgWideAddress'];

        For(SVMXC__Service_Request__c srVal:scope){
            If(string.valueOf(srVal.OwnerId).startsWith(userStringVal)){
                if(!userSRMap.containsKey(srVal.Owner.Email)){
                userSRMap.put(srVal.Owner.Email,new set<SVMXC__Service_Request__c>{srVal} );
                }else
                {
                   userSRMap.get(srVal.Owner.Email).add(srVal);
                   system.debug('**userSRMap'+userSRMap);
                }                   system.debug('**userSRMap'+userSRMap);

            }
            else{
                if(!srMap.containsKey(srVal.OwnerId)){
                    srMap.put(srVal.OwnerId,new set<SVMXC__Service_Request__c>{srVal} );
                }else
                {
                   srMap.get(srVal.OwnerId).add(srVal);
                   system.debug('***srMap'+srMap);
                }                   system.debug('**userSRMap'+userSRMap);

            }
            
            For(SVMXC__Service_Order__c woVal :srVal.SVMXC__Work_Orders__r){
                If(string.valueOf(srVal.OwnerId).startsWith(userStringVal)){
                if(!userWoMap.containsKey(woVal.Owner.Email)){
                userWoMap.put(woVal.Owner.Email,new set<SVMXC__Service_Order__c>{woVal} );
                }else
                {
                   userWoMap.get(woVal.Owner.Email).add(woVal);
                }
            }
            else{
                if(!woMap.containsKey(woVal.OwnerId)){
                    woMap.put(woVal.OwnerId,new set<SVMXC__Service_Order__c>{woVal} );
                }else
                {
                   woMap.get(woVal.OwnerId).add(woVal);
                }
            }
            }
            
            For(SIG_Administrative_Task__c atVal :srVal.Administrative_Tasks__r){
                If(string.valueOf(atVal.OwnerId).startsWith(userStringVal)){
                if(!userATMap.containsKey(atVal.Owner.Email)){
                userATMap.put(atVal.Owner.Email,new set<SIG_Administrative_Task__c>{atVal} );
                }else
                {
                   userATMap.get(atVal.Owner.Email).add(atVal);
                }
            }
            else{
                if(!atMap.containsKey(atVal.OwnerId)){
                    atMap.put(atVal.OwnerId,new set<SIG_Administrative_Task__c>{atVal} );
                }else
                {
                   atMap.get(atVal.OwnerId).add(atVal);
                }
            }
            }
         }
        
         
         // Sending Email for Service Request
         
          For(String userval1: userSRMap.keyset()){
           For(SVMXC__Service_Request__c srlistval: userSRMap.get(userval1)){
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

                Id templateId =  [select id, name from EmailTemplate where developername = 'SIG_Service_Request_Escalation_Email'].id;
                // Create Contact
                    Contact con;
                    con = new Contact();
                    con.FirstName = 'Test';
                    con.LastName = 'Contact';
                    con.Email = 'no-reply@organization.com';
                    insert con;
                    email.setTargetObjectId(con.Id);
               // email.setTargetObjectId(srlistval.ownerId);
               String[] toAddresses ;
            
              
              toAddresses = new String [] {};
                for (String emaill : srlistval.SIG_Queue_Members_Email_Address__c.split(',')) {
                                    String trimmed = emaill.trim(); 
                                     if (trimmed.length() > 0) {
                                      toAddresses.add(trimmed);
                                     }
                                  System.debug('***emailAddress'+toAddresses);
                }
                
                email.setOrgWideEmailAddressId(owea[0].Id);
                email.setCcAddresses(toAddresses);

                email.setWhatId(srlistval.Id);
                email.setTemplateId(templateId);
                email.setSaveAsActivity(false);
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
                                   system.debug('***email Message'+email);

            }
          }
          
          // Sending Email For Work Order
    
          For(String userval1: userWoMap.keyset()){
           For(SVMXC__Service_Order__c woListVal: userWoMap.get(userval1)){
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                Id templateId =  [select id, name from EmailTemplate where developername = 'Work_Order_test'].id;
                //email.setTargetObjectId(woListVal.ownerId);
                // Create Contact
                    Contact con;
                    con = new Contact();
                    con.FirstName = 'Test';
                    con.LastName = 'Contact';
                    con.Email = 'no-reply@organization.com';
                    insert con;
                    email.setTargetObjectId(con.Id);
                String[] toAddresses ;
  
              // I have hardcoded my Id for testing , please update the logic to add the respective email addresses.
            // toAddresses   = new String[] {'vivek.agrawal@servicemax.com'};
            //   toAddresses = new String [] {'vivek.agrawal@servicemax.com', 'cristian-ovidiu.trif@sig.biz'};
                for (String emaill : woListVal.SIG_Queue_Members_Email_Address__c.split(',')) {
                                    String trimmed = emaill.trim(); 
                                     if (trimmed.length() > 0) {
                                      toAddresses.add(trimmed);
                                     }
                                  System.debug('***emailAddress'+toAddresses);
                }
                             
                             
                email.setOrgWideEmailAddressId(owea[0].Id);
                email.setCcAddresses(toAddresses);

                email.setWhatId(woListVal.Id);
                email.setTemplateId(templateId);
                email.setSaveAsActivity(false);
               Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
            }
          }
          
          // Sending Email For Admin  Task
           For(String userval1: userATMap.keyset()){
           For(SIG_Administrative_Task__c adListVal: userATMap.get(userval1)){
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                Id templateId =  [select id, name from EmailTemplate where developername = 'Admin_Task_Escalation_Email'].id;
                // Create Contact
                    Contact con;
                    con = new Contact();
                    con.FirstName = 'Test';
                    con.LastName = 'Contact';
                    con.Email = 'no-reply@organization.com';
                    insert con;
                    email.setTargetObjectId(con.Id);
               // email.setTargetObjectId(adListVal.ownerId);
               
              String[] toAddresses ;
  
              // I have hardcoded my Id for testing , please update the logic to add the respective email addresses.
            // toAddresses   = new String[] {'vivek.agrawal@servicemax.com'};
             //  toAddresses = new String [] {'vivek.agrawal@servicemax.com', 'cristian-ovidiu.trif@sig.biz'};
                for (String emaill : adListVal.SIG_Queue_Members_Email_Address__c.split(',')) {
                                    String trimmed = emaill.trim(); 
                                     if (trimmed.length() > 0) {
                                      toAddresses.add(trimmed);
                                     }
                                  System.debug('***emailAddress'+toAddresses);
                }
                
                email.setOrgWideEmailAddressId(owea[0].Id);
                email.setCcAddresses(toAddresses);

                email.setWhatId(adListVal.Id);
                email.setTemplateId(templateId);
                email.setSaveAsActivity(false);
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
            }
          }
          
          
          
    }
    
    global void finish(database.BatchableContext bc) {
        
    }
}


I'm trying to send email notifications to 3 different objects.: Admin Task, Work Order, Service Request.
We have a trigger that automatically populates the field: SIG_Queue_Members_Email_Address__c with the email users from a queue. In our case the owner can be a queue also...
Trif Cristian 7Trif Cristian 7
Now that I gave you my whole code can you tell me the line on where I have to put : " SVMXC__Service_Request__c.SIG_Service_Request_Escalation_Email" please?
Max H. GoldfarbMax H. Goldfarb
I see the issue, it is in your initial query, try this:
 
return Database.getquerylocator([SELECT Id,OwnerId,Name,Owner.Email,SIG_Service_Request_Escalation_Email, (Select Id,OwnerId,Name,Owner.Email from SVMXC__Work_Orders__r where SIG_Due_Date__c =:dt and SVMXC__Order_Status__c NOT IN ('Closed','Completed')), (Select Id,OwnerId,Name,Owner.Email from Administrative_Tasks__r where SIG_Due_Date__c =:dt and SIG_Status__c Not IN ('Closed','Completed')) From SVMXC__Service_Request__c WHERE SVMXC__Status__c = 'Open' and SVMX_SIG_Due_Date__c =: dt]);

 
Trif Cristian 7Trif Cristian 7

Nope, its not working...

User-added image

Max H. GoldfarbMax H. Goldfarb
The only reason that did not work is because you copy and pasted my reply and did not remove the html underline tags...
Max H. GoldfarbMax H. Goldfarb
Use this:
return Database.getquerylocator([SELECT Id,OwnerId,Name,Owner.Email,SIG_Service_Request_Escalation_Email, (Select Id,OwnerId,Name,Owner.Email from SVMXC__Work_Orders__r where SIG_Due_Date__c =:dt and SVMXC__Order_Status__c NOT IN ('Closed','Completed')), (Select Id,OwnerId,Name,Owner.Email from Administrative_Tasks__r where SIG_Due_Date__c =:dt and SIG_Status__c Not IN ('Closed','Completed')) From SVMXC__Service_Request__c WHERE SVMXC__Status__c = 'Open' and SVMX_SIG_Due_Date__c =: dt]);
This was selected as the best answer
Trif Cristian 7Trif Cristian 7

Yeah. I fixed the error :)

I had to include the:SIG_Queue_Members_Email_Address__c

 

User-added image

Max H. GoldfarbMax H. Goldfarb
Dang, sorry for providing wrong field name, same underlying issue though :)