1

I get the error

The Id field should not be specified in the sobject data.

when trying to update accounts name. I followed advice from another post about setting the Id to null but I'm unable to do so. I get the error c.Id cannot be assigned to -- It's read only.

var accounts = await client.QueryAsync<Account>("SELECT Id, Name FROM Account Where Name='TTR'");
        var sfAccounts = accounts.Records.Select(c => new
        {
             c.Id,
            Name = "Blah",
        });

        foreach (var c in sfAccounts)
        {
            var id = c.Id;
            c.Id = null;
            var success = await client.UpdateAsync("Account", id, c);
        }
2

In Salesforce, record Id's are generated when you insert any record. Once record id's are generated they become Read Only fields. Hence they cannot be modified.

In above code, you're trying to make id field of contact to null. Now consider a RDBMS case, were you're trying to make Primary key as a null. This is a invalid scenario because primary key cannot be null. Same is the case with Salesforce, here id is used as primary key to identify records individually.

If your purpose is make related contact to null then simply delete those records. In this way you can get rid of that error.

  • I'm trying to update accounts where the name is TTR and naming them blah. My original code was var accounts = await client.QueryAsync<Account>("SELECT Id, Name FROM Account Where Name='TTR'"); var sfAccounts = accounts.Records.Select(c => new { c.Id, Name = "Blah", }); foreach (var c in sfAccounts) { var success = await client.UpdateAsync("Account", id, c); } This code was throwing the error. – Laura Bejjani Jun 19 '17 at 23:47
1

Have a look at how UpdateAsync is implemented with respect to the underlying REST API. It maps the record ID into the PATCH URL.

Yet your anonymous type includes an Id property, even if it is null.

Try something like:

var auth = new AuthenticationClient();
await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password);

ForceClient client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion);

var accounts = await client.QueryAsync<Account>("SELECT Id, Name FROM Account limit 2");
var sfAccounts = accounts.Records.Select(c => new { c.Id, Name = c.Name + "Blah" });
foreach (var a in sfAccounts)
{
    // Use another anonymous Type to avoid passing the Id in.
    var success = await client.UpdateAsync("Account", a.Id, new { Name = a.Name });
}

If you were using strongly type classes to represent that class you could try using the UpdateableAttribute on the Id property to prevent it being serialized into the JSON payload with a patch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.