Tutorial Example-Multiple nested IF/REPLACE loops
Posted: 27 Feb 2015, 10:21
Multiple Nested If loops.
We're a manufacturer, and we have one customer in particular who likes to re-brand our products with their own name on their invoices and delivery notes etc.
For years we've been editing the description manually every order, so I thought i'd automate it, using nested if / replace loops. What a pain the backside that was to get working!
Anyway, I got it working, so I thought i'd share it simply because I found very few examples of deeply nested IFs on the net.
So,
example product descriptions
"OurProd1 100mm x 10m" becomes "TheirProd1 100mm x 10m"
"OurProd2 100mm x 10m" becomes "TheirProd2 100mm x 10m"
"Ours Clip 100mm x 10m" becomes "OursClip 100mm x 10m" (no name change, but prevents the next replace affecting it by removing the space)
"Ours Somename 100mm x 10m" becomes "Theirs Somename 100mm x 10m" (all other products drop our prefix and use their prefix.)
The following expression is applied to the SOP_ITEM.DESCRIPTION (or INVOICE_ITEM.DESCRIPTION on the invoice layout)
_______________________________________
S.O, P.O and despatch note find replace loop
_______________________________________
SALES_ORDER.ACCOUNT_REF <> "TheirAccountRef"
?
SOP_ITEM.DESCRIPTION ////////////////////////////////// if it's not their account ref, display the normal description
:
SOP_ITEM.DESCRIPTION LIKE "OurProd1%" /////////////////// if it is them, search for "OurProd1%" (% is any chars after the text)
?
Replace(SOP_ITEM.DESCRIPTION, "OurProd1", "TheirProd1") //// make the switch
:
SOP_ITEM.DESCRIPTION LIKE "OurProd2%" /////////////////// if no match found, search for "OurProd2%"
?
Replace(SOP_ITEM.DESCRIPTION, "OurProd2", "TheirProd2") //// make the switch
:
SOP_ITEM.DESCRIPTION LIKE "Ours Clip%" /////////////////// if no match found, search for "Ours Clip%"
?
Replace(SOP_ITEM.DESCRIPTION, "Ours Clip", "OursClip") ////// remove the space from "Ours Clip"
:
SOP_ITEM.DESCRIPTION LIKE "Ours %" ///////////////////// search for "Ours " (this is why we removed the space, to prevent the next line matching)
?
Replace(SOP_ITEM.DESCRIPTION, "Ours ", "Theirs ") ///////// make the switch
:
SOP_ITEM.DESCRIPTION //////////////////////////////// if nothing matches anywhere, fall back to the default description.
______________________________________
invoice find replace loop
______________________________________
INVOICE.ACCOUNT_REF <> "TheirAccountRef"
?
INVOICE_ITEM.DESCRIPTION
:
INVOICE_ITEM.DESCRIPTION LIKE "OurProd1%"
?
Replace(INVOICE_ITEM.DESCRIPTION, "OurProd1", "TheirProd1")
:
INVOICE_ITEM.DESCRIPTION LIKE "OurProd2%"
?
Replace(INVOICE_ITEM.DESCRIPTION, "OurProd2", "TheirProd2")
:
INVOICE_ITEM.DESCRIPTION LIKE "Ours Clip%"
?
Replace(INVOICE_ITEM.DESCRIPTION, "Ours Clip", "OursClip")
:
INVOICE_ITEM.DESCRIPTION LIKE "Ours %"
?
Replace(INVOICE_ITEM.DESCRIPTION, "Ours ", "Theirs ")
:
INVOICE_ITEM.DESCRIPTION
We're a manufacturer, and we have one customer in particular who likes to re-brand our products with their own name on their invoices and delivery notes etc.
For years we've been editing the description manually every order, so I thought i'd automate it, using nested if / replace loops. What a pain the backside that was to get working!
Anyway, I got it working, so I thought i'd share it simply because I found very few examples of deeply nested IFs on the net.
So,
example product descriptions
"OurProd1 100mm x 10m" becomes "TheirProd1 100mm x 10m"
"OurProd2 100mm x 10m" becomes "TheirProd2 100mm x 10m"
"Ours Clip 100mm x 10m" becomes "OursClip 100mm x 10m" (no name change, but prevents the next replace affecting it by removing the space)
"Ours Somename 100mm x 10m" becomes "Theirs Somename 100mm x 10m" (all other products drop our prefix and use their prefix.)
The following expression is applied to the SOP_ITEM.DESCRIPTION (or INVOICE_ITEM.DESCRIPTION on the invoice layout)
_______________________________________
S.O, P.O and despatch note find replace loop
_______________________________________
SALES_ORDER.ACCOUNT_REF <> "TheirAccountRef"
?
SOP_ITEM.DESCRIPTION ////////////////////////////////// if it's not their account ref, display the normal description
:
SOP_ITEM.DESCRIPTION LIKE "OurProd1%" /////////////////// if it is them, search for "OurProd1%" (% is any chars after the text)
?
Replace(SOP_ITEM.DESCRIPTION, "OurProd1", "TheirProd1") //// make the switch
:
SOP_ITEM.DESCRIPTION LIKE "OurProd2%" /////////////////// if no match found, search for "OurProd2%"
?
Replace(SOP_ITEM.DESCRIPTION, "OurProd2", "TheirProd2") //// make the switch
:
SOP_ITEM.DESCRIPTION LIKE "Ours Clip%" /////////////////// if no match found, search for "Ours Clip%"
?
Replace(SOP_ITEM.DESCRIPTION, "Ours Clip", "OursClip") ////// remove the space from "Ours Clip"
:
SOP_ITEM.DESCRIPTION LIKE "Ours %" ///////////////////// search for "Ours " (this is why we removed the space, to prevent the next line matching)
?
Replace(SOP_ITEM.DESCRIPTION, "Ours ", "Theirs ") ///////// make the switch
:
SOP_ITEM.DESCRIPTION //////////////////////////////// if nothing matches anywhere, fall back to the default description.
______________________________________
invoice find replace loop
______________________________________
INVOICE.ACCOUNT_REF <> "TheirAccountRef"
?
INVOICE_ITEM.DESCRIPTION
:
INVOICE_ITEM.DESCRIPTION LIKE "OurProd1%"
?
Replace(INVOICE_ITEM.DESCRIPTION, "OurProd1", "TheirProd1")
:
INVOICE_ITEM.DESCRIPTION LIKE "OurProd2%"
?
Replace(INVOICE_ITEM.DESCRIPTION, "OurProd2", "TheirProd2")
:
INVOICE_ITEM.DESCRIPTION LIKE "Ours Clip%"
?
Replace(INVOICE_ITEM.DESCRIPTION, "Ours Clip", "OursClip")
:
INVOICE_ITEM.DESCRIPTION LIKE "Ours %"
?
Replace(INVOICE_ITEM.DESCRIPTION, "Ours ", "Theirs ")
:
INVOICE_ITEM.DESCRIPTION