You need to sign in to do that
Don't have an account?
find values in string and insert them using loop
I am helping out a nonprofit and wondering if a more experienced Apex developer can help me finish this change to their code? I could probably even pay for an hour of your time if need be. Here is what I need to do:
Project: Parse string and insert opportunity line items for each value
Example of string to parse (quantity, product, totalprice):
| 1, donation-variable, 50 | 2, ticket-individual, 500 | 1, publication-schools, 25
Desired result from example: 3 opportunity line items (opportunity products) inserted:
OpportunityID = (provided by another piece of code already written)
Quantity = 1
PricebookEntryID = (provided)
TotalPrice = 50
ServiceDate = (provided)
OpportunityID = (provided by another piece of code already written)
Quantity = 2
PricebookEntryID = (provided)
TotalPrice = 500
ServiceDate = (provided)
OpportunityID = (provided by another piece of code already written)
Quantity = 1
PricebookEntryID = (provided)
TotalPrice = 25
ServiceDate = (provided)
I think a loop would need to be performed to grab and insert each item in the string. Obviously, the values in the string could be of any existing products, quantity and price. This block of code will be added to an existing Apex class right after the Opportunity is inserted.
Let me know if you can help me out. Thanks.
Jenn
If I'm reading your requirements correctly, I recommend using the String.split() method, and having a cache of products. Here's some untested pseudocode that I think should get you started...
Map<String, Product> prodMap = new Map<String, Product>();
String[] items = input.split('|');
for (String item : items) {
String[] fields = item.split(',');
String quantity = fields[0].trim();
String product = fields[1].trim();
String totalPrice = fields[2].trim();
Product p = prodMap.get(product);
if (p == null) {
p = [select ... from Product ... limit 1];
prodMap.insert(product, p);
}
// do whatever you need to do with the parsed info
}