1
global class InvocableController{

   @InvocableMethod
   global  static List<ActionRequest> convert(List<ActionRequest> requestlist) {
        List<ActionRequest> results = new List<ActionRequest>();
        for (ActionRequest actionReq : requestlist) {
            results.add(createTask(actionReq));
             results.add(sendEmailToContacts(actionReq));
        }
        return results;

   }

    global class ActionRequest{
        @InvocableVariable
        public ID parentId;

        @InvocableVariable
        public String subject;

        @InvocableVariable
        public String description;

        @InvocableVariable
        public Date dueDate;

        @InvocableVariable
        public String header;

        @InvocableVariable
        public String message;

        @InvocableVariable
        public Id emailTemplateId ;

        @InvocableVariable
        public List<Contact> Contacts;
    }

    public static ActionRequest createTask(ActionRequest request){
        List<Task> taskList=new List<Task>();
        for(Contact con:request.contacts){
            Task task = new Task();
            task.Subject=request.subject;
            task.WhatId=request.parentId;
            task.WhoId=con.Id;
            task.Description=request.description;
            task.ActivityDate=request.dueDate;
            task.Confirmation_Header__c=request.header;
            task.Confirmation_Message__c=request.message;
            taskList.add(task);
        }
        insert taskList;

        return request;
    }

    public static ActionRequest sendEmailToContacts(ActionRequest request){ 

        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        EmailTemplate et=[Select id from EmailTemplate where Id =:request.emailTemplateId];

        for(Contact con : request.contacts){
            Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
            singleMail.setTargetObjectId(con.Id);
            singleMail.setWhatId(request.parentId);
            singleMail.setTemplateId(et.id);
            singleMail.setSaveAsActivity(true);
            emails.add(singleMail);
        }
        Messaging.sendEmail(emails);
         return request;
    }
}
2

0

You must log in to answer this question.

Browse other questions tagged .