In addition of casting the Object at the first index of arr
to a map you also need to cast the Object that comes back from Data
to a Map<string, Object>
as well.
String jsonInput = '[{\n' +
' "Data" : ' +
'{ "height" : 5.5 , ' +
'"width" : 3.0 , ' +
'"depth" : 2.2 }\n' +
'},\n' +
'{\n' +
' "Data" : ' +
'{ "height" : 5.5 , ' +
'"width" : 3.0 , ' +
'"depth" : 2.2 }' +
'}]';
List<Object> arr =(List<Object>)JSON.deserializeUntyped(jsonInput);
Map<String, Object> dataNode = (Map<String, Object>)((Map<String, Object>)arr[0]).get('Data');
System.debug(dataNode);
As that is starting to get unwieldy due to the multiple casts it would be helpful to split it out to a few lines.
List<Object> arr =(List<Object>)JSON.deserializeUntyped(jsonInput);
Map<String, Object> firstMap = (Map<String, Object>)arr[0];
Map<String, Object> dataNodeMap = (Map<String, Object>)firstMap.get('Data');
System.debug(dataNodeMap);
As far as Apex is concerned, a picklist is just a string.
Your provided code has some issues with it. You're not querying your accounts for the child VIP_Type__c
records (which would would do here by using a parent-child subquery, a nested SELECT clause inside another SELECT clause).
The bigger issue though is that you seem to misunderstand what your query is giving you (or would be giving you).
If your query were something like [SELECT Id, (SELECT Id, VIP_Type__c FROM VIP_Types__r) FROM Account]
, then account.VIP_Types__r
is a List of SObjects, specifically, a List<VIP_Type__c>
.
The subquery doesn't give you just a single string, it gives you a list. Hence your error.
That aside, I think you're going about this the hard(er) way. The way I'd approach this would be something like (in pseudo-code)
// manual rollups like this which need to be updated when a child record is deleted
// need to have special care taken with the deletion bits
// In a nutshell, if we delete the last child record, the parent record's rollup should
// end up being blank
// We need to prepare to do that by default, since the second query won't return any results
// if the last related child is being deleted.
Map<Id, Account> accountsToUpdate = new Map<Id, Account>();
for each VIP_Type__c record{
add account id to a list
accountsToUpdate.put(accountId, new account(Id = accountId, VIP_Types__c = ''));
}
// A set naturally contains only unique values, so it's a better fit for what you're
// trying to do
Map<Id, Set<String>> accountIdToPicklistVals = new Map<Id, Set<String>>();
// I think querying the child object makes more sense here
// The single vs nested loop thing wouldn't really matter, but since I don't think
// there's really any benefit to having SOQL group the child records under each parent
// here, I'd opt for simplicity and brevity
for(vip_type :[SELECT AccountId, picklistField FROM VIP_Type__c WHERE AccountId IN :accountIds]){
if(account Id not in accountIdToPicklistVals map){
accountIdToPicklistVals.put(accountId, new Set<String>());
}
grab the set from the accountIdToPicklistVals map, and add vipType.picklistField to the set
}
for each id in the accountIdToPicklistVals map{
// String.join only works on lists, but we used a set to prevent duplicate values
// Lucky for us, there is a list constructor that takes a set
String uniqueVals = String.join(new List<String>(accountIdToPicklistVals.get(accountId)), ';');
accountsToUpdate.get(accountId).VIP_Types__c = uniqueVals;
}
update accountsToUpdate;
Best Answer
try with sObject instead of Contact since get and set methods are available in sObject class. Replace your
Contact objContact
withsObject objContact
in for loop. Also, try withoutObject obj
as it should work fine without casting it to Object type