0

I have programmed an autoflow that launches the below class. I need to test the e-mail notifications of Messaging.sendEmail in Sandbox. The strange thing is that when launched manually from the autoflow interface (both with Run and Debug), it works perfectly. However, when launched through the same autoflow but Scheduled at certaing time, it does what it has to in terms of the Approval Process, queries etc., but doesn't send the emails. So the whole code is being read.

What I have already checked: - The class is working correctly - The Test class worked and covers 100% - It debugs and runs correctly from the run and Debug actions of flow manually. - The sandbox Deliverability is set as All Emails

The question is: - Why isn't it sending the messaging.sendEmail() when launched with scheduled autoflow? Is it a sandbox limitation that I can expect to be solved in the production environment?

public class releaseListings {

@InvocableMethod(label='Release Expired Listings')
public static void releaseExpiredBlockage(){
    Date todayDate= Date.today();
    emailTemplate emailTemplate= [SELECT id,Name, Body FROM emailTemplate WHERE Name='releaseNotification'];

    List<pba__Listing__c> expiredBlockedListings = [SELECT id, name, External_Property_ID__c, Development_Name__c,Modelo__c, sonn_Last_Block_By_User__r.Name, sonn_Last_Block_By_User__r.Email, sonn_Last_Block_Date__c, pba__Status__c FROM pba__Listing__c WHERE pba__Status__c='Blocked' AND sonn_Expected_Release_Date__c<=:todayDate];
    List<ProcessInstanceWorkitem> pendingApprovalItems = [SELECT Id,ProcessInstance.TargetObjectId FROM ProcessInstanceWorkitem WHERE  ProcessInstance.Status = 'Pending' AND ProcessInstance.ProcessDefinition.TableEnumOrId='pba__Listing__c'] ; 

    for(pba__Listing__c listing:expiredBlockedListings){
        Messaging.SingleEmailMessage releaseNotification= Messaging.renderStoredEmailTemplate(emailTemplate.Id, listing.sonn_Last_Block_By_User__c, listing.Id);
        releaseNotification.setSubject('Release Notification ' + listing.External_Property_ID__c);
        String body=releaseNotification.getHtmlBody();
        releaseNotification.setHtmlBody(body);
        releaseNotification.setTargetObjectId(listing.sonn_Last_Block_By_User__c);
        releaseNotification.setSaveAsActivity(false);


        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {releaseNotification});
        system.debug(listing.sonn_Last_Block_By_User__c);

        //Sets the expired items as "approved" and takes them out of the process. Sets all the fields to null.
        for(ProcessInstanceWorkitem wfItem: pendingApprovalItems){
            String listingIdInProcess= (String)wfItem.ProcessInstance.TargetObjectId;
            if(listingIdInProcess==listing.id){
                String itemId = (String)wfItem.get('Id');
                Approval.ProcessWorkitemRequest pwr = new Approval.ProcessWorkitemRequest();  
                pwr.setWorkItemId(itemId);
                pwr.setAction('Approve');
                Approval.ProcessResult result=Approval.process(pwr);

            }
        }

        listing.pba__Status__c='Available';
        listing.sonn_Last_Block_Date__c=null;
        listing.sonn_Last_Block_By_User__c=null;
        listing.sonn_Related_Inquiry_Lock__c=null;
        listing.sonn_Expected_Release_Date__c=null;
        update listing;


    }


}

}

0

So far, I couldn't find an explanation regarding why the Messaging method is not working as expected when scheduled with an autoflow. However, I found a workaround. I've changed the schedule and made it through APEX and the Schedule APEX Class UI in Salesforce and now it is working as expected. In case someone arrives to this question looking for an answer, this is the other way I used to Schedule it:

The class implementing the Schedule interface:

public class ScheduleReleaseListings implements Schedulable {
 public void execute(SchedulableContext SC){
    releaseListings rl= new releaseListings();
    rl.releaseExpiredBlockage();
 }}

And this was the test:

@isTest 
public class ScheduleReleaseListingTest {
@isTest public static void testSchedule(){
    ScheduleReleaseListings srl= new ScheduleReleaseListings();
    //Indicates it has to launche every two hours
    String sc= '0 0 2 * * ?';
    system.schedule('Test of Schedulable Release Listings', sc, srl);
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.