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
sfdcIssfdcIs 

how to pass sObject from a list to a map

my acctMap value is an Account sObject.  I am trying to put the sObject from List<Account> aNewList which is the collection from Trigger.new into this acctMap. How can I do this? I tried with the get.sObjectType method but it's not working

 

acctsMap.put(aOldList.get(i).Id,aNewList.getSObjectType());

 

 

public static void updateContactOptOutSettings(List<Account> aNewList, List<Account> aOldList )
{
for(Integer i = 0; i < aNewList.size(); i++)
{
Map<Id, Account> acctsMap= new Map<Id, Account>();
if ( (aOldList.get(i).Email_Opt_Out__c != aNewList.get(i).Email_Opt_Out__c || aOldList.get(i).Email_Fax_Out__c != aNewList.get(i).Email_Fax_Out__c ||
aOldList.get(i).Do_Not_Call__c!= aNewList.get(i).Do_Not_Call__c ))
{
acctsMap.put(aOldList.get(i).Id,aNewList.getSObjectType());
}
}

Best Answer chosen by Admin (Salesforce Developers) 
sfdcIssfdcIs

I was able to make it work using a nested loop.  You can't use use the Id from the sObject to get the value - it has to be the integer to grab the value associated with the index of the list. I was able to make it work doing this below

 

public static void UpdateContactOptOutSettings(List<Account> aNewList, List<Account> aOldList )
{
for(Account a: aNewList)
{
Map<Id, Account> acctsMap= new Map<Id, Account>();
List<Contact> updatedContacts = new List<Contact>();

for(integer i=0; i< aNewList.size(); i++)
{
if ( (aOldList.get(i).Email_Opt_Out__c != a.Email_Opt_Out__c ||
aOldList.get(i).Fax_Opt_Out__c != a.Fax_Opt_Out__c||
aOldList.get(i).Do_Not_Call__c!= a.Do_Not_Call__c ))
{
acctsMap.put(a.Id, a);
}


All Answers

BritishBoyinDCBritishBoyinDC

I would pass the trigger.oldmap into the class, and use that...something like this:

 

updateContactOptOutSettings(trigger.new, trigger.oldmap)


public static void updateContactOptOutSettings(List<Account> aNewList, Map<Id, Account> aOldListMap) {
for(Account a: trigger.new) {

Map<Id, Account> acctsMap= new Map<Id, Account>();

if ( 
(aOldListMap.get(a.Id).Email_Opt_Out__c != a.Email_Opt_Out__c 
|| aOldListMap.get(a.id).Email_Fax_Out__c != a.Email_Fax_Out__c 
|| aOldListMap.get(a.Id).Do_Not_Call__c!= a.Do_Not_Call__c )
) {
acctsMap.put(a.Id,a);
}
}

 

sfdcIssfdcIs

I was able to make it work using a nested loop.  You can't use use the Id from the sObject to get the value - it has to be the integer to grab the value associated with the index of the list. I was able to make it work doing this below

 

public static void UpdateContactOptOutSettings(List<Account> aNewList, List<Account> aOldList )
{
for(Account a: aNewList)
{
Map<Id, Account> acctsMap= new Map<Id, Account>();
List<Contact> updatedContacts = new List<Contact>();

for(integer i=0; i< aNewList.size(); i++)
{
if ( (aOldList.get(i).Email_Opt_Out__c != a.Email_Opt_Out__c ||
aOldList.get(i).Fax_Opt_Out__c != a.Fax_Opt_Out__c||
aOldList.get(i).Do_Not_Call__c!= a.Do_Not_Call__c ))
{
acctsMap.put(a.Id, a);
}


This was selected as the best answer
BritishBoyinDCBritishBoyinDC

Just to clarify - you can't refer to a list with integers, but trigger collections come with lists and maps - see here for explanation

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

 

So if you pass the map into the class from the trigger as well as the trigger.new list, you can use the synatx I used which is more reliable...

sfdcIssfdcIs

make sense I'll try using a map-  I agree that it will be more efficient that way. i was confused cuz i was invoking my class and method from the trigger itself so this statement: for(Account a: trigger.new)

caused this error

 

Loop variable must be of type sObject 

 

If I pass trigger.new to my class don't we have to pass into a sObject first? Just want to make sure i understand

 

 

BritishBoyinDCBritishBoyinDC

trigger.new and trigger.newmap are two separate collections - one is a list collection of the sObject from the trigger, so you can just loop through that, and the other is a map of Id to Account

 

so in the example, you could also do this

updateContactOptOutSettings(trigger.new, trigger.old, trigger.newmap, trigger.oldmap)


public static void updateContactOptOutSettings(List<Account> aNewList, List<Account> oldlist, Map<Id, Account> anewmap, Map<Id, Account> aOldMap) {

 trigger.new is a list of accounts, so you can just loop through it:

 

for (Account a: Trigger.New)

 

Trigger.oldmap is a map of <Id, Account> from the trigger (there is a newmap one as well) so you access the account in that map for that same Id in trigger.new using the get syntax