Check this How to use Global Variable in SAP PI Mapping
- Create the global variable "the flag" as "String/Boolean"
- Create a UDF that read the flag, if "the flag" is "empty/false" then return "HEADER" and change "the flag value to X/true"
- Use the UDF in the root of your target message, if the value is not "HEADER" then map the root element.