You need to sign in to do that
Don't have an account?
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!
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!
You can try the below code:
Please do let me know if it helps.
Regards,
Mahesh
All Answers
You can try the below code:
Please do let me know if it helps.
Regards,
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
Please check the below code:
I verified the similar example in my DE environement and it looks good.
Please let me know if it helps you.
Regards,
Mahesh