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
Bryan TelfordBryan Telford 

How to assign variables in a FOR loop for parsing XML

I have the below FOR loop, which gets the attribute value of 'descript' and assigns it to the adjDescription variable. The incoming xml actually has 6 repeating child elements under PRICING_DETAILS, each with a different value in 'descript'.  Using the System.debug statement I am able to see each of the 'descript' values, so I know it's correctly iterating. However, I need to assign each value to a variable, so I can use them for inserting the data. 

 for (Dom.XmlNode node : doc.getRootElement().getChildElement('PRICING_DETAILS', null).getChildElements()){
                 if(node.getAttributeValue('descript','') != null) {
                    adjDescription = node.getAttributeValue('descript', '');
                    System.debug(adjDescription)
                   
                 }
    
             }

// Insert the data.
 NYLX_Lock_Data__c lockdata = new NYLX_Lock_Data__c(
     NYLX_Adjustment_Desc_1__c = adjDescription,
     NYLX_Adjustment_Desc_2__c = adjDescription
      );  
     insert lockdata; 

The above code for the insert is not working because it assigns the same adjDescrpition value for both. How do I increment the adjDescription variable in the FOR loop, so I that I am able to assign each one when I insert the data?  For reference here is the relevant part in the debug log showing each value from the loop.

12:58:35.12 (625245897)|USER_DEBUG|[194]|DEBUG|FCLS LTV / FICO Bump
12:58:35.12 (625303655)|USER_DEBUG|[194]|DEBUG|LTV Range: FICO >= 740
12:58:35.12 (625349569)|USER_DEBUG|[194]|DEBUG|Condo: LTV > 75%
12:58:35.12 (625394298)|USER_DEBUG|[194]|DEBUG|FCLS Regional Subsidy TX
12:58:35.12 (625438759)|USER_DEBUG|[194]|DEBUG|FCLS - Purchase Special
12:58:35.12 (625488311)|USER_DEBUG|[194]|DEBUG|SRP Adjuster

Thanks for any help!
Best Answer chosen by Bryan Telford
Mahesh DMahesh D
Hi Bryan,

You can try the below code:
 
List<String> adList = new List<String>();
 for (Dom.XmlNode node : doc.getRootElement().getChildElement('PRICING_DETAILS', null).getChildElements()){
	if(node.getAttributeValue('descript','') != null) {
		adList.add(node.getAttributeValue('descript', ''));
		System.debug(adList)
	}
}

// Insert the data.
 NYLX_Lock_Data__c lockdata = new NYLX_Lock_Data__c(
     NYLX_Adjustment_Desc_1__c = adList.get(0),
     NYLX_Adjustment_Desc_2__c = adList.get(1)
      );  
     insert lockdata;

Please do let me know if it helps.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Bryan,

You can try the below code:
 
List<String> adList = new List<String>();
 for (Dom.XmlNode node : doc.getRootElement().getChildElement('PRICING_DETAILS', null).getChildElements()){
	if(node.getAttributeValue('descript','') != null) {
		adList.add(node.getAttributeValue('descript', ''));
		System.debug(adList)
	}
}

// Insert the data.
 NYLX_Lock_Data__c lockdata = new NYLX_Lock_Data__c(
     NYLX_Adjustment_Desc_1__c = adList.get(0),
     NYLX_Adjustment_Desc_2__c = adList.get(1)
      );  
     insert lockdata;

Please do let me know if it helps.

Regards,
Mahesh
This was selected as the best answer
Bryan TelfordBryan Telford
Hi Mahesh, 

This worked great! Can I ask a follow-up question? The incoming XML may vary on the number of child elements under PRICING_DETAILS. There can be up to a total of 8. So let's assume I have the following for my insert logic to account for all 8 possibilities.

    NYLX_Adjustment_Desc_1__c = adjList.get(0),
    NYLX_Adjustment_Desc_2__c = adjList.get(1),
    NYLX_Adjustment_Desc_3__c = adjList.get(2),
    NYLX_Adjustment_Desc_4__c = adjList.get(3),
    NYLX_Adjustment_Desc_5__c = adjList.get(4),
    NYLX_Adjustment_Desc_6__c = adjList.get(5),
    NYLX_Adjustment_Desc_7__c = adjList.get(6),
    NYLX_Adjustment_Desc_8__c = adjList.get(7)

When it gets to index 6, I get an "List index out of bounds" error. Do you know how to resolve that?

13:22:57.12 (561761763)|USER_DEBUG|[195]|DEBUG|(FCLS LTV / FICO Bump, LTV Range: FICO >= 740)
13:22:57.12 (561827921)|USER_DEBUG|[195]|DEBUG|(FCLS LTV / FICO Bump, LTV Range: FICO >= 740, Condo: LTV > 75%)
13:22:57.12 (561892571)|USER_DEBUG|[195]|DEBUG|(FCLS LTV / FICO Bump, LTV Range: FICO >= 740, Condo: LTV > 75%, FCLS Regional Subsidy TX)
13:22:57.12 (561956773)|USER_DEBUG|[195]|DEBUG|(FCLS LTV / FICO Bump, LTV Range: FICO >= 740, Condo: LTV > 75%, FCLS Regional Subsidy TX, FCLS - Purchase Special)
13:22:57.12 (562021712)|USER_DEBUG|[195]|DEBUG|(FCLS LTV / FICO Bump, LTV Range: FICO >= 740, Condo: LTV > 75%, FCLS Regional Subsidy TX, FCLS - Purchase Special, SRP Adjuster)
13:22:57.12 (570032356)|FATAL_ERROR|System.ListException: List index out of bounds: 6


 
Mahesh DMahesh D
Hi Bryan,

Please check the below code:
 
List<String> adList = new List<String>();
 for (Dom.XmlNode node : doc.getRootElement().getChildElement('PRICING_DETAILS', null).getChildElements()){
	if(node.getAttributeValue('descript','') != null) {
		adList.add(node.getAttributeValue('descript', ''));
		System.debug(adList)
	}
}

// Insert the data.
 NYLX_Lock_Data__c lockdata = new NYLX_Lock_Data__c();
 
 for(Integer i = 1; i <= adList.size(); i++) {
	lockdata.put('NYLX_Adjustment_Desc_'+i+'__c', adList.get(i-1));
 }
  
 insert lockdata;

I verified the similar example in my DE environement and it looks good.
 
List<Integer> intList = new List<Integer>();

intList.add(1);
intList.add(2);

Account acc = new Account();
acc.Name = 'Test Acc';
for(Integer i=1; i<=intList.size(); i++) {
    acc.put('Field_'+i+'__c', intList.get(i-1));
}
insert acc;

Please let me know if it helps you.

Regards,
Mahesh
Bryan TelfordBryan Telford
Thank you, Mahesh! That worked. Have a great day.