6 Replies Latest reply on Feb 10, 2016 12:07 PM by chrish RSS
    chrish Explorer

    How to use variable in "VALUES.<COLUMN>" as the <COLUMN>? (i.e. VALUES.variable=1)

    I'm trying to create a service that I can globally put in a ThingShape; then reference inside every new DataTable that I create. This service will need to find the PK of each new data table and ensure that it is populated correctly.

    The code snippet below gets the PK FieldName of any table dynamically (pkName).


    QUESTION...I need to use the variable value (type: STRING) of pkName dynamically as the ID varaiable in the VALUES and updateVALUES params of UpdateDataTableEntriesWithQuery(params).  See red below...What's the syntax to accomplish this?


    NOTE **IF YOU SEE SOMETHING ELSE WRONG BELOW, please help!? **

       

        var values = me.CreateValues();  // INFOTABLE

        var updateValues = me.CreateValues();  // INFOTABLE

        var dsName = me.GetDataShape();  // DataShapeName -> auto-converts to STRING

        var ID=eventData.ID

        var pkName="findIT" // STRING

        var ds = DataShapes[dsName]; // DataShape

     

        //Dubug logging for variable verification

        logger.warn("Thing: " + me.name + " || VARIABLE data BEFORE logic || dsName="+dsName + " newID="+newID + " pkName="+pkName + " ds="+ds);


        var fieldInfo = ds.GetDataShapeMetadataAsJSON().fieldDefinitions;

     

        if(originaID==0) {  // I only need to run logic if the PK is at default of 0

            for (var property in fieldInfo) {

                if (fieldInfo.hasOwnProperty(property)) {

                    if (fieldInfo[property].aspects) {

                        if (fieldInfo[property].aspects.isPrimaryKey) {

                            pkName = fieldInfo[property].name;

                            }

                        }

                    }

                }

        }

     

        //Dubug logging for variable verification and logic check.

        logger.warn("Thing: " + me.name + " || VARIABLE data AFTER logic || dsName="+dsName + " newID="+newID + " pkName="+pkName + " ds="+ds);

     

      // Insert SEARCH values mapping here...PRIMARY_KEY MUST BE SET

            values.?pkName.VALUE? = ID; //NUMBER [Primary Key]

     

     

        // Insert UPDATE values mapping here...

            updateValues.?pkName.VALUE? = ID+1; //NUMBER [Primary Key]..updated PK

     

     

          // Set params variable used to insert record into actual data table

            var params = {

                sourceType: undefined /* STRING */,

                values: values /* INFOTABLE*/,

                query: undefined /* QUERY */,

                location: undefined /* LOCATION */,

                source: undefined /* STRING */,

                updateValues: updateValues /* INFOTABLE*/,

                tags: undefined /* TAGS */

            };

     

     

          //Update row data with INSERT timestamp and log user

          me.UpdateDataTableEntriesWithQuery(params);