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
Travis Lee 6Travis Lee 6 

Invocable Apex Class Struggles

Hey everyone,

I'm trying to create a class that I can invoke with a process but I'm having some trouble. The process exists on the Opportunity and needs to invoke a class that will unsync the current quote. I found a blog post that gave me a headstart on the framework for the class but apparently there's a conflict between future and invocable methods and it definitely must be invocable. I'm not a very experienced developer so any advice would be appreciated! Code below
 
public class QuoteAutoSyncUtil
{
    @future
    public static void syncQuote(Map<Id, Id> quoteMap)
    {
        Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>();
        for(Id currentQuote : quoteMap.keyset())
        {
            Opportunity opp = new Opportunity();
            opp.Id = quoteMap.get(currentQuote);
            opp.SyncedQuoteId = currentQuote;
            oppMap.put(opp.Id, opp);
        }
        update oppMap.values();
    }
}
Thanks!

Travis
 
NagendraNagendra (Salesforce Developers) 
Hi Travis,

Try the code below which works perfectly for me.
public class QuoteAutoSyncUtil
    {
        @future
        public static void syncQuote(Map<Id, Id> quoteMap)
        {
            List<Opportunity> oppList = new List<Opportunity>();
           
            for(Id currentQuote : quoteMap.keyset())
            {
                Opportunity opp = new Opportunity();
                opp.Id = quoteMap.get(currentQuote);
                opp.SyncedQuoteId = currentQuote;
                oppList.add(opp);
            }
           
            Integer oppSize = oppList.size();
            update oppList[oppSize -1 ];
     
        }
       
    }

Please mark this as best answer if it's resolved.

Best Regards,
Nagendra.P
Travis Lee 6Travis Lee 6
Hey Nagendra,

Yes, the code works perfectly as is, but I need to make it invocable through Process Builder which means I have to add the @Invocable annotation, no? When doing this and attempting to save, I get an error notifying me that future methods cannot also be invocable methods. Also, when attempting to replace future with invocable I get errors that reference the Map on line 4 that I'm not very familiar with. Does that make sense?

Regards,

Travis
Travis Lee 6Travis Lee 6
Maybe this will help? I think the root of the issue is that the Map<Id, Id> parameter conflicts with an invocable method? See below for what I've got so far:
 
public class QuoteAutoSyncUtil
{
    @InvocableMethod
    public static void syncQuote(List<Quote> quoteList)
    {
        Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>();
        for(Id currentQuote : quoteList.keyset())
        {
            Opportunity opp = new Opportunity();
            opp.Id = quoteList.get(currentQuote);
            opp.SyncedQuoteId = currentQuote;
            oppMap.put(opp.Id, opp);
        }
        update oppMap.values();
    }
}