This article will give you some common use cases for advanced C# logic to use in the map.
1. Introduction
a. C# code in a map is where you can assign data to a field with some specific conditions or get/set special value.
b. A code in Map must have the “return exp;” statement. “exp” could be a variable, constant value, a function … Example:
return "ST";
c. "value" refers to the data being passed in from the source side. Example:
if(string.IsNullOrEmpty(value))
return value;
d. Example about the relation of field in structure
2. Basic code
a. Code supported by Simplify
Usage | Description | Example | Note 1233 |
getValueFromXpath(xpath) | Get value from source element by xpath | return getValueFromXpath("/Document/FGrp/Msg /DTM[DTM01='011']/DTM02"); | |
getValuesFromXpath(xpath) | Get list values of elements by xpath | return getValuesFromXpath("/HortonInvoice /IT/Trading_Partner_ID").Count.ToString(); | return a list of string |
getChildValue(xpath) | Get the value of a child element by xpath | var typeCode = getChildValue("Address_Type_Code"); | |
getSiblingValue(name) | Get value of the current source sibling element by name | var sku = getSiblingValue("sku"); | |
getTotalValueByQualifier (qualifierXpath) | Get values of elements by qualifier xpath then calculate the total value | return getTotalValueByQualifier("/Root/Implicit /Detail/CUSTOMER_ITEM_NUMBER") | |
getTotalValueFromXpath(xpath) | Get values of elements by xpath then calculate the total value | return getTotalValueFromXpath("/Root/Implicit /Detail/note.StartsWith("TRK-")PALLET_QUANTITY"); | |
getTradingPartnerName() | Get name of trading partner | if(getTradingPartnerName()=="Eaton") return ""; | |
GetConfigFromTradingPartner(name) | Get the configuration data of trading partner by name | var ediId = GetConfigFromTradingPartner("EdiId"); | |
getXrefValue(lkTableName, valueToCheck) | Get a value from a lookup table based on certain conditions and configurations | return getXrefValue("810 - Adjustment Type - TruckPro", value); | |
groupBy(groupValue, index) | Group the collection of destination elements based on a specific value | groupBy(getChildValue("Cust_Item_Number")); | no return data, index is number |
MapCustomerIdFromQbToShopify(id) | Map a customer ID from the QuickBooks platform to a corresponding ID used in Shopify | return MapCustomerIdFromQbToShopify(value); | |
MapCustomerIdFromShopifyToQb(id) | Map a customer ID from the Shopify platform to a corresponding ID used in QuickBooks | return MapCustomerIdFromShopifyToQb(value); | |
MapProductIdFromQbToShopify(id) | Map a product ID from the QuickBooks platform to a corresponding ID used in Shopify | return MapProductIdFromQbToShopify(value); | |
MapProductIdFromShopifyToQb(id) | Map a product ID from the Shopify platform to a corresponding ID used in QuickBooks | return MapProductIdFromShopifyToQb(value); | |
skip() | Skip to map this element | return skip(); | |
setValue(key, inputValue) | Set map variable. Update or assign a value to a specific key, this value can be used in other link(s) | setValue("Vend_Item", value); | no return data, inputValue can be anything (object) |
getValue(key) | Retrieve a value saved in a map variable on another link. Get value by specific key, which has been assign by using setValue(key) | return getValue("Vend_Item"); |
b. Some code in C# that common use in Map code
Usage | Description | Example | Note |
string.IsNullOrEmpty(value) | Check if value has data or not | string.IsNullOrEmpty(value) | return boolean (true or false) |
string.IsNullOrWhiteSpace(value) | Check if value has data or empty space or not | string.IsNullOrWhiteSpace(value) | return boolean (true or false) |
DateTime.Now.ToString(format); | Return a string represent the current date time | DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss"); | |
value.Length | length of the value | value.Length | return a number |
value.Substring(start, end) | extracts characters from start to end | value.Substring(0, 80) | |
value.Trim() | remove space from begin and end of the value | getChildValue("Adjustment_Type").Trim() | |
value.Replace(from, to) | replace "from" and replace "to" | descript.Replace("*","") | |
input.Split(value) | Separate the input string based on value | barcode.Split('/') | return a list of string |
int.Parse(value) | transfer from string to integer number | int.Parse(getValue("LineCount")) | return number |
double.TryParse(value, out var doublevalue) | transfer from string to double number | double.TryParse(getChildValue("Adjustment_Amount"), out var amount) | this function return nothing, but the output data is "doublevalue" |
doublevalue.ToString(format) | convert double number to string in right format | return result.ToString("0.##"); | |
input.StartsWith(value) | check if input start with value | note.StartsWith("TRK-") | return true or false |
DateTime.Now.AddDays(num).ToString(format); | Return a string represent the day after current date | return DateTime.Now.AddDays(1).ToString("yyyyMMdd"); |
3. Common example
a. Getting the qualifiers in the for loop at the detail level
This is Map code from:
Map: ANSI X12 830 004010 to Horton Planning Schedule FFS Target
Link: /Document/FGrp/Msg/LoopLIN/LoopFST => /Root/Implicit/H_RECORD/LIN_LOOP/F_RECORD
string[] qualFields = new string[] { "LIN02", "LIN04", "LIN06", "LIN08", "LIN10", "LIN12", "LIN14", "LIN16", "LIN18", "LIN20", "LIN22", "LIN24", "LIN26", "LIN28", "LIN30" };
string[] qualValues = new string[] { "LIN03", "LIN05", "LIN07", "LIN09", "LIN11", "LIN13", "LIN15", "LIN17", "LIN19", "LIN21", "LIN23", "LIN25", "LIN27", "LIN29", "LIN31" }; for (var i = 0; i < qualFields.Length; i++)
{
var qual = getChildValue("../LIN/" + qualFields[i]);
var field = getChildValue("../LIN/" + qualValues[i]);
if (qual == "BP")
{
setValue("partBP", field);
}
if (qual == "EC")
{
setValue("partEC", field);
}
if (qual == "CR")
{
setValue("partPO", field);
}
if (qual == "RC")
{
setValue("partRC", field);
}
if (qual == "RN")
{
setValue("partRN", field);
}
if (qual == "VP")
{
setValue("partVP", field);
}
} return "";
Explanation:
- We declare 2 string arrays
- Then we loop through the array: (start with i= 0)
- about the getChildValue("../LIN/" + arr[i])
- we are at /Document/FGrp/Msg/LoopLIN/LoopFST (Source xpath)
- ../ will move the xpath to upper level (/Document/FGrp/Msg/LoopLIN/)
- then move to LIN/ record,
- then we add arr[i] to get element
- qual variable will get value from xpath: /Document/FGrp/Msg/LoopLIN/LIN/LIN02
- field variable will get value from xpath: /Document/FGrp/Msg/LoopLIN/LIN/LIN03
- then we check if the qual equals "BP"
- we will set value "partBP" by field variable value
- then we check if the qual equal others, and do like "BP" case
- then the i will increase 1 and begin the loop again
- about the getChildValue("../LIN/" + arr[i])
- At the end of this: we will set right value for keys: partBP, partEC, partPO, partRC, partRN, partVP, so that we can use them later (by calling getValue("partBP")...)
b. The cross reference for EDI look up table.
This is Map code from:
Map: ANSI X12 830 004010 to Horton Planning Schedule FFS Target
Link: /Document/FGrp/Msg/BFR/BFR04 => /Root/Implicit/H_RECORD/LIN_LOOP/F_RECORD/DateTypeCode
return getXrefValue("830 - Schedule Type", value);
Explain:
- We will get value from /Document/FGrp/Msg/BFR/BFR04 (input)
- Then we check the input in Lookup table "830 - Schedule Type"
- if the inputmatch any name in the pair (name, value) of the Lookup table "830 - Schedule Type"
- Then the function will return the value of that pair
- But if no pair match then the function will return empty
4. Using cross reference with arrays
a. This code is looking up EDI qualifier and IDs (ZZ:SENDER) based off a vendor code.
Explanation:
- It looks up a value in the lookup table using the variable "result".
- Then it checks to make sure it contains a ":" in the returned value. If it doesn't, it returns empty which is the double quotes "";
- If it contains a colon ":", then it splits it into an array.
- Then it assigns the first value as the sender qualifier to a variable. Ex: "ZZ"
- Then it assigns the second the value as the sender ID to a variable. Ex: "SENDER"
- Then it saves each one into a map variable with the setValue function (see above for usage).
- Then it returns the qualifier variable.