我正在工作的合作伙伴门户用户用户创建遇到该异常,但在沙箱中该错误不会出现,它仅在生产中出现。我的要求是创建 api 服务(接受客户姓名、联系人、电子邮件字段作为有效负载),当将帐户插入系统时,它将检查帐户是否存在,不要创建,否则创建帐户,如果联系人存在,则不要创建,否则创建联系人,同样如果用户存在并且已停用,则激活用户,否则创建用户,使用下面的代码我可以在沙箱中创建用户,但在生产中我收到该错误如何解决该错误,如果插入角色,它将出现混合 dml 异常。 :使用流程我正在更新帐户 ispartner = true
@RestResource(urlMapping='/RenovateProjectCustomerAPI/*')
global class RenovateCustomerAPI {
@HttpPost
global static void createRecord(String accountName, String contactFirstName, String contactLastName, String userEmail, String contactPhone) {
Map<String, Object> responseMap = new Map<String, Object>();
Id userId;
String renovateAccountRecordTypeId = Common.getRecordTypeIdByDeveloperName('RenovateProjectAccount', Account.SObjectType);
// Query to check if Account with the given name exists
List<Account> existingAccounts = [SELECT Id FROM Account WHERE Name = :accountName LIMIT 1];
Account acc;
if (existingAccounts.isEmpty()) {
acc = new Account(Name = accountName,phone = contactPhone,recordtypeId = renovateAccountRecordTypeId);
insert acc;
responseMap.put('AccountCreated', true);
responseMap.put('AccountId', acc.id);
} else {
acc = existingAccounts[0];
responseMap.put('AccountMessage', 'Account Already Existed');
responseMap.put('ExistedAccountId', acc.id);
}
// Query to check if Contact with the given name and AccountId exists
List<Contact> existingContacts = [SELECT Id FROM Contact WHERE FirstName = :contactFirstName AND LastName = :contactLastName AND AccountId = :acc.Id LIMIT 1];
Contact con;
if (existingContacts.isEmpty()) {
con = new Contact(FirstName = contactFirstName, LastName = contactLastName, AccountId = acc.Id, Email=userEmail);
insert con;
responseMap.put('ContactCreated', true);
responseMap.put('ContactId', con.id);
//create User
createPortalUser(acc.Id,con.Id, contactFirstName, contactLastName, userEmail);
} else {
con = existingContacts[0];
responseMap.put('ContactMessage', 'Contact Already Existed');
responseMap.put('ExistedContactId', con.id);
createPortalUser(acc.Id,con.Id, contactFirstName, contactLastName, userEmail);
}
RestContext.response.addHeader('Content-Type', 'application/json');
RestContext.response.responseBody = Blob.valueOf(JSON.serialize(responseMap));
}
@future
public static void createPortalUser(Id accId, Id contactId, String contactFirstName, String contactLastName, String userEmail) {
User existingUser = null;
String renovateCustomerProfileName = System.label.renovationProfileName;
Id renovateCustomerProfileId = getProfileIdByName(renovateCustomerProfileName);
try {
// Query to check if User with the given ContactId exists
List<User> existingUsers = [SELECT Id, IsActive FROM User WHERE ContactId = :contactId LIMIT 1];
system.debug(existingUsers+'<<<'+contactId);
if (!existingUsers.isEmpty()) {
existingUser = existingUsers[0];
}
if (existingUser != null) {
if (!existingUser.IsActive) {
existingUser.IsActive = true;
try {
update existingUser;
} catch (DmlException e) {
sendExceptionEmail(e);
}
}
} else {
User usr = new User(
FirstName = contactFirstName,
LastName = contactLastName,
Email = userEmail,
Username = userEmail,
Alias = 'alias',
ProfileId = renovateCustomerProfileId,
TimeZoneSidKey = 'America/Phoenix',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US',
ContactId = contactId
);
try {
insert usr;
} catch (DmlException e) {
sendExceptionEmail(e);
system.debug('exception' + e.getMessage());
}
}
} catch (QueryException e) {
system.debug('exception' + e.getMessage());
sendExceptionEmail(e);
}
}
@TestVisible
private static void sendExceptionEmail(Exception e) {
String toAddress = System.Label.renovateExceptionEmailAddress;
// Send exception details to the specified email address
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {toAddress};
mail.setToAddresses(toAddresses);
mail.setSubject('Exception in RenovateCustomerAPI');
mail.setPlainTextBody('An exception occurred in RenovateCustomerAPI: ' + e.getMessage());
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
@TestVisible
private static Id getProfileIdByName(String profileName) {
Id profileId;
Profile prof = [SELECT Id FROM Profile WHERE Name = :profileName LIMIT 1];
profileId = prof.Id;
return profileId;
}
}
我尝试在 apex 类中创建角色,但出现了混合 dml 异常,测试类也失败了
当您尝试使用同一方法在设置和自定义对象上创建 dml 时,会发生混合 dml 异常。
我用来规避它的一种方法是使用您的 dml 创建一个新方法并从起始方法调用它
除此之外,您可能想找到问题的根源并解决它,我假设您已经检查了用户对象的验证规则?请检查生产环境,因为它可能未与开发环境同步和“对齐”