You need to sign in to do that
Don't have an account?

Need help with my apex method
Hi, I got my method to add one record to my custom sobject but now I am having trouble adding multiply records to my sobject with one email.
Method:
String delim = '---';
String[] emailBody = email.plainTextBody.split('\n', 0);
Account__c newAccount = new Account__c();
try{
for (Integer i=0; emailBody.size() != i; i++) {
String[] values = emailBody[i].split(delim, 2);
newAccount.put(values[0], values[1]);
}
}
catch (System.DmlException e)
{
System.debug('ERROR: Not able to create person:' + e);
}
insert newAccount;
In the body of the email, the message looks like this:
First Name__c --- John
Last Name__c --- Doe
Age__c --- 45
BirthDate__c --- 1/1/1955
I have been trying to add multiply account records to a single email and splitting each account with a <Start> and <End>. That way I can add 20 or more records with a single email. So the email body looks like this:
<Start>
First Name --- John
Last Name --- Doe
Age --- 45
BirthDate --- 1/1/1955
<End>
<Start>
First Name --- Steve
Last Name --- Doe
Age --- 45
BirthDate --- 1/1/1955
<End>
<Start>
First Name --- Peter
Last Name --- Doe
Age --- 45
BirthDate --- 1/1/1955
<End>
Thank you for any help and advice you can give.
Hi,
pass data in xml format and then try to read it using xmlStreamReader
for e.g.
<users> <user> <fname>Steve</fname> <lname>Doe</lname> <age>45</age> <bdate style="dd/MM/yyyy">1/1/1955</bdate> </user> <user> <fname>Steve</fname> <lname>Doe</lname> <age>45</age> <bdate style="dd/MM/yyyy">1/1/1955</bdate> </user> <user> <fname>Steve</fname> <lname>Doe</lname> <age>45</age> <bdate style="dd/MM/yyyy">1/1/1955</bdate> </user> <user> <fname>Steve</fname> <lname>Doe</lname> <age>45</age> <bdate style="dd/MM/yyyy">1/1/1955</bdate> </user> </users>
String xmlString = email.plainTextBody();
XmlStreamReader xsr = new XmlStreamReader(xmlString);
Wow thank you so much. I was wondering how the xmlsteamreader will add each record or does the xmlstreamreader let me pick the field type and the name that i am adding. So I can use the put(1,2).
http://java.sun.com/javaee/5/docs/tutorial/doc/bnbfl.html