Invocable Method Question in Salesforce Apex

92 views Asked by At

I am very new to Apex development, and I am trying to pass a list of strings from records in bulk from a Salesforce flow, but can't get the Apex script to work. If I try invoking from the external flow, the code doesn't run. But if I debug the code block from within the invocable method, it runs... Would anyone be able to provide some insight?

public without sharing class trac_GuidestarOrgUpdateBatch {
@InvocableMethod(Label='Update Bulk Organisations' Description='Updates list of orgs returned from guidestar.' Category='Account')
public void<String> getBulkOrganisations(List<String> searchKeywords){
    
    //Get search list
    List<Account> search = [SELECT EIN__c, Primary_Issue_Area__c FROM Account WHERE EIN__c =: searchKeywords];
    
    //Initialize account list to update
    List<Account> accountList = new List<Account>();
    
    for (Account acct : search) {
        acct.Primary_Issue_Area__c = 'New test';
        accountList.add(acct);
    }
    Database.update(accountList,false);    
}

Screenshot from flow

2

There are 2 answers

0
eyescream On

Are you sure it doesn't execute? Open Developer Console (or Setup -> Debug Logs and start tracking yourself), execute your screen flow, see if there's a log generated with your code in it. Examine the account that it was supposed to find, see if the value changed (or if the value is already "New test" - has the lastmodifieddate, lastmodifiedby changed)

The big problem I see is Database.update(accountList,false);.

Either make a normal update accountList; (and you may notice it throws errors). Or you'd process what Database.update returns and do... something (create a Task for each account that failed to update?). Something like https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database_saveresult.htm

0
Michael Schirger On

There are a few things that stick out that would prevent the code posted from compiling

  1. invocable methods must be static.
  2. void<String> is not a valid return type. Based on the rest of the code I believe that void will work.
  3. There is a missing curly bracket at the end of the snippet. Right now the closing bracket closes the method but does there is nothing closing the class.

The resulting updates make the code snippet look like this:

public without sharing class trac_GuidestarOrgUpdateBatch {
    @InvocableMethod(Label='Update Bulk Organisations' Description='Updates list of orgs returned from guidestar.' Category='Account')
    public static void getBulkOrganisations(List<String> searchKeywords){
    
        //Get search list
        List<Account> search = [SELECT EIN__c, Primary_Issue_Area__c FROM Account WHERE EIN__c =: searchKeywords];
    
        //Initialize account list to update
        List<Account> accountList = new List<Account>();
    
        for (Account acct : search) {
           acct.Primary_Issue_Area__c = 'New test';
           accountList.add(acct);
        }
        Database.update(accountList,false);    
   }
}

Does it work after cleaning up the syntax errors?