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

Writing an Inbound Handler to read XML
All I am writing an Inbound Message Class to handle XML in the body of the e-mail. I am struggling with one error and I am not sure how to resolve it. Any assistance would be appreciated. Below is my class. My Error is on line 95. It is Method Does Not Exist or Incorrect Signature: getDecodedString(System.XMLStreamReader) I understand that it is missing an element before 'reader', btu honestly I am not sure what it should be.
/**
* Email services are automated processes that use Apex classes
* to process the contents, headers, and attachments of inbound
* email.
*/
global class Email_CustomerRecord implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
List<Account> act1;
try
{
String messageBody = '';
String action = '';
String cust_num = '';
String name = '';
String cust_seq = '';
String addr1 = '';
String addr2 = '';
String addr3 = '';
String addr4 = '';
String city = '';
String CusCounty = '';
String state = '';
String country = '';
String zip = '';
messageBody = email.plainTextBody;
messageBody = messageBody.trim();
messageBody = messageBody.substring(messageBody.indexof('<?xml version="1.0" encoding="UTF-8"?>'),messageBody.indexOf('</CustomerRecord>')+12);
Action = readXMLelement(messageBody,'action');
String cn = readXMLelement(messageBody,'cust_num');
List<Account> actl = [SELECT Id, Customer_Number__c, Name, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry
FROM Account
WHERE Customer_Number__c =: cn
];
if(Action == 'add')
{
Account act = new Account();
act.Customer_Number__c = readXMLelement(messageBody,'cust_num');
act.Name = readXMLelement(messageBody,'name');
act.BillingStreet = readXMLelement(messageBody,'addr1') + readXMLelement(messageBody,'addr2') + readXMLelement(messageBody,'addr3') + readXMLelement(messageBody,'addr4');
act.BillingCity = readXMLelement(messageBody,'city');
act.BillingState = readXMLelement(messageBody,'state');
act.BillingPostalCode = readXMLelement(messageBody,'zip');
act.BillingCountry = readXMLelement(messageBody,'country');
insert act;
}
if(Action == 'modify')
{
for(Account actList : act1)
{
actList.Name = readXMLelement(messageBody,'name');
actList.BillingStreet = readXMLelement(messageBody,'addr1') + readXMLelement(messageBody,'addr2') + readXMLelement(messageBody,'addr3') + readXMLelement(messageBody,'addr4');
actList.BillingCity = readXMLelement(messageBody,'city');
actList.BillingState = readXMLelement(messageBody,'state');
actList.BillingPostalCode = readXMLelement(messageBody,'zip');
actList.BillingCountry = readXMLelement(messageBody,'country');
}
update act1;
}
if(Action == 'delete')
{
delete act1;
}
}
catch(exception e)
{
}
return result;
}
public static String readXMLelement(String xml, String element)
{
String elementValue = 'NOT FOUND';
try
{
Xmlstreamreader reader = new Xmlstreamreader(xml);
while (reader.hasNext())
{
if (reader.getEventType() == XmlTag.START_ELEMENT && reader.getLocalName() == element)
{
System.debug('Found SID');
reader.next();
elementValue = getDecodedString(reader); <---Error Method Does Not Exist or Incorrect Signature: getDecodedString(System.XMLStreamReader)
}
reader.next();
}
return elementValue;
}
catch(exception e)
{
String err;
err = e.getMessage();
return err;
}
}
}
Thank you for your assistance.
Hi! I'm happy I can help :)
You are right, the problem is the list variable act1 ...it should be actl:
In order to get 100 % coverage you can add something like this to your test methods:
Cheers!
All Answers
Hi! I assume you are using the approach here: http://salesforceapexcodecorner.blogspot.de/2011/11/read-xml-and-insert-data-in-to.html
It seems that you have to implement an additional method called getDecodedString:
That solved my error, however I am not sure that the XML is reading into the system properly. I wrote a test class for this and it is not reading in all of the elements. Ex: not reading action in.
Here is my test class:
it's a small mistake in the indexation (should be 17 instead of 12) :
First off thank you for that. Your help is really appreciated! One more thing to get my code covereage to 100%. The last thing I do not have covered is part of the the 'modify' section:
if(Action == 'modify')
{
for(Account actList : act1)
{
actList.Name = readXMLelement(messageBody,'name');
actList.BillingStreet = readXMLelement(messageBody,'addr1') + readXMLelement(messageBody,'addr2') + readXMLelement(messageBody,'addr3') + readXMLelement(messageBody,'addr4');
actList.BillingCity = readXMLelement(messageBody,'city');
actList.BillingState = readXMLelement(messageBody,'state');
actList.BillingPostalCode = readXMLelement(messageBody,'zip');
actList.BillingCountry = readXMLelement(messageBody,'country');
}
update act1;
}
I know there is only something small but I cannot seem to get it.
Hi! I'm happy I can help :)
You are right, the problem is the list variable act1 ...it should be actl:
In order to get 100 % coverage you can add something like this to your test methods:
Cheers!
Once again thank you for all of your help!
Hello,
I am doing something very similar. In my case I am writing an outbound BOD to fire my xml and pick up an object and its related objects (related list records). To this end I am writing my class to handle the lifting but I am running into a few walls. For one, I keep getting an error message stating that my field does not exist, but in this case I am not sure why. The items do reside object. Here is my posted code: