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
TerminusbotTerminusbot 

Apex Syntax: Need help with looping issue and simplifying code.

I am getting a SOAP response from a Apex callout. I am using the XMLStreamReader to find two tags. Once I get the values I am looking for I want to update the Account record fields with those values. When I look in the logs I am getting at least 100 loops through the XML response even after it finds my values. I would appreciate some help on the loop as well as updating my syntax once the values are found. 

Thanks! 
 
        String theSagittaIdValue; 
        String theSagittaCodeValue; 
        
        String bodyXML = res.getBody(); //grab the body of the response
        XmlStreamReader reader = new XmlStreamReader(bodyXML); //construct an XmlStreamReader object
        while (reader.hasNext()) { //while there are more XML events
            if (reader.getEventType() == XmlTag.START_ELEMENT) { //if this is the opening tag
                

                //Loop through to look for SagittaId
                if ('SagittaId' == reader.getlocalName()) {
                    while(reader.hasNext()) { //while there are more XML events
                        if (reader.getEventType() == XmlTag.END_ELEMENT) { //if this is the closing tag
                            break; //exist the loop
                        } else if (reader.getEventType() == XmlTag.CHARACTERS) { //if this is the content between the tags
                            theSagittaIdValue = reader.getText(); // grab the content
                        }
                        reader.next(); //advance to the next XML event
                    }
                }

                //Loop through to look for Sagitta Client Code 
                if ('SagittaCode' == reader.getLocalName()) { //if the tag is sagittacode
                    while(reader.hasNext()) { //while there are more XML events
                        if (reader.getEventType() == XmlTag.END_ELEMENT) { //if this is the closing tag
                            break; //exist the loop
                        } else if (reader.getEventType() == XmlTag.CHARACTERS) { //if this is the content between the tags
                            theSagittaCodeValue = reader.getText(); //grab the content
                            
                        }
                        reader.next(); //advance to the next XML event
                    }
                }
            }
        
        reader.next(); //advance to the next XML event
        
        //update the Account record fields with found values
            if(theSagittaIdValue != null) {
                accnt.SagittaID__c = theSagittaIdValue;
                update accnt;
            }

            if (theSagittaCodeValue != null) { 
                accnt.Client_Code__c = theSagittaCodeValue;
                update accnt;
            }



 
Best Answer chosen by Terminusbot
Patrick KaiserPatrick Kaiser
Hey,

In my opinion there is a simple solution. Just create two boolean variables, for the two targets. If you found one change the value of your boolean to true. Now you just have to modifie your loopheader and the magic should work.
boolean bFoundSagittaId = false;
boolean bFoundSagittaCode = false;

while (reader.hasNext() && !bFoundSagittaId && !bfoundSagittaCode) { ... }

 

All Answers

Patrick KaiserPatrick Kaiser
Hey,

In my opinion there is a simple solution. Just create two boolean variables, for the two targets. If you found one change the value of your boolean to true. Now you just have to modifie your loopheader and the magic should work.
boolean bFoundSagittaId = false;
boolean bFoundSagittaCode = false;

while (reader.hasNext() && !bFoundSagittaId && !bfoundSagittaCode) { ... }

 
This was selected as the best answer
TerminusbotTerminusbot
That worked. Thanks.