简体   繁体   中英

unable to get Salesforce Apex map to list

seeing strange behaviour that I cannot see a solution for. Any suggestions appreciated. I have a map that is definitely populated with a key-value pair. I then try to pass the map value to a list per provided map method prior to updating. Immediately I do this, for some reason I lose the values on the record just passed to list, and in turn cannot update it. Code and debug log follow;

code (debug lines removed):

if(!posMap.isEmpty()){
   List<Position__c> posUpdates = new List<Position__c>();
   posUpdates = posMap.values();
   update posUpdates;
}

debug log:

    USER_DEBUG|[403]|DEBUG|posUpdate record -->Position__c:{Affiliation_Final__c=a03N0000000mQEKIA2, Position_Type__c=Warden, Id=a07N0000000iA6lIAE}
11:21:17.825 (825365000)|SYSTEM_METHOD_EXIT|[403]|System.debug(ANY)
11:21:17.825 (825386000)|SYSTEM_METHOD_ENTRY|[404]|String.valueOf(Object)
11:21:17.825 (825402000)|SYSTEM_METHOD_EXIT|[404]|String.valueOf(Object)
11:21:17.825 (825413000)|SYSTEM_METHOD_ENTRY|[404]|System.debug(ANY)
11:21:17.825 (825418000)|USER_DEBUG|[404]|DEBUG|id ->null
11:21:17.825 (825422000)|SYSTEM_METHOD_EXIT|[404]|System.debug(ANY)
11:21:17.825 (825439000)|SYSTEM_METHOD_ENTRY|[405]|String.valueOf(Object)
11:21:17.825 (825453000)|SYSTEM_METHOD_EXIT|[405]|String.valueOf(Object)
11:21:17.825 (825464000)|SYSTEM_METHOD_ENTRY|[405]|System.debug(ANY)
11:21:17.825 (825468000)|USER_DEBUG|[405]|DEBUG|Affiliation_Final__c ->null
11:21:17.825 (825472000)|SYSTEM_METHOD_EXIT|[405]|System.debug(ANY)
11:21:17.825 (825516000)|DML_BEGIN|[406]|Op:Update|Type:Position__c|Rows:2
11:21:17.826 (826821000)|DML_END|[406]
11:21:17.826 (826971000)|EXCEPTION_THROWN|[406]|System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified

The error indicates that the records you have in posMap have not been retrieved from or inserted into the database.

An upsert operation will insert those which are not in the database yet and update those that are.

upsert posMap.values();

If the records exist in the data base try update posMap.values(); its not necessary check if map is empty, else change update to insert or upsert

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM