function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tyler SchryverTyler Schryver 

Problem with passing Account ID to a Flow Interview

I'm having an issue that I'd like another set of eyes to look at. I have a trigger that is invoked after an update on Task. The trigger updates a custom object to which the Tasks are attached, when a Task is completed. The custom object is linked in a M/D relationship to Account. When an Opportunity is Closed as Won, a Process Builder Flow invokes a FLOW interview passing it the Account Id and the Opportunity Id from the Opportunity that invokes the flow. The flow then creates the custom object record. This all works fine.

I'm writting the Test Class for the Trigger and this is where the issue comes up. When I execute a test on the Test Class it fails with error message indicating that it failed to invoke a FLOW. Bottom line is that the FLOW that creates the custom object is failing because the account id being passed to it is null, although the opportunity id has data.

I'm creating the account, several opportunities, and a number of tasks in the test class, and I'm pulling the account id from the account list i've created. Please see the code below, and please let me know why I'm not getting the account id for the account created. Thanks!
@isTest (seeAllData=false)
private class TestONBUpdateFromTask {

    public static List<Task> tasksList;
    public static List<Opportunity> opportunitiesList;
    public static List<Account> accountsList;
    public static String subject;
    
    static void init(){
    tasksList = new List<Task>();
    opportunitiesList = new List<Opportunity>();
    accountsList = new List<Account>();    
    
        accountsList.add(new Account(Name = 'Test', Sales_Region__c = '1 - Southeast', Type = 'Prospect' ));                       
        opportunitiesList = new List<Opportunity>{
            new Opportunity(
                        Name = 'Test1',
                        CloseDate = date.today().addDays(5),
                        AccountId = accountsList[0].Id,
                        Type = 'New Business', 
                        TransLoc_Hardware__c = 'Yes',
                        Integration__c = 'NO', 
                        StageName = 'Closed Won'),
                new Opportunity(
                        Name = 'Test2',
                        CloseDate = date.today().addDays(5),
                        AccountId = accountsList[0].Id,
                        Type = 'New Business', 
                        TransLoc_Hardware__c = 'Yes',
                        Integration__c = 'NO', 
                        StageName = 'Closed Won'),
                new Opportunity(
                        Name = 'Test3',
                        CloseDate = date.today().addDays(5),
                        AccountId = accountsList[0].Id,
                        Type = 'New Business', 
                        TransLoc_Hardware__c = 'Yes',
                        Integration__c = 'NO', 
                        StageName = 'Closed Won'),
                new Opportunity(
                        Name = 'Test4',
                        CloseDate = date.today().addDays(5),
                        AccountId = accountsList[0].Id,
                        Type = 'New Business', 
                        TransLoc_Hardware__c = 'Yes',
                        Integration__c = 'NO', 
                        StageName = 'Closed Won'),
                new Opportunity(
                        Name = 'Test5',
                        CloseDate = date.today().addDays(5),
                        AccountId = accountsList[0].Id,
                        StageName = 'Closed Won')
            };
    }
    
    static testMethod void testWithExistingTask() {
    init();
    Test.startTest();
    
        tasksList = new List<Task>{new Task(
        Subject = subject, Status='Completed'
        ),
        new Task(Subject='RealTime Project Kick-Off', Status ='Completed'),
        new Task(Subject='Confirm OSM Region Added', Status ='Completed'),
        new Task(Subject='Enable Go-Live', Status='Completed') ,
        new Task(Subject='Finance Confirm Payment Received' , Status = 'Completed'),
        new Task(Subject='GTFS and Backend Setup' , Status = 'Completed'),
        new Task(Subject='OnDemand New Product Training' , Status = 'Completed'),
        new Task(Subject='Schedule Travel', Status = 'Completed')};
        
        
        insert accountsList;
        insert opportunitiesList;
        insert tasksList;       
        
    tasksList = [
        SELECT Id, WhatId
        FROM Task
        
        ];

        //if (!tasksList.isEmpty()){
        Integer i = 0;
        for(Task t : tasksList){
            t.WhatId = opportunitiesList[i].Id;
            //i++;
        }
        
        update tasksList;
        
    //System.assertEqual(tasksList[0].WhatId, opportunitiesList[0].Id);
    
    //Test.stopTest();
    }
    
    /*
    static testMethod void testWithoutExistingTask() {
    init();
    Test.startTest();
    
    insert accountsList;
    accountsList = [
        SELECT Id, Name
        FROM Account
        WHERE Name = 'Test'
        ];
      
    insert opportunitiesList;
    opportunitiesList = [
        SELECT Id
        FROM Opportunity
        //WHERE AccountName = 'Test'
        ];
        
    insert tasksList;
    tasksList = [
        SELECT Id, WhatId
        FROM Task
        WHERE Id = :tasksList[0].WhatId
        ];
        
    system.assertEquals(tasksList[0].WhatId, opportunitiesList[0].Id);
    
    Test.stopTest();
    }
*/
}