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
Emily PhillipsEmily Phillips 

How can I get the value from this map?

I'm trying to assign the value from a map to an opportunity field. There isn't a relationship between the two objects: Opportunity and Custom_Object_1__c. Any help is appreciated!
 
public static void mapDemo(List<Opportunity> oppList){
        
        Set<Id> userSet = new Set<Id>();
        
        for(Opportunity opp: oppList){
            userSet.add(opp.CreatedById);
            userSet.add(opp.OwnerId);
        }
        
        Map<Id, Id> mapCustomObject = new Map<Id, Id>();
        
        List<Custom_Object_1__c> co1List = [SELECT Id, Special__c, User__c FROM Custom_Object_1__c WHERE User__c IN :userSet];
        
        for(Custom_Object_1__c co1 : co1List){
            mapCustomObject.put(co1.User__c, co1.Special__c);
        }
        System.debug('mapCustomObject: ' + mapCustomObject);
        for(Opportunity opp: oppList){
            opp.Test_Field__c = mapCustomObject.get(); /* assign Special__c here */
        }
    }

 
Best Answer chosen by Emily Phillips
AnkaiahAnkaiah (Salesforce Developers) 
Hi Emily,

What is the data type of Special__c field on Custom_Object_1__c object?

If it is text then modify the below lines of code.
 
Map<Id, string> mapCustomObject = new Map<Id, string>();
        
        List<Custom_Object_1__c> co1List = [SELECT Id, Special__c, User__c FROM Custom_Object_1__c WHERE User__c IN :userSet];
        
        for(Custom_Object_1__c co1 : co1List){
            mapCustomObject.put(co1.User__c, co1.Special__c);
        }
        System.debug('mapCustomObject: ' + mapCustomObject);
        for(Opportunity opp: oppList){
		if(mapCustomObject.containskey(opp.ownerid)){
            opp.Test_Field__c = mapCustomObject.get(opp.ownerid); /* assign Special__c here */
			}
        }

If this helps, please mark it as best answer.

Thanks!!
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Emily,

What is the data type of Special__c field on Custom_Object_1__c object?

If it is text then modify the below lines of code.
 
Map<Id, string> mapCustomObject = new Map<Id, string>();
        
        List<Custom_Object_1__c> co1List = [SELECT Id, Special__c, User__c FROM Custom_Object_1__c WHERE User__c IN :userSet];
        
        for(Custom_Object_1__c co1 : co1List){
            mapCustomObject.put(co1.User__c, co1.Special__c);
        }
        System.debug('mapCustomObject: ' + mapCustomObject);
        for(Opportunity opp: oppList){
		if(mapCustomObject.containskey(opp.ownerid)){
            opp.Test_Field__c = mapCustomObject.get(opp.ownerid); /* assign Special__c here */
			}
        }

If this helps, please mark it as best answer.

Thanks!!
 
This was selected as the best answer
Emily PhillipsEmily Phillips
Hi Ankaiah, 

The Special__c field is a lookup field to another custom object. Will your solution still work? 

Thank you!
AnkaiahAnkaiah (Salesforce Developers) 
Yes it will work.

Please let me know if any issues?

Thanks!!
Emily PhillipsEmily Phillips
Thank you, Ankaiah! I've marked your response as the Best Answer.