Untitled

 avatar
unknown
plain_text
a year ago
3.1 kB
3
Indexable
public class BatchToSendSurvey implements Database.Batchable<sObject>, Database.Stateful {
    
    
    public Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT Id,Name,Email FROM Contact where Email= \'tarun.rana1999@gmail.com\'';
    
        return Database.getQueryLocator(query);
    }
    public void execute(Database.BatchableContext BC, List<Contact> conList){
  string communityId =[select Id from Network where Name = 'Patient'][0].Id;
   string  surveyId = [SELECT Id FROM Survey WHERE Name = 'Test Survey'][0].Id;
        System.debug('communityId>>>>>>'+ communityId);
        System.debug('SurveyId>>>>>>'+ surveyId);
        
         List<SurveyInvitation> surveyInvite = new List<SurveyInvitation>();
        List<SurveySubject> surveySub = new List<SurveySubject>();
  
        
        for(Contact con: conList){
            
                           
            SurveyInvitation sInv = new SurveyInvitation();
            sInv.Email__c=con.Email;
            sInv.CommunityId= communityId;
            sInv.Name=con.Name;
             sInv.OptionsCollectAnonymousResponse = true;
            sInv.OptionsAllowGuestUserResponse = true;
            sInv.SurveyId = surveyId;
            surveyInvite.add(sInv);
            
                      
            SurveySubject sSub = new SurveySubject();
            sSub.Name=con.Name;
            sSub.ParentId=sInv.Id;
            surveySub.add(sSub);
            
          
        }
               insert surveyInvite;
        
        for (SurveySubject sSub : surveySub) {
            sSub.ParentId = surveyInvite[0].Id;
        }
        insert surveySub;
             

         List<SurveyInvitation> surveyInvitations = [SELECT Email__c, InvitationLink FROM SurveyInvitation where Email__c = 'tarun.rana1999@gmail.com'];
     
            Map<String, String> emailToLinkMap = new Map<String, String>();
        for (SurveyInvitation inv : surveyInvitations) {
            System.debug(inv.InvitationLink);
            emailToLinkMap.put(inv.Email__c, inv.InvitationLink);
        }
        
        // Send emails
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        String temail = 'tarun.rana1999@gmail.com';
            if (emailToLinkMap.containsKey(temail)) {
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setToAddresses(new String[] { temail });
                String subject = 'Link Mil Gaya';
                String body = 'Here is your survey invitation link: ' + emailToLinkMap.get(temail);
                mail.setSubject(subject);
                mail.setPlainTextBody(body);
                mails.add(mail);
            
        }
        
        // Send all emails in a single sendEmail call
        Messaging.sendEmail(mails);
    }
           
                 
    public void finish(Database.BatchableContext BC){
        System.debug('batch executed');
        
    }

    
}
Editor is loading...
Leave a Comment