You need to sign in to do that
Don't have an account?
Rajar Kumar
System.DmlException: Insert failed. role type must match user type
Hi Guys,
I am getting this error while inserting user in the test class.
System.DmlException: Insert failed. role type must match user type.
Please find the below code.
Set<String> customerUserTypes = new Set<String> {'CSPLiteUser', 'PowerPartner', 'PowerCustomerSuccess', 'CustomerSuccess'};
Profile portalProfile = [SELECT Id FROM Profile WHERE UserType in :customerUserTypes Limit 1];
UserRole role = [Select Id From UserRole Where PortalType = :'CustomerPortal' Limit 1];
User user1 = new User(
Username = System.now().millisecond() + 'test12345@test.com',
ContactId = contact1.Id,
ProfileId = portalProfile.Id,
UserRoleId = role.Id,
Alias = 'test56',
Email = 'test12345@test.com',
EmailEncodingKey = 'UTF-8',
LastName = 'McTesty',
CommunityNickname = 'test12345',
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US'
);
Database.insert(user1);
I am getting this error while inserting user in the test class.
System.DmlException: Insert failed. role type must match user type.
Please find the below code.
Set<String> customerUserTypes = new Set<String> {'CSPLiteUser', 'PowerPartner', 'PowerCustomerSuccess', 'CustomerSuccess'};
Profile portalProfile = [SELECT Id FROM Profile WHERE UserType in :customerUserTypes Limit 1];
UserRole role = [Select Id From UserRole Where PortalType = :'CustomerPortal' Limit 1];
User user1 = new User(
Username = System.now().millisecond() + 'test12345@test.com',
ContactId = contact1.Id,
ProfileId = portalProfile.Id,
UserRoleId = role.Id,
Alias = 'test56',
Email = 'test12345@test.com',
EmailEncodingKey = 'UTF-8',
LastName = 'McTesty',
CommunityNickname = 'test12345',
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US'
);
Database.insert(user1);
You have to create account,contact first.
Please refer below links which might help you in this
https://developer.salesforce.com/forums/?id=906F000000091RxIAI
https://developer.salesforce.com/forums/?id=906F00000009266IAA
Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.
Thanks and Regards
RecordType rt = [Select Id From RecordType Where DeveloperName = 'Staff' limit 1];
RecordType accountRT = [Select Id From RecordType Where DeveloperName = 'organization' limit 1];
UserRole r1 = [SELECT Id FROM UserRole WHERE DeveloperName = 'YourUserRole' limit 1];
Profile portalProfile = [Select Id from Profile where name = 'Your Operator Portal User'];
Contact portalContact;
User thisUser = new User(Id=UserInfo.getUserId());
thisUser.UserRoleId = r1.Id;
update thisUser;
System.runAs(thisUser) {
Account portalAccount = new Account(name = 'portalAccount',RecordTypeId=accountRT.Id);//create a portal account first
insert portalAccount;
portalContact = new contact(LastName = 'portalContact', AccountId = portalAccount.Id,RecordTypeId = rt.Id); //create a portal contact
insert portalContact;
}
User u1 = new User( email='test@test.com',
profileid = portalProfile.Id,
UserName='portalUser@portalTestUser.com',
Alias = 'a',
TimeZoneSidKey='Australia/Sydney',
EmailEncodingKey='ISO-8859-1',
LocaleSidKey='en_US',
LanguageLocaleKey='en_US',
ContactId = portalContact.Id,
FirstName = 'Test FirstName',
LastName = 'LastName'
);
insert u1;
Hope this helps!
If this helps kindly mark it as solved so that it may help others in future.
Thanks,
Parmanand