I am trying to create a list of contacts and add these to the community as users. I keep getting the error:
MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): User, original object: Contact: []
I have walked trough some similar questions but could not apply these to my case. Could someone please explain.
Apex
public without sharing class addController {
@AuraEnabled
public static void saveContacts(List<Contact> listContact){
Insert listContact;
system.debug('listContact '+ listContact);
User currentUser = [SELECT Id, Contact.AccountId FROM User WHERE Id =: UserInfo.getUserId() LIMIT 1];
system.debug('currentUser '+ currentUser);
List<User> uList= new List<User>();
List<Relatie__c> rel= new List<Relatie__c>();
UserRole ur = [SELECT Id FROM UserRole Where name = 'Partner'];
Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Customer Community User' LIMIT 1];
for(Contact con : listContact){
Relatie__c rc = new Relatie__c(Account__c = currentUser.contact.AccountId, Contactpersoon__c = con.id ) ;
system.debug('rc '+ rc);
rel.add(rc);
string alias = con.firstName.substring(0,1) + con.lastName.substring(0,1);
user u = New user(userName = con.firstName+'.'+con.lastName+'@test.nl' ,
firstName = con.firstName,
lastName = con.lastName,
alias = alias,
email = con.Email,
communityNickName = alias,
timeZoneSidKey = 'Europe/Amsterdam',
LocaleSidKey = 'nl_NL',
EmailEncodingKey = 'ISO-8859-1',
LanguageLocaleKey = 'nl_NL',
ContactID = con.id,
ProfileId = profileId.id,
UserRoleId = ur.id
);
uList.add(u); }
insert rel;
system.debug('rel '+ rel);
insert uList;
}
}