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
Anu-SFDCAnu-SFDC 

How to loop the XML tags

Hi,

 

 

Here is my code.

 

 

trigger dmldata on CnPData__c (before insert,before update) {
 Custom_Question__c[] question = new Custom_Question__c[0];
String dataxml;
  for(CnP__CnPData__c cpdata:Trigger.new){
         dataxml=cpdata.CnP__DataXML__c;
     }
  XmlStreamReader reader= new XmlStreamReader(dataxml);
   
                Custom_Question__c cq = parseQuestion(reader);
                question.add(cq);
                System.debug('questions after parsinggg'+question);
                
                insert question;
Custom_Question__c parseQuestion(XmlStreamReader reader5){
     Custom_Question__c qstn= new Custom_Question__c();
 
    while(reader5.hasNext()) {
     System.debug('event typpeeee in transaction'+reader5.getLocalName());
  if('CustomFieldList' == reader.getLocalName() && reader.getEventType() == XmlTag.END_ELEMENT) {
  
        break;
   }
        else if('FieldName' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
  List<CnP_Transaction__c> cnptransact = [Select Id,Name from CnP_Transaction__c where Name=:ordnum];
            qstn.Name = cnptransact[0].Name;
            qstn.Custom_Question__c= reader.getText();
            System.debug('Question  Nameeeeeeee'+qstn.Name);
   
          }
          }
        else if('FieldValue' == reader.getLocalName() && reader.getEventType() == XmlTag.START_ELEMENT){
          reader.next();                   
          if(reader.getEventType() == XmlTag.CHARACTERS) { 
 
            qstn.Answer__c= reader.getText();
            System.debug('Question  Answer'+qstn.Answer__c);
   
          }
          }
    reader5.next();
    }
    return qstn;
    }

 

}

 

 

And Here is my xml file.

 

 

<?xml version="1.0" encoding="utf-8"?>

<CnPTransactionData>

 

 

<CustomFieldList>

      <CustomField>

 

        <FieldName>Employer</FieldName>

        <FieldValue>Good Company</FieldValue>

      </CustomField>

      <CustomField>

        <FieldName>Occupation</FieldName>

        <FieldValue>Good Job</FieldValue>

      </CustomField>

      <CustomField>

        <FieldName>Place of Employment</FieldName>

        <FieldValue>Blacksburg, Virginia</FieldValue>

      </CustomField>

    </CustomFieldList>

 

</CnPTransactionData>

 

 

 

 

 

My problem is: I can insert a record in custom question easily. but I need to insert 3 records of Custom questions for a single order number(thatis transaction name). How can I loop the code so that 3 custom questions for a single order number has been araised

 

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 

All Answers

Afzal MohammadAfzal Mohammad
This was selected as the best answer
Anu-SFDCAnu-SFDC

Hi Afzal,

 

 

Thanks alot. I solved my problem with your link.

 

Thanks a  ton.

 

 

Anu