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
sieb4mesieb4me 

question on getting map field values

Hi,

I have code below, I wish to know how to get value of individual fields instead of using it like values() from map.

 

CODE:

 

for(Case c:caseemlist) {
for(EmailMessage em:c.EmailMessages) {
Emailmessage message = new Emailmessage (fromname = em.fromname,subject = em.subject,textbody = em.textbody,htmlbody = em.htmlbody,toaddress = em.toaddress,
fromaddress = em.fromaddress,ccaddress = em.ccaddress,status = em.status,messagedate = em.messagedate,ParentId=oldnewCaseIdmap.get(c.id));
//emlisttoinsert.add(message);
emailMessageMap.put(em.Id, message);
}
}

Attachment[] attList = new list <Attachment>();
Attachment[] insertAttList = new list <Attachment>();
if(emailMessageMap.values().size()>0) {
insert emailMessageMap.values();

 

Example in above, i wsh to insert individual values like fromaddress, ccaddress, how do i do it, i dont want to use values().

 

dphilldphill
EmailMessage eMsg = emailMessageMap.get(msgId);
eMsg.FieldName = 'some value';

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

Bhawani SharmaBhawani Sharma
You can loop through the map and insert values. Like:
for(Emailmessage message : emailMessageMap.values()) {
insert message;
}
dphilldphill
You definitely do NOT want to loop through and insert 1 at a time... You will easily run into DML governor limits doing something like that. I think he's just trying to access specific fields in the map.
Bhawani SharmaBhawani Sharma

Definitely, inserting data in loop is very bad practice. As the question was how to get data from map, that I answered. Insertion should be always in the list to handle governor limit.


dphill wrote:
You definitely do NOT want to loop through and insert 1 at a time... You will easily run into DML governor limits doing something like that. I think he's just trying to access specific fields in the map.

 

sieb4mesieb4me
that doest work
EmailMessage eMsg = emailMessageMap.get(em.Id);
or
EmailMessage eMsg = emailMessageMap.get(id);
also doesnt work
em.id is very issue is, how do we get values from this map?
Bhawani SharmaBhawani Sharma
Did you try this?
for(Emailmessage message : emailMessageMap.values()) {
System.debug('Message:::::' + message);
}
dphilldphill
I think you need a little more explanation of what you're actual problem is.
sieb4mesieb4me
I PUT THIS
system.debug(emailMessageMap.values());

AND IT SHOWS THIS


13:33:15:085 USER_DEBUG [40]|DEBUG|(EmailMessage:{Status=0, ParentId=500J0000001IBG7IAO, Subject=RE: YellowBook Escalation call to sync up, HtmlBody=<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsof

THIS STATEMENT IS BAD
insert emailMessageMap.values();

its causing heapsize to increase a lot, which is why we see the issue, i want to control that.

THIS
sieb4mesieb4me
all i need to know is syntax of how toy pull individual field values and not all values() from map.

emailMessageMap.put(em.Id, message);
}
}

if(emailMessageMap.values().size()>0) {
Bhawani SharmaBhawani Sharma
Did you try this?
for(Emailmessage message : emailMessageMap.values()) {
System.debug('Message:::::' + message);
}
sieb4mesieb4me
yes, i did, that did help.
however i realized how to get individual values from map, which was doing the for loop again getting value using id and it worked.

thanks