Untitled

 avatar
unknown
plain_text
10 months ago
1.8 kB
5
Indexable
public class BatchToSendSurvey implements Database.Batchable<sObject> {
    
   
    public Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT Id,Name,Email FROM Contact Where Name = \'Batch Test\'';
    
        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){
            String conEmail= con.Email;
            System.debug('conEmai'+conEmail);
            
            SurveyInvitation sInv = new SurveyInvitation();
            sInv.Email__c=conEmail;
            sInv.CommunityId= communityId;
            sInv.Name=con.Name;
             sInv.OptionsCollectAnonymousResponse = true;
            sInv.OptionsAllowGuestUserResponse = true;
            sInv.SurveyId = surveyId;
            surveyInvite.add(sInv);
            
            insert surveyInvite;
            
            SurveySubject sSub = new SurveySubject();
            sSub.Name=con.Name;
            sSub.Id= con.Id;
            sSub.ParentId=sInv.Id;
            surveySub.add(sSub);
            
             insert surveySub;
            
        }
            
       
    }
    public void finish(Database.BatchableContext BC){
        System.debug('batch executed');

    }
    
}
Editor is loading...
Leave a Comment