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
Sricharan Puligundla 11Sricharan Puligundla 11 

Collections not working.Last value of Age in the XML is getting stored for all asset records.

I am calling a web service,getting response and updating mutiple asset records in Salesforce.Response XML is given below

In the XML we have multiple values for Age.First Age value must be stored in first asset record and so on.Issue is last value of Age in the xml is stored is getting stored for all Asset Records.
My Apex class code is given below.
global class AxedaDataItemBatch1 implements Database.Batchable<sObject>,Database.AllowsCallouts{
Public String query;

String email;

Id toUserId;

Id fromUserId;
    global Database.querylocator start(Database.BatchableContext BC)
    {
       //String query = 'Select Id from Asset where SerialNumber = 'SCLP0003'';          
            return Database.getQueryLocator(query);
    }
global void execute(Database.BatchableContext BC, List<sObject> scope){

        //List<Asset> myobjects = [ select Id from Asset where SerialNumber = 'SCLP0007' ];
        List<Asset> myobjects = [ select Id from Asset where Id In ('02i54000000DdVJAA0','02i54000000DdVKAA0','02i54000000DdVRAA0','02i54000000DdViAAK','02i54000000DdW4AAK')];
       
       
        AxedaSession asp = new AxedaSession();
        String SessionId = asp.SessionId();
        System.debug('session id'+SessionId);
       
        HttpRequest req = new HttpRequest();
        HTTPResponse res;
        String AssetId = '2381';
        //Set HTTPRequest Method 
        req.setMethod('POST');
       
        //String password = 'Cynosure793*';
        //System.debug('password is'+password);
      
        //Blob headerValue = Blob.valueOf(username + ':' + password); */
      
        //String authorizationHeader = 'NTLM ' + EncodingUtil.base64Encode(headerValue);
        //String authorizationHeader = 'NTLM ' + headerValue; 
        //System.debug('authorization header is'+authorizationHeader);
 
        //req.setEndpoint('https://cynosure.axeda.com/services/v2/rest/dataItem/findCurrentValues');
        req.setEndpoint('https://cynosure.axeda.com/services/v2/rest/dataItem/findHistoricalValues');   
        //req.setEndpoint('http://www.webservicex.com/stockquote.asmx');
        req.setMethod('POST');
        //req.setMethod('findByCriteria');
        //req.setHeader('Content-Type', 'text/xml; charset=UTF-8');
        req.setHeader('Content-Type', 'application/xml');
        //req.setHeader('Content-Type', 'charset=UTF-8');
        //req.setHeader('Content-Type','application/json');
        req.setHeader('x_axeda_wss_sessionid',SessionId);
        //req.setHeader('sessionId', MySessionID);
        //req.setHeader('Authorization', authorizationHeader);  
        //req.setHeader('SOAPAction', 'http://devwdapps.cynosure.com/XiPayWSConnector/XiPayWSConnector.XiPay.svc');
        //req.setHeader('SOAPAction','http://Primesys/XiPaySoap/message/IXiPay/Authorize');
        /*string b =   '<?xml version="1.0" encoding="UTF-8"?><v2:CurrentDataItemValueCriteria xmlns:v2="http://www.axeda.com/services/v2"'+
            ' xmlns:xsi="http://www.w3.org/2001/ XMLSchema-instance" sortAscending="true" sortPropertyName="Name">'+
                      '<v2:name>*</v2:name><v2:assetId>'+AssetId+'</v2:assetId>'+
                       '<v2:types/></v2:CurrentDataItemValueCriteria>';*/
        string b =   '<?xml version="1.0" encoding="UTF-8"?><v2:HistoricalDataItemValueCriteria xmlns:v2="http://www.axeda.com/services/v2"'+
            ' xmlns:xsi="http://www.w3.org/2001/ XMLSchema-instance" sortAscending="true" sortPropertyName="Name">'+
                      '<v2:assetId>'+AssetId+'</v2:assetId>'+
                       '<v2:dataItemIds/></v2:HistoricalDataItemValueCriteria>';   
        req.setBody(b);
        System.debug('b is'+b);
        System.debug('request is'+req);
        Http http = new Http();
        String response;
        try { 
          //Execute web service call here        
          res = http.send(req); 
          response = res.getBody();
          System.debug(res.toString());
          System.debug(res.getBody());
          System.debug('response is'+response);
          Dom.Document doc = new Dom.Document();
          doc.load(response);
          //Retrieve the root element for this document.
        Dom.XMLNode ordDtls = doc.getRootElement();
         //Dom.XmlNode dataitemvalues= ordDtls.getChildElement('dataItemValues','http://www.axeda.com/services/v2').getchildElement('dataItemValue','http://www.axeda.com/services/v2');
         Dom.XmlNode dataitemvalues= ordDtls.getChildElement('dataItemValues','http://www.axeda.com/services/v2');
            //String dataitemvalue = ordDtls.getChildElement('dataItemValue', 'http://www.axeda.com/services/v2').getText();
        // print out specific elements
  
        //for( Asset myobject : myobjects)
        for( List <Asset> myobject : [ select Id from Asset where Id In ('02i54000000DdVJAA0','02i54000000DdVKAA0','02i54000000DdVRAA0','02i54000000DdViAAK','02i54000000DdW4AAK')])
        {
            for(dom.XmlNode node : dataitemvalues.getChildElements())
            { 
             for(Integer i = 0;i<myobject.size();i++)
             {
        System.debug('dataitemvalues: ' + dataitemvalues);
        System.debug('dataitemvalues children' + dataitemvalues.getChildren());
        System.debug('size is'+myobject.size());        
            //for(dom.XmlNode node : dataitemvalues.getChildElements())//commented on 04192016
            //for(dom.Xmlnode node:dataitemvalues.getChildren())
            //{
              //for(Integer i = 0;i<node;i++) 
                if (node.getName() == 'dataItemValue')
                {
                String name =  node.getChildElement('dataItem','http://www.axeda.com/services/v2').getChildElement('name','http://www.axeda.com/services/v2').getText();  
                System.debug('name is'+name);
                    if ((name == 'FBackThigh') || (name == 'FFlank') || (name == 'FLowerAbdomen') || (name== 'FOther') || (name == 'FOtherAnatomy') || (name == 'FUpperAbdomen') || (name == 'MaleChests') || (name == 'MBackShoulder') || (name == 'MBackThigh') || (name == 'MCalves') || (name == 'MChest') || (name == 'MFlank') ||
                    (name == 'MLowerAbdomen') || (name == 'MOther') || (name == 'MOtherAnatomy') || (name == 'MUpperAbdomen') || (name =='SN') || (name == 'CalculationExpression') || (name  == 'FBackShoulder'))
                {
                   }
                else
                {
                   if (name == 'Age')
                   {
                     System.debug('inside if loop');
                     System.debug('value of i is'+i); 
                     //List<String> Agevalue = new List<String>(); 
                     String Agevalue;
                     Map<Integer, String> m = new Map<Integer, String>();
                     List<String> Agevalues = new List<String>(); 
                     //Agevalue.add(node.getChildElement('value','http://www.axeda.com/services/v2').getText());
                     Agevalue = node.getChildElement('value','http://www.axeda.com/services/v2').getText();
                     m.put(1,Agevalue);                      
                     System.debug('node child elemenode child element Agent Agenode child element Age value value'+Agevalue);
                     System.debug('map get value is'+m.get(1));
                     System.debug('map keyset is'+m.values());
                     Agevalues = m.values();
                     System.debug('Age Values are'+Agevalues); 
                     //for (String values : Agevalues) 
                     //{   
                     //myobjects[i].Age_Range__c = m.get(1);
                     myobject[i].Age_Range__c = Agevalue;
                     //update myobject; 
                     System.debug('Age Range is'+myobject[i].Age_Range__c);
                     //System.debug('age range index values are'+Agevalues[i]);   
                     //update myobject;   
                     //}  
                     //update myobject; 
                     //myobjects[i].Age_Range__c =  Agevalue;
                     if (myobjects.size()> 0)
                    {
                       //for(Integer i = 0;i<myobjects.size() - 1;i++) 
                       //{
                        //myobject.Age_Range__c = AgeValue;
                        //if (i < myobjects.size() - 1){
                        try
                        {   
                         //myobjects[i].Age_Range__c =  Agevalue.get(i);
                        }
                        catch(Exception e)
                        {
                          System.debug('Error Message is'+e); 
                        } 
                       // }
                        //System.debug('Age value is'+myobjects[i].Age_Range__c);
                     }
                                   
                     //}
                       
                     //update myobjects[i]; 
                   }
                         
                     
                     //System.debug('node child element value'+Agevalue); 
                    }//end of else
                   
       } //end of if
                    //update myobject;
                    //System.debug('node child element name'+name);
                     } //end of inner for loop
                      System.debug('My Object is'+myobject);
                       update myobject;//commnted this line on 04192016
                       //update myobjects[i];
                       //update myobjects;
                     } //end of for loop i
                    } //end of outer for loop
                     System.debug('My Objects are'+myobjects);
                    //myobjects.update(myobject);
                    //update myobjects;
           }
          catch(System.DMLException e) { 
           //Exception handling goes here....
           System.debug('error message is'+e);
          
        }
        System.debug('response is'+response);
       //return response;
     
        }
   
Sricharan Puligundla 11Sricharan Puligundla 11
How to loop through mutiple xml nodes and update multiple records in salesforce.I need help in this.

Thanks
Sricharan