+ Start a Discussion
Anu-cnpAnu-cnp 

Too many script statements: 200001 error

Hi

 

I've  a custom field in a custom object. I'm posting an xml file through external API. It is posted successfully. Now I want to map that data into Contact Object. I'm facing too many Script statements.

 

My code is:

 

 

trigger dmldata on CnPData__c (before insert,after insert) {
String dataxml;
    for(CnP__CnPData__c cpdata:Trigger.new){
         dataxml=cpdata.CnP__DataXML__c;
    }
    System.debug('hiiiiiiiii'+dataxml);
    XmlStreamReader reader= new XmlStreamReader(dataxml);
    System.debug('reader valueeee'+reader);
    while(reader.hasNext()) {
    System.debug('reader event nameee'+reader.getEventType());
     /*if (reader.getEventType() == XmlTag.START_DOCUMENT) {
     System.debug('local nameee'+reader.getLocalName());
    
}*/
}
 
}
My sample xml in the field is :
<?xml version="1.0" encoding="utf-8"?>
<Contact>
 <FirstName>anu</FirstName>
    <LastName>rrr</LastName>  
 </Contact>
One more, If I debug the reader.getEventType() it is showing the Start_Document. I want this value as START_ELEMENT. how can I get that?
If it START_DOCUMENT how can I proceed with that to read the values.
Thanks a ton in advance.

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

When reading an XML file, the root tag is the START_DOCUMENT target. Thus, when you are reading this XML file, "Contact" triggers START_DOCUMENT, because it is the root tag.

 

To make it a START_ELEMENT, you must change your XML:

 

 

<?xml version="1.0" encoding="UTF-8"?>
<Data>
 <Contact>
  <FirstName>John</FirstName>
  <LastName>Doe</LastName>
 </Contact>
</Data>

This way, Data will become the START_DOCUMENT/END_DOCUMENT event tag, and the rest will become start/end element tags. This would also allow you to design the code to support multiple Contact entries in a single pass.

 

All Answers

ca_peterson_oldca_peterson_old

You never call reader.next() so it keeps looping over the same element over and over again until the govenor limit kills it for executing 200k statements.

At the end of the while block you should call that method to advance to the next element.

Anu-cnpAnu-cnp

Hi Peterson,

 

Thanks for the reply. but sorry did not understand the answer. Can you modify it and post it again??/

 

 

And also, I want that event type is Start_Element, but it is showing the Start_document.What modification should I do to make that event type as element??

 

 

Thanks a ton.

 

 

Anu.

Anu-cnpAnu-cnp

Hi,

 

Clarified. Thank you.

 

But can you specify about start_document. I need this as Start_Element but it comes start_Document.

 

Thanks

Anu

sfdcfoxsfdcfox

When reading an XML file, the root tag is the START_DOCUMENT target. Thus, when you are reading this XML file, "Contact" triggers START_DOCUMENT, because it is the root tag.

 

To make it a START_ELEMENT, you must change your XML:

 

 

<?xml version="1.0" encoding="UTF-8"?>
<Data>
 <Contact>
  <FirstName>John</FirstName>
  <LastName>Doe</LastName>
 </Contact>
</Data>

This way, Data will become the START_DOCUMENT/END_DOCUMENT event tag, and the rest will become start/end element tags. This would also allow you to design the code to support multiple Contact entries in a single pass.

 

This was selected as the best answer