Examples of Import Service Customization
The following sections provide example customizations for certain overridable services:
Import
GetImportSupportedBaseTypes
The GetImportSupportedBaseTypes service allows you to control the property base types supported for import. This example customization excludes a property base type from import.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportExportEquipmentUtils Thing for editing.
2. Under Services, find GetImportSupportedBaseTypes, and click Override service icon to override the service.
3. In the script editor, view the list of supported base types.
var result = ['Boolean', 'DateTime', 'Integer', 'Long', 'JSON', 'Number', 'String'];
4. To exclude a base type from import, delete or comment the base type that you want to exclude from the list. For example, to exclude the JSON base type, update the service as follows:
var result = ['Boolean', 'DateTime', 'Integer', 'Long', 'Number', 'String'];
5. Click Done to save the change to the service.
6. Click Save to save the Thing.
After this change, if properties with the JSON base type are present in the import spreadsheet, they are not imported. When the spreadsheet is uploaded and validated, properties with the JSON base type are counted as invalid and are skipped during the import.
* 
To view details of invalid data for import, access the script logs:
1. In ThingWorx Composer, click Monitoring Monitoring icon in the left pane.
2. Under Logs, click ScriptLog.
3. Select a row in the ScriptLog table to view the Log Messages for that entry.
ParseImportFile
This example customizes the import parsing process, so that parsing messages are displayed in the ScriptLog with a Warn level.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ParseExcelProcessor Thing.
2. Under Services, find ParseImportFile, and click Override service icon to override the service.
3. In the script editor, change the logger level to WARN, as follows:
var result = (function () {
var constants = Things["PTC.SCA.SCO.Common.ImportExportEquipmentUtils"].GetImportExportConstants();

logger.warn("Parsing process started");

var user = Resources["CurrentSessionInfo"].GetCurrentUser();
var lastFileEntry = Things["PTC.SCA.SCO.Common.ImportProcessHistoryDataTable"].GetDataTableEntryByKey({ key: user });

// Invoke service which parses equipment data
var parsedInfoTable = Resources["PTC.Factory.CommonUtilities"].ParseImportFile({
fileName: lastFileEntry.FileName /* STRING */ ,
dataShape: constants.importExportDataShape /* DATASHAPENAME */
});

logger.warn("Parsing process completed");
return parsedInfoTable;
})();
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
ValidateEquipment
This example customizes the equipment validation, so that equipment is created even when the project specified in the import spreadsheet does not exist.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportValidator Thing.
2. Under Services, find ValidateEquipment, and click Override service icon to override the service.
3. In the script editor, find and comment out the following lines:
if (project.length > 0 && !validateProject(project)) {
logger.warn("Import Validation Process: [General Information Tab] [Name: " + name + "] Invalid reason: Project does not exist");
isInformationIncorrect = true;
}
This causes the code to skip validating the existence of the project specified for the equipment.
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
ValidateEquipmentHierarchyTab
This example customization causes the ScriptLog to log the start of the ValidateEquipmentHierarchyTab service as a Trace entry.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportValidator Thing.
2. Under Services, find ValidateEquipmentHierarchyTab, and click Override service icon to override the service.
3. In the script editor, add the following code at the end of the script:
logger.trace("Validation complete for EquipmentHierarchyTab custom service");
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
6. Navigate to Monitoring > ScriptLog, and change the log level settings to Trace. The log now includes a Trace entry for entering the ValidateEquipmentHierarchyTab custom service.
ValidateImportFile
This example customization causes the Validation Results table to display the worksheet (tab) name as Hierarchy, rather than Equipment Structure.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportValidator Thing.
2. Under Services, find ValidateImportFile, and click Override service icon to override the service.
3. In the script editor, find and comment out the following lines:
var equipmentHierarchyToken = Resources["RuntimeLocalizationFunctions"].GetEffectiveTokenForLanguage({
language: currentUserLanguage,
token: constants.equipmentHierarchyToken
});
4. Edit the equation in the line immediately following the new comment to change the value from equipmentHierarchyToken to Hierarchy as follows:
hierarchyReport[workSheetColumn] = Hierarchy;
5. Click Done to save the change to the service.
6. Click Save to save the Thing.
ValidateLocallyBoundProperty
This example customizes the validation of locally bound properties, so that only the specified PTC.SCA.SCO.Demo.KepServer Thing can be used for locally bound properties:
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportValidator Thing.
2. Under Services, find ValidateLocallyBoundProperty, and click Override service icon to override the service.
3. In the script editor, search for and find the following lines:
// Check if server thing or property for bounding exists
if (!boundThingExists || property.length === 0) {
validationResult = me.GenerateReportForEquipment({
operation: constants.invalid
});

if (!boundThingExists) {
logger.warn("Import Validation Process: [Properties Tab] [Equipment Name: " + thingName + " Name: " + name + "] Invalid reason: Thing does not exist");
}

if (property.length === 0) {
logger.warn("Import Validation Process: [Properties Tab] [Equipment Name: " + thingName + " Name: " + name + "] Invalid reason: Property not specified");
}
} else {
Replace those lines with the following code:
// Check if server thing or property for bounding exists and if thing is same as required
if (!boundThingExists || thing !== "PTC.SCA.SCO.Demo.KepServer" || property.length === 0) {
validationResult = me.GenerateReportForEquipment({
operation: constants.invalid
});

if (!boundThingExists) {
logger.warn("Import Validation Process: [Properties Tab] [Equipment Name: " + thingName + " Name: " + name + "] Invalid reason: Thing does not exist");
}

if (thing !== "PTC.SCA.SCO.Demo.KepServer") {
logger.warn("Import Validation Process: [Properties Tab] [Equipment Name: " + thingName + " Name: " + name + "] Invalid reason: Property can be locally bounded only to specified thing");
}

if (property.length === 0) {
logger.warn("Import Validation Process: [Properties Tab] [Equipment Name: " + thingName + " Name: " + name + "] Invalid reason: Property not specified");
}
}
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
ValidatePropertiesTab
This example customizes the validation of the Properties tab of the import spreadsheet when importing equipment, so that if the property name does not start with InternalPrefix, then it is considered a valid property name.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportValidator Thing.
2. Under Services, find ValidatePropertiesTab, and click Override service icon to override the service.
3. In the script editor, find the following line:
var isNameValid = isValidPropertyName(propertyName);
Insert the following code as the next line:
isNameValid = !propertyName.startsWith("InternalPrefix");
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
ValidateProperty
This example customizes the validation, so that any properties specified in the import spreadsheet as having a Remotely Bound value in the Binding column are considered invalid, and are not created or updated by the import process.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportValidator Thing.
2. Under Services, find ValidateProperty, and click Override service icon to override the service.
3. In the script editor, find and comment out the following lines:
} else if (equalsIgnoreCase(binding, constants.remotelyBound)) {
validationResult = me.ValidateRemotelyBoundProperty({
parsedInfoTable: parsedInfoTable
});
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
ValidateRemotelyBoundProperty
This example customizes the validation, so that if the Remote Property Name value for an existing remotely bound property is changed in the import spreadsheet, the validation process does not recognize this change, and the property value is not updated during import.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportValidator Thing.
2. Under Services, find ValidateRemotelyBoundProperty, and click Override service icon to override the service.
3. In the script editor, find the following lines:
if (isRemotePropertyNameDifferent(remotePropertyName, remotePropertyBindingInfo)) {
updated = true;
} else if (isCacheIntervalDifferent(cacheInterval, thingName, name)) {
Comment out the code through the else statement, as shown here:
/* if (isRemotePropertyNameDifferent(remotePropertyName, remotePropertyBindingInfo)) {
updated = true;
} else */
if (isCacheIntervalDifferent(cacheInterval, thingName, name)) {
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
ValidateGeneralInformationTab
This example customizes the validation, so that any equipment defined in the spreadsheet that does not have the required Equipment_ prefix on its name is considered to be invalid and is not created during import.
To customize the validation of the General Information tab when importing equipment:
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportValidator Thing.
2. Under Services, find ValidateGeneralInformationTab, and click Override service icon to override the service.
3. In the script editor, find the following line of code:
// validate context and network
Immediately preceding that line of code, insert the following code:
if (name.substring(0, 10) !== "Equipment_") {
reportInfoTable = markEquipment(reportInfoTable, reportDataShapeFields, constants.invalid);

logger.warn("Import Validation Process: [General Information Tab] [Row number: " + rowNum + "] Invalid reason: Name does not have required prefix");

continue;
}
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
Import
This example customization prevents the post-import process from running.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportProcessor Thing.
2. Under Services, find Import, and click Override service icon to override the service.
3. In the script editor, find and comment out the following lines:
// Run post-import process
me.PostImport({
parsedInfoTable: parsedInfoTable
});
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
ImportEquipmentHierarchy
This example customization causes the ScriptLog to log the start of the ImportEquipmentHierarchy service as a Trace entry.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportProcessor Thing.
2. Under Services, find ImportEquipmentHierarchy, and click Override service icon to override the service.
3. In the script editor, add the following code at the end of the script:
logger.trace("Entering ImportEquipmentHierarchy custom service");
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
6. Navigate to Monitoring > ScriptLog, and change the log level settings to Trace. The log now includes a Trace entry for entering the ImportEquipmentHierarchy custom service.
ImportEquipmentInformation
This example customization adds the prefix "E_" to the default display name for any piece of equipment that has no display name value specified in the import spreadsheet. The default display name is the Thing name in ThingWorx Composer.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportProcessor Thing.
2. Under Services, find ImportEquipmentInformation, and click Override service icon to override the service.
3. In the script editor, find the following code:
if (thing != null) {
if (displayName.length > 0) {
thing.displayName = displayName;
}
After the end bracket, insert the following code:
else {
thing.displayName = "E_" + name;
}
The code now appears as follows:
if (thing != null) {
if (displayName.length > 0) {
thing.displayName = displayName;
} else {
thing.displayName = "E_" + name;
}
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
ImportGeneralInformation
This example customization displays information about the number of rows processed during the import in the ScriptLog, and changes the displayed log level to Trace.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportProcessor Thing.
2. Under Services, find ImportEquipmentInformation, and click Override service icon to override the service.
3. In the service pane, add the following line of code after line 27 to send log information about processed rows:
logger.trace("Import Process: [General Information Tab] Already processed rows: " + (i + 1));
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
To change the log level displayed in the ScriptLog:
1. In the ScriptLog, click Configure.
2. In the Configure Log window, select the desired level from the Log Level drop-down list.
3. Click Apply.
ImportNoneBoundProperty
This example customization causes any properties that are specified in the import spreadsheet with a Binding value of None to be created in ThingWorx Composer with a Logged value of true, regardless of the Logged value that is specified in the import spreadsheet.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportProcessor Thing.
2. Under Services, find ImportNoneBoundProperty, and click Override service icon to override the service.
3. In the script editor, find the following line:
var logged = getBooleanValue(equipmentProperty[loggedHeader]);
Edit this line to read as follows:
var logged = true;
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
ImportProperties
This example customization causes the import process to create no locally bound properties, even if they are specified in the import spreadsheet. They are also not reported as invalid rows by the validation process.
To customize the importing of properties when importing equipment:
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportProcessor Thing.
2. Under Services, find ImportProperties, and click Override service icon to override the service.
3. In the script editor, find and comment out the following lines:
} else if (equalsIgnoreCase(binding, constants.locallyBound)) {
me.ImportLocallyBoundProperty({
equipmentProperty: propertyRowInfoTable
});
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
ImportRemotelyBoundProperty
This example customization causes any property created by the import process that was specified in the import spreadsheet with a Binding value of Remotely Bound to have a When Disconnected value of Fold all the changes into a single last changed value on the Properties and Alerts page for the piece of equipment in ThingWorx Composer.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportProcessor Thing.
2. Under Services, find ImportRemotelyBoundProperty, and click Override service icon to override the service.
3. In the script editor, find the following sections of code, and in each section change the value of NONE to FOLD:
if (params.whenDisconnected.length === 0) {
params.whenDisconnected = 'NONE';
}
if (whenDisconnected.length === 0 || whenDisconnected.length > 0 && !isWhenDisconnectedAllowed(whenDisconnected)) {
whenDisconnected = 'NONE';
}
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
PreImport
This example customization causes the ScriptLog to log the start of the PreImport service as a Trace entry.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportProcessor Thing.
2. Under Services, find PreImport, and click Override service icon to override the service.
3. In the script editor, add the following code at the end of the script to enable the PreImport custom service:
logger.trace("Entering PreImport custom service");
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
6. Navigate to Monitoring > ScriptLog, and change the log level settings to Trace. The log now includes a Trace entry for entering the PreImport custom service.
PostImport
This example customization causes the ScriptLog to log the start of the PostImport service as a Trace entry.
1. In ThingWorx Composer, open the PTC.SCA.SCO.Common.ImportProcessor Thing.
2. Under Services, find PostImport, and click Override service icon to override the service.
3. In the script editor, add the following code at the end of the script to enable the PostImport custom service:
logger.trace("Entering PostImport custom service");
4. Click Done to save the change to the service.
5. Click Save to save the Thing.
6. Navigate to Monitoring > ScriptLog, and change the log level settings to Trace. The log now includes a Trace entry for entering the PostImport custom service.
Was this helpful?