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
Ben Merton 15Ben Merton 15 

Using the process builder with the following Apex Code

I don't seem to be able to call the following class from the Process Builder:

I know this could be written as a trigger, but I would prefer to do this from the Process Builder if that is possible as it is easier to manage all the Process flows from here.  Does this makes sense?

How can I modify this code to allow it to be called from the Process Builder?
 
public class POContactsController{
@InvocableMethod(Label='Insert PO Contacts')


    string poid;
    string vendorId;
    string contactid;
    
    public pageReference POContacts()
    {
       //Ensures that there is a Vendor on the PO or returns an error
       
        if (string.IsBlank(vendorid))
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, 'No Vendor selected on the Work Order'));
            return null;
        }
        
        //Deletes any items if they already exist
        
          List<unifize__PO_Contact__c> existingpocontacts = [select ID from unifize__PO_Contact__c where unifize__Purchase_Order__c = :poid];
        if(existingpocontacts.size() != 0)
        {
        delete existingpocontacts;
        }
        
        //Checks that there are Contacts assigned to POs on the related Contacts and returns error if not
        
        List<Contact> Contactlist = [select ID, Email, MobilePhone, Phone, Name FROM Contact where unifize__Purchase_Orders__c=TRUE AND Accountid = :VendorId];
        if (Contactlist.size() == 0)
        {
        ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, 'No Contacts assigned to receive POs for this Vendor'));
        System.debug('Contact List Size' + Contactlist.size());
        return null;
        }
         
        List<unifize__PO_Contact__c> POContactList = new List<unifize__PO_Contact__c>();
        
        for (Contact POContactItem : ContactList)
        {
        
        unifize__PO_Contact__c newpocontact = new unifize__PO_Contact__c();
        newpocontact.unifize__Contact__c=contactid;
     
            POContactList.add(newpocontact);
            
        }
        if (POContactList.size() > 0)
        {
            insert POContactList;
        }
        return new PageReference('/' + poId);
    }
}

 
Dorian Sutton 9Dorian Sutton 9
It depends on what you're trying to achieve with the Process you are writing - you can't use a Process to redirect the user to another page or to write Apex Page Messages.

Invocable Methods need to be static and can only return a List of primitives, sObjects or user defined types - see here for more details:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm