Trying to update a cell in a row with PowerShell by passing a variable

Alen
Alen
edited 10/06/22 in API & Developers

This works okay:

$Headers = @{

Authorization = "Bearer ***********"

}

$uri = "https://api.smartsheet.com/2.0/sheets/5034086415787908/rows"

$body = '[{"id": "5257700085983108", "cells": [{"columnId": "853109098473348","value": true}]}]'

Invoke-RestMethod -Method put -Uri $uri -Headers $headers -body $body


But this doesn't work:

$rowid = "5257700085983108"

$body = '[{"id": $rowid, "cells": [{"columnId": "853109098473348","value": true}]}]'

Error:

Invoke-RestMethod : {

 "errorCode" : 1008,

 "message" : "Unable to parse request. The following error occurred: Field \"null\" was not parsable. Unrecognized token '$rowid': was expecting ('true', 'false' or 'null')\n

How do I pass a variable in the body of the API?

Thanks.

Answers

  • Julio S.
    Julio S. Moderator
    edited 10/06/22

    Hi @Alen,

    It looks like one of the "$rowID" values in your call isn't expected - probably this is the one after the "id": field which is expecting "true", "false" or "null" values instead of "$rowID". There should also be specific reference to the specific line of command and column that are causing the API call to fail.

    I also believe that PowerShell requires double quotes to handle the variable in the string:

    $body = "[{"id": $rowid, "cells": [{"columnId": "853109098473348","value": true}]}]"
    

    If this doesn't look correct or can't figure the actual value to be inserted, I'd suggest reviewing the specific method syntax required for this action in the API Documentation.

    If you'd need further advise, please make sure to include a full capture of the logs returned by the API including the specific line and column triggering this error. 

    I hope that this can be of help.

    Cheers!

    Julio