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 

Translating Maps to Lists in Apex?

Hey there,

I'm trying to create a class that I can invoke with a process. The process exists on the Opportunity and needs to invoke a class that will unsync the current quote. I'm a new developer, but this is what I've researched so far. Based on this blog post (http://salesforcekings.blogspot.com/2014/09/how-to-automatically-sync-new-quote.html), I can't use a trigger. I found an example of a class that I think will accomplish what I need, but it's a future class (not required for my scenario, I don't think). Here is that example that I'm pilfering:
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();
    }
}

Since I need it to be invocable by a process, I believe I need to include the @InvocableMethod annotation. When I do, I get an error that says the Map<Id, Id> is an unsupported parameter type and I have to use List instead. So I started switching the Maps over to Lists and this is where I get stuck because the "get" parameter on line 11 below isn't compatible with List methods I guess? This is where my knowledge starts to drop off.
 
public class QuoteAutoSyncUtil
    {
        @InvocableMethod
        public static void syncQuote(List<Quote> quoteList)
        {
            List<Opportunity> oppList = new List<Opportunity>();
           
            for(Id currentQuote : quoteList.keyset())
            {
                Opportunity opp = new Opportunity();
                opp.Id = quoteList.get(currentQuote);
                opp.SyncedQuoteId = currentQuote;
                oppList.add(opp);
            }
           
            Integer oppSize = oppList.size();
            update oppList[oppSize -1 ];
     
        }
       
    }

Any advice on the code above would be great, but I'd also like to understand a bit more about why I'm experiencing these errors with Maps vs. Lists. Thanks in advance!

Travis
Best Answer chosen by Travis Lee 6
Nayana KNayana K
public class QuoteAutoSyncUtil
    {
        @InvocableMethod
        public static void syncQuote(List<Quote> quoteList)
        {
            List<Opportunity> oppList = new List<Opportunity>();
           
            for(Quote currentQuote : quoteList)
            {
                Opportunity opp = new Opportunity();
                opp.Id = currentQuote.OpportunityId;
                opp.SyncedQuoteId = currentQuote.Id;
                oppList.add(opp);
            }
           
            
            update oppList;
     
        }
       
    }

List.get(index) ...using get for list is gettting an item on ith index. So it was syntax error in your code.

All Answers

Nayana KNayana K
public class QuoteAutoSyncUtil
    {
        @InvocableMethod
        public static void syncQuote(List<Quote> quoteList)
        {
            List<Opportunity> oppList = new List<Opportunity>();
           
            for(Quote currentQuote : quoteList)
            {
                Opportunity opp = new Opportunity();
                opp.Id = currentQuote.OpportunityId;
                opp.SyncedQuoteId = currentQuote.Id;
                oppList.add(opp);
            }
           
            
            update oppList;
     
        }
       
    }

List.get(index) ...using get for list is gettting an item on ith index. So it was syntax error in your code.
This was selected as the best answer
Travis Lee 6Travis Lee 6
Thanks Nayana! I appreciate the explanation. I've been getting thrown into the deep end recently when it comes to code projects so this is a life saver.