There are other articles here in this form explaining how to generally work with selector scripts. This article is mainly about PTC Integrity resp. PTC Windchill RV&S. There is a "integrity.js" library floating around but we will ignore this at first and come back to it later.
First lets solve a simple problem: the import shall only import requirements with a certain "State", say "Accepted". The attribute "State" is a default attribute in Integrity which makes it easier for us. We translate this wish into: "the selector script shall return true if - and only if - the state of the mapped object is 'Accepted'.
Remember that the script works on the internal representation of an Integrity element. We can simply do this:
<typeMappings
mediniType="SafetyRequirement"
mappedType="Requirement"
mappedSelector="element.values.toArray().find(e => e.field.name == 'State').element == 'Accepted'">
<attributeMappings
mediniAttribute="name"
mappedAttribute="Text"/>
<attributeMappings
mediniAttribute="identifier"
mappedAttribute="Live Item ID"
direction="in" />
</typeMappings>
The important point here is that the State attribute is an Enumeration, hence, it does not have an atomic value but again a reference to an element, which is the enumeration literal.
Now lets assume we want to extend the selector by a second condition, like the Category. We could put a second condition there and combine it with && but sooner or later that will become unreadable. Better solution is to write a few helper functions in a separate file to keep the selector readable.
Now the integrity.js comes back into play. The original version had already a function 'hasAnyValue' which worked for non-enums. Lets add our code from above as a new function 'hasEnumValue' into integrity.js and put the library next to the mapping file! The last step is essential: mapping files load their libraries from the same folder they reside, not from the scripting folder by default.
The mapping selector reduces to:
<typeMappings
mediniType="SafetyRequirement"
mappedType="Requirement"
mappedSelector="load('integrity.js'); hasEnumValue(element, 'State', 'Accepted');">
<attributeMappings
mediniAttribute="name"
mappedAttribute="Text"/>
<attributeMappings
mediniAttribute="identifier"
mappedAttribute="Live Item ID"
direction="in" />
</typeMappings>
Happy selecting
Jan