UpdateItem

Description

This function makes changes to an existing item's attributes, or adds a new item to the table, if the item does not exist. Using this function, you can also perform a conditional update on an existing item.

For more information, see UpdateItem.

Category

History

ColdFusion (2021 release): Added this function.

Syntax

serviceHandle.updateItem(requestParameters)

Parameters

See request parameters of UpdateItem.

Example

<cfscript> 
    cred = { 
        "credentialAlias" : "myalias", 
        "vendorName" : "AWS", 
        "region" : "us-east-2", 
        "secretAccessKey" : "xxxxx", 
        "accessKeyId" : "xxxx" 
  } 
     
    conf={ 
        "serviceName"="DYNAMODB" 
    } 
     
    dynamo=getCloudService(cred, conf) 
    tableName="NewProductCatalog" 
 
    Stage 1: Create a table 
    createTableStruct={ 
        "TableName": "#tableName#", 
        "KeySchema": [ 
            { 
                "AttributeName": "id", 
                "KeyType": "HASH" 
            } 
        ], 
        "AttributeDefinitions": [ 
            { 
                "AttributeName": "id", 
                "AttributeType": "N" 
            } 
        ], 
        "ProvisionedThroughput": { 
            "ReadCapacityUnits": 10, 
            "WriteCapacityUnits": 10 
        } 
    } 
 
    dynamo.createTable(createTableStruct) 
    sleep(20000) 
 
    // Stage 2: Insert an item 
    putItemStruct = { 
        "TableName": "#tableName#", 
        "Item":{ 
            "id": { 
                "N": 550 
            }, 
            "Title": { 
                "S": "Macbeth" 
            }, 
            "Price": { 
                "N": "2" 
            } 
        }, 
        "ReturnValues": "ALL_OLD" 
    } 
    dynamo.putItem(putItemStruct,{"hasType": true}) 
    sleep(20000) 
    //abort; 
 
    // Stage 3: Update the item that was inserted 
    updateItemStruct={ 
        "TableName": "#tableName#", 
        "Key": { 
            "id": { 
                "N": 550 
            } 
        }, 
        "UpdateExpression": "set Title = :val1", 
        "ConditionExpression": "Price = :val2", 
        "ExpressionAttributeValues": { 
            ":val1": {"S": "Hamlet"}, 
            ":val2": {"N": "2"} 
        }, 
        "ReturnValues": "ALL_NEW" 
    } 
    try{ 
        result = dynamo.updateItem(updateItemStruct, {"hasType": true}) 
        if(result.Attributes.Title.S == "Hamlet") { 
            writeOutput("Title changed successfully<br/>") 
        } else { 
            writeOutput("Failed to change the title<br/>") 
        } 
    } 
    catch (any e){ 
        writeDump(e) 
    } 
         
</cfscript>

 Adobe

Get help faster and easier

New user?