+ Start a Discussion
Antonio Marques 13Antonio Marques 13 

Getting field from object map

Hey developers,

I'm little confused trying to access a field in this object, could you help me? 

I have the following sentence in the code:

Map<String, Object> tester = exampleFunction();

When I get the response using "system.debug(tester)" I get the following structure:

{cartId=222222XX, 
messages=(cc_bean_Message:[classToAppend=couponMessagingSection-Error, 
labelId=CouponAddMsg_MaxUseExceeded, 
messageId=null])
}

What I am trying to access is the value of "labelId", how can I do this? 
Best Answer chosen by Antonio Marques 13
OFröhlichOFröhlich
Hi,

here a simple example:

List<Account> ls = [select Id,Name from Account limit 2];
Map<Id, Account> m = new Map<Id, Account>(ls);
system.debug (m);

// for a special id (map key)
system.debug (m.get(<account id>).name);

// through a loop
for (account v : m.values()) {
system.debug (v.name);
}

If this helps, please mark as Best Answer to help others too.

All Answers

OFröhlichOFröhlich
Hi,

here a simple example:

List<Account> ls = [select Id,Name from Account limit 2];
Map<Id, Account> m = new Map<Id, Account>(ls);
system.debug (m);

// for a special id (map key)
system.debug (m.get(<account id>).name);

// through a loop
for (account v : m.values()) {
system.debug (v.name);
}

If this helps, please mark as Best Answer to help others too.
This was selected as the best answer
sachinarorasfsachinarorasf
Hi Antonio,

Please have a look at the map concept

List<Account> ls = [select Id,Name from Account];
Map<String, Account> m = new Map<String, Account>(ls);
System.debug('Map..' + m);

Also, go throw the below link.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com