UpdateRespondentsGeneral

Description

Updates the respondents. Missing respondents will be added.

If "inTransaction" is set to true, the update will be performed in a transaction. If an error occurs, a rollback for the whole update will be executed. Since the operation will perform a rollback if an error occurs, the return value will not have any function when the a transaction is used.
Signature
ErrorMessage[] UpdateRespondentsGeneral(string key, string projectId, string uniqueKey, ConfirmitData data, bool inTransaction)

Parameters

Name Data Type Description

key

string required

The authentication key

projectId

string required

The project ID

uniqueKey

string required

The name of the unique key to use when updating the respondents. If set to null or an empry string, the internal id (respid) for respondents will be used.

data

ConfirmitData containing the respondent rows to be updated.

inTransaction

bool

True runs the update in a transaction, False does not.

Response

Data Type Description

Example

//***************************************************
//
// GetRespondentsGeneral
// UpdateRespondentsGeneral
//
//***************************************************

// The example will add a new column to the respondent data. The name of the new
// column will be "myNewColumn".
const string newColumnName = "myNewColumn";

// New instance of the SurveyData webservice.
DataTransferSoapClient dataTransfer = new DataTransferSoapClient();

RespondentToken token = null;
RespondentConfirmitDataResult result;

// Create a RespondentDataTransferDef object to get an empty list of  respondents
// from the given project.
RespondentDataTransferDef respondentDataTransferDef = new RespondentDataTransferDef
{
    ProjectId = projectId,
    Expression = "noOfEmailsSent = -1"
};

// Get the respondents. Run in loop while there is more data to get.
do
{
    result = dataTransfer.GetRespondentsGeneral(key, respondentDataTransferDef, token);
    token = result.Token;
    DataLevel dataLevel = result.Result.DataLevels[0];

    // Add a new column to the respondent data if it does not exist
    if (!dataLevel.Variables.Any(variable => variable.Name == newColumnName))
    {
        var listVariables = dataLevel.Variables.ToList();
        listVariables.Add(new Variable
            {
                Name = newColumnName,
                Type = VariableType.String
            });
        dataLevel.Variables = listVariables.ToArray();
    }

    // Get a list of the indexes for columns we want to set for the new respondent
    // ("gender", "age", "newColumnName" and "email").
    List<int> columnIndexes = new List<int>();
    var indexedVariables = dataLevel.Variables.Select((variable, i) => new {Variable = variable, Index = i});
    columnIndexes.Add(indexedVariables.First(indexedVariable => indexedVariable.Variable.Name == "gender").Index);
    columnIndexes.Add(indexedVariables.First(indexedVariable => indexedVariable.Variable.Name == "age").Index);
    columnIndexes.Add(indexedVariables.First(indexedVariable => indexedVariable.Variable.Name == newColumnName).Index);
    columnIndexes.Add(indexedVariables.First(indexedVariable => indexedVariable.Variable.Name == "email").Index);

    // Create a new record. Use the list of indexes to set the correct values.
    DataRecord dataRecord = new DataRecord();
    dataRecord.Values = new object[dataLevel.Variables.Count()];
    dataRecord.Values[columnIndexes[0]] = "F";
    dataRecord.Values[columnIndexes[1]] = 17;
    dataRecord.Values[columnIndexes[2]] = "my new value";
    dataRecord.Values[columnIndexes[3]] = "dummy@dummy.com";

    // Add the new record to the list of records
    var listRecords = dataLevel.Records.ToList();
    listRecords.Add(dataRecord);
    dataLevel.Records = listRecords.ToArray();

    // Make the update
    // Note that we do not include a unique key in the UpdateRespondentsGeneral call. In
    // this example we have created a new respondent that should be appended to the list of
    // respondents. Without a unique key the respondents will be appended.
    ErrorMessage[] errors = dataTransfer.UpdateRespondentsGeneral(key, projectId, null, result.Result, false);

    // Check if there was any errors
    if (errors.Length > 0)
    {
        //Implement error-handling here
    }
} while (!result.Token.LastDataSet);

// Make a definition that limits to retrieve the user columns "email",
// the new column defined by the variable newColumnName, and
// the system column "respid" from all female respondents (gender="F")
respondentDataTransferDef = new RespondentDataTransferDef
{
    ProjectId = projectId,
    Expression = "gender = \"F\"",
    FieldNames = new string[] { "respid", "email", newColumnName }
};

token = null;

do
{
    // Read the specified respondents
    result = dataTransfer.GetRespondentsGeneral(key, respondentDataTransferDef, token);
    token = result.Token;
    DataLevel dataLevel = result.Result.DataLevels[0];

    var newColumnNameIndex = dataLevel.Variables.Select((variable, i) => new { Variable = variable, Index = i })
        .First(x => x.Variable.Name == newColumnName).Index;

    // Change the new column for the retrieved rows
    foreach (DataRecord dataRecord in dataLevel.Records)
        dataRecord.Values[newColumnNameIndex] = "My female comment";

    // Make the update.
    // Note that we needed to include a unique key in the data to be able to update. In
    // this example we use the system field "respid" as the unique key. Without a unique
    // identifier in the data the respondents would be appended to the respondent list.
    ErrorMessage[] errors = dataTransfer.UpdateRespondentsGeneral(key, projectId, "respid", result.Result, false);

    // Check if there was any errors
    if (errors.Length > 0)
    {
        //Implement error-handling here
    }
} while (!result.Token.LastDataSet);