Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X
I am converting a json dataset to a datashape using this code but it returns an empty infotable:
let transformData = function (input) {
try {
const transformedData = {
fieldDefinitions: {},
data: []
};
// Iterate over each record in the input data
input.records.forEach(function(record, index) {
// Create the field definitions (same as before)
transformedData.fieldDefinitions["occurence_" + index] = {
name: "occurence",
aspects: { isPrimaryKey: false },
description: "",
baseType: typeof record.Occurence__c === 'number' ? "NUMBER" : "STRING",
ordinal: 4
};
transformedData.fieldDefinitions["productCode_" + index] = {
name: "productCode",
aspects: { isPrimaryKey: false },
description: "",
baseType: "STRING",
ordinal: 2
};
transformedData.fieldDefinitions["errorCode_" + index] = {
name: "errorCode",
aspects: { isPrimaryKey: false },
description: "",
baseType: "STRING",
ordinal: 3
};
transformedData.fieldDefinitions["id_" + index] = {
name: "id",
aspects: { isPrimaryKey: true },
description: "",
baseType: "GUID",
ordinal: 1
};
transformedData.fieldDefinitions["timeFrame_" + index] = {
name: "timeFrame",
aspects: { isPrimaryKey: false },
description: "",
baseType: "STRING",
ordinal: 5
};
// Push the actual data values into the "data" array
transformedData.data.push({
occurence: record.Occurence__c,
productCode: record.Product_Code__c,
errorCode: record.Error_Code__c,
id: record.Id,
timeFrame: record.Timeframe__c
});
});
return transformedData;
} catch (err) {
logger.error("Problem data conversion: " + err);
}
};
const result = transformData(inputData);
logger.debug(JSON.stringify(result, null, 2));
The input json is
{ "headers": { "Authorization": "Bearer xxxxx", "Accept": "application/json", "Content-Type": "application/json" }, "totalSize": 2, "records": [ { "Timeframe__c": "24h", "Product_Code__c": "12-4300-00", "Type__c": "Critical", "attributes": { "type": "Error_Codes__c", "url": "/services/data/v57.0/sobjects/Error_Codes__c/a39KI000000GyDMYA0" }, "Id": "a39KI000000GyDMYA0", "Error_Code__c": "0-122", "Occurence__c": 1 }, { "Timeframe__c": "Immediate", "Product_Code__c": "12-4100-00", "Type__c": "Critical", "attributes": { "type": "Error_Codes__c", "url": "/services/data/v57.0/sobjects/Error_Codes__c/a39KI000000H0kPYAS" }, "Id": "a39KI000000H0kPYAS", "Error_Code__c": "0-1303", "Occurence__c": 1 } ], "done": true }
please help
Solved! Go to Solution.
-the field for the content is rows, not data
-you're not declaring a Datashape, those only exist as persistent objects
-no need to "const" result, it's already declared implicitly.
-your code defines 5 fields per row, but in the end you don't use those when pushing the data
-You can't have different types per row. All rows must have the same type for a field
-I would recommend using the Infotable API to create an infotable instead of doing it the "raw" way.
try this:
let transformData = function (input) {
try {
let transformedData = Resources["InfoTableFunctions"].CreateInfoTable();
// for the types, we take the first row as a sample
let record=input.records[0];
transformedData.AddField({name: 'id', baseType: "GUID"});
transformedData.AddField({name: 'productCode', baseType: "STRING"});
transformedData.AddField({name: 'errorCode', baseType:"STRING"});
transformedData.AddField({name: 'occurence', baseType: typeof record.Occurence__c === 'number' ? "NUMBER" : "STRING"});
transformedData.AddField({name: 'timeFrame', baseType: "STRING"});
// Iterate over each record in the input data
input.records.forEach( row=> {
transformedData.AddRow({ occurence:row.Occurence__c,
productCode:row.Product_Code__c,
errorCode:row.Error_Code__c,
id:row.Id,
timeFrame:row.Timeframe__c});
});
return transformedData;
} catch (err) {
logger.error("Problem data conversion: " + err);
}
};
result = transformData(inputData);
-the field for the content is rows, not data
-you're not declaring a Datashape, those only exist as persistent objects
-no need to "const" result, it's already declared implicitly.
-your code defines 5 fields per row, but in the end you don't use those when pushing the data
-You can't have different types per row. All rows must have the same type for a field
-I would recommend using the Infotable API to create an infotable instead of doing it the "raw" way.
try this:
let transformData = function (input) {
try {
let transformedData = Resources["InfoTableFunctions"].CreateInfoTable();
// for the types, we take the first row as a sample
let record=input.records[0];
transformedData.AddField({name: 'id', baseType: "GUID"});
transformedData.AddField({name: 'productCode', baseType: "STRING"});
transformedData.AddField({name: 'errorCode', baseType:"STRING"});
transformedData.AddField({name: 'occurence', baseType: typeof record.Occurence__c === 'number' ? "NUMBER" : "STRING"});
transformedData.AddField({name: 'timeFrame', baseType: "STRING"});
// Iterate over each record in the input data
input.records.forEach( row=> {
transformedData.AddRow({ occurence:row.Occurence__c,
productCode:row.Product_Code__c,
errorCode:row.Error_Code__c,
id:row.Id,
timeFrame:row.Timeframe__c});
});
return transformedData;
} catch (err) {
logger.error("Problem data conversion: " + err);
}
};
result = transformData(inputData);
I also needed to change my input type from string to JSON