我有一个RESTful API,可让我更新某些内容,如下所示:
由于这是外部用户(使用个人帐户自动创建了一个用户)。我想知道为什么我不能更新用户名?如果我对此进行更新,则会出现错误:
10:38:10:978 USER_DEBUG [283] | DEBUG | ===用户更新错误===更新失败。 ID为0030m00000IztRaAAJ的第0行的第一个异常;第一个错误:MIXED_DML_OPERATION,在更新了非设置对象(反之亦然)之后,不允许对设置对象进行DML操作:反之亦然:帐户,原始对象:用户:[]
以下是代码:
public static CA_RSEUserResponse updateUserInfo(CA_RSEUserInfo rseUserInfo) {
User u = null;
CA_RSEUserResponse resp = new CA_RSEUserResponse();
Savepoint sv = Database.setSavepoint();
try {
// 1. Update User
u = [
SELECT Username,Email,FirstName,LastName,Id,ContactId
FROM User
WHERE Id = :rseUserInfo.id
LIMIT 1
];
System.debug('=== User ID is ===' + rseUserInfo.id);
System.debug('User Contact ID is ===' + u.ContactId);
u.Username = rseUserInfo.email_address;
u.Email = rseUserInfo.email_address;
u.FirstName = rseUserInfo.first_name;
u.LastName = rseUserInfo.last_name;
update u;
// 2. Find the related contact and fill in the info
Contact foundContact = [
SELECT LastName,FirstName,Birthdate,MailingCity,MailingState,
MailingPostalCode,Phone,CA_Subscription_Status__c,CA_Gender__c,
CA_Dependants__c,CA_SkillLevel__c,CA_FavoriteCuisines__c,
CA_RecipeDislikes__c,CA_RecipeLikes__c,CA_Retailer__c,
CA_LanguagePreference__c,CA_EnjoymentLevel__c,CA_RepeatFrequency__c,
CA_AdventureLevel__c,CA_CreatedProfile__c,CA_CompletedProfile__c,
CA_HouseholdAdults__c
FROM Contact
WHERE Id = :u.ContactId
LIMIT 1
];
System.debug('Birth Year ==='+rseUserInfo.birth_year);
foundContact.LastName = rseUserInfo.last_name;
foundContact.FirstName = rseUserInfo.first_name;
foundContact.Birthdate = rseUserInfo.birth_year;
foundContact.MailingCity = rseUserInfo.city;
foundContact.MailingState = rseUserInfo.state;
foundContact.MailingPostalCode = rseUserInfo.zip;
foundContact.Phone = rseUserInfo.mobile_phone;
foundContact.CA_Subscription_Status__c = rseUserInfo.subscription_status;
foundContact.CA_Gender__c = rseUserInfo.gender;
foundContact.CA_Dependants__c = rseUserInfo.dependants;
foundContact.CA_SkillLevel__c = rseUserInfo.skill_level;
foundContact.CA_FavoriteCuisines__c = rseUserInfo.favorite_cuisines;
foundContact.CA_RecipeDislikes__c = rseUserInfo.recipe_dislikes;
foundContact.CA_RecipeLikes__c = rseUserInfo.recipe_favorites;
foundContact.CA_Retailer__c = rseUserInfo.retailer;
foundContact.CA_LanguagePreference__c = rseUserInfo.language_preference;
foundContact.CA_EnjoymentLevel__c = rseUserInfo.enjoyment_level;
foundContact.CA_RepeatFrequency__c = rseUserInfo.repeat_frequency;
foundContact.CA_AdventureLevel__c = rseUserInfo.adventure_level;
foundContact.CA_CreatedProfile__c = rseUserInfo.created_profile;
foundContact.CA_CompletedProfile__c = rseUserInfo.completed_profile;
foundContact.CA_HouseholdAdults__c = rseUserInfo.household_adults;
update foundContact;
resp.errorMsg = null;
resp.content = rseUserInfo;
} catch (Exception ex) {
Database.rollback(sv);
System.debug('=== User Updated Error ===' + ex.getMessage());
rseUserInfo = null;
}
return resp;
如果我不更新User.username,一切都很好……那为什么以及如何更新该字段?