Query and Update a Person Account
Because Person Accounts are tightly coupled with Contacts, the Name
field is compound and cannot be updated. Instead you must update FirstName
and LastName
.
SalesforceSession salesforceSession = new SalesforceSession(new LoginDetails("username@example.com", "salesforcePassword"));
AccountService accountService = new AccountService(salesforceSession);
// This queries an existing Account record by 'Name' and retrieves all fields.
Account acct = accountService.GetSingleByFieldEquals(Account.Fields.Name, "John Smith");
// Change the FirstName.
acct.InternalFields[Contact.Fields.FirstName.Name] = "Jack";
// If we tried to update now, Salesforce would return INVALID_FIELD_FOR_INSERT_UPDATE error because we can't update 'Name'.
// Remove Name from the fields to be updated.
acct.InternalFields.Remove(Account.Fields.Name.Name);
// Now we can update successfully.
SaveResult result = accountService.UpdateEntity(acct);
Assert(result.success);