+ Start a Discussion
sumit dsumit d 

Making class invocable to run a schedulable class

Hi All,
           I have a schedulable class, I want ot call it from another class and make the second class invocable. how can i do this?
My class and invocable class is given below:-

global  class SendEmailToKnowledgeArticleFollower implements Schedulable {

    global void execute(SchedulableContext sc)
        {
            SendEmail();
        }
    public Static void SendEmail() {
        Set<Id> knowledgeArticleIdSet = new Set<Id>();
        Map<Id,Knowledge__kav> mapOfKnowledgeArticleVersion = new  Map<Id,Knowledge__kav>();
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        
        EmailTemplate et = [SELECT Id,Subject, Body FROM EmailTemplate WHERE DeveloperName = 'KnowledgeArticlePublished'];
        List<Knowledge__kav> knowledgeArticlesPublishedToday =  [SELECT Id,LastPublishedDate,PublishStatus,KnowledgeArticleId,IsLatestVersion,Title  
                                                                FROM Knowledge__kav 
                                                                WHERE LastPublishedDate  = TODAY ];
        
        if (!knowledgeArticlesPublishedToday.isEmpty()) {
            
            for (Knowledge__kav kav : knowledgeArticlesPublishedToday) {
                knowledgeArticleIdSet.add(kav.KnowledgeArticleId);
                mapOfKnowledgeArticleVersion.put(kav.KnowledgeArticleId,kav);
            }

            List<EntitySubscription> knowledgeArticleFolower = [SELECT  ID, ParentId, SubscriberId,Subscriber.Name,Subscriber.Email   
                                                                FROM EntitySubscription 
                                                                WHERE ParentId 
                                                                IN :knowledgeArticleIdSet];
            if (!knowledgeArticleFolower.isEmpty()) {
                OrgWideEmailAddress orgWideAddress = [SELECT Id, Address, DisplayName 
                                                              FROM OrgWideEmailAddress 
                                                              WHERE Address='techsupport_test@on24.com' 
                                                              LIMIT 1 ];
                        
                        
                for (EntitySubscription follower : knowledgeArticleFolower) {
                    List<String> sendTo = new List<String>();
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    Messaging.SingleEmailMessage getEmailTempResponse = Messaging.renderStoredEmailTemplate(et.Id, follower.SubscriberId, mapOfKnowledgeArticleVersion.get(follower.ParentId).Id);
                    sendTo.add(follower.Subscriber.Email);
                    // Set email contents - you can use variables!
                    if ( orgWideAddress != null) {
                        mail.setOrgWideEmailAddressId( orgWideAddress.Id );
                        // mail.setSenderDisplayName(orgWideAddress.DisplayName);
                    }
                    mail.setToAddresses(sendTo);
                    mail.setSubject(getEmailTempResponse.getSubject());
                    mail.setHTMLBody(getEmailTempResponse.getHtmlBody());
                    mails.add(mail);
                }
                Messaging.sendEmail(mails);
            }
    }
    }
}

Invocable class is given below:- 
public class InvocableSendEmailToFollowers {
    @InvocableMethod
    public static void SendEmailToFollower( List<Id> articleIds ) {
     List<Knowledge__kav> knowledgeArticlesPublishedToday =  [SELECT Id,LastPublishedDate,PublishStatus,KnowledgeArticleId,IsLatestVersion,Title  
                                                                FROM Knowledge__kav 
                                                                WHERE LastPublishedDate  = TODAY AND Id IN: articleIds ];
        SendEmailToKnowledgeArticleFollower.SendEmail();
    }
        
}
here i want that when i will call a flow from button after clicking of button flow will call the invocable and do the rest thing.
Can anyone help me with this?
 
PriyaPriya (Salesforce Developers) 
Hi Sumit,

You can create apex action to call the ivocable method.And you can create a Button Invoke Method to call the flow.

Please refer below link https://www.greytrix.com/blogs/salesforce/2021/12/30/how-to-call-an-apex-class-from-a-screen-flow/

Hope this is helpful!

Regards,
Ranjan