so I'm getting this error below. Seems like its pretty explanatory right? The thing is that I have another class that actually inserts the user, and the test class on that works fine. All this trigger is doing is inserting a contact that matches the info of the Case. I've included my test class & trigger below, I figured this would be an easy fix for someone that knows more about this than me, maybe my eyes are just failing me!
trigger CreateContactonCase on Case (before insert, before update) {
if(Trigger.isInsert){
List<Contact> ct = new List <Contact>();
for(Case acc : trigger.new){
Contact c = new Contact
(LastName = acc.Contact_Last_Name__c,
Account=acc.Account,
FirstName = acc.Contact_First_Name__c,
//AccountId=acc.id,
//Account= acc.Account_Name_If_not_in_Salesforce__c,
MailingStreet=acc.Account_Address__c,
/* MailingCity=acc.BillingCity,
/* MailingState=acc.BillingState,
MailingPostalCode=acc.BillingPostalCode,
MailingCountry=acc.BillingCountry,*/
//Name=acc.Contact,
Role__c=acc.Contact_Role__c,
Title=acc.Title__c,
Phone=acc.Contact_Phone__c,
CIS_Contact__c= false,
Contact_Not_Verified_del__c = false,
// ManageExternalUser = true,
Email=acc.Contact_Email__c);
//update c;
ct.add(c);
acc.Status ='Closed';
acc.Portal_User_Created__c = true;
//acc.Completed_By__c = System.user;
acc.Completed_Date__c = System.today();
}
insert ct;
update ct;
}
}
@IsTest
public class CreateContactonCase_TST {
static testmethod void CreateContactonCase () {
Account a = new account (name ='testaccount');
insert a;
Case acc = new Case
(Contact_Last_Name__c = 'TestLastname',
Account= [SELECT id, name FROM Account WHERE name = 'test account'],
Contact_First_Name__c = 'TestFirstName',
//AccountId=acc.id,
//Account= acc.Account_Name_If_not_in_Salesforce__c,
Account_Address__c='TestAddress',
/* MailingCity=acc.BillingCity,
/* MailingState=acc.BillingState,
MailingPostalCode=acc.BillingPostalCode,
MailingCountry=acc.BillingCountry,*/
//Name=acc.Contact,
Contact_Role__c='TestRole',
Title__c='TestTitle',
Contact_Phone__c='12345678',
//CIS_Contact__c= false,
//Contact_Not_Verified_del__c = false,
// ManageExternalUser = true,
Contact_Email__c='testemail@email.com');
insert acc;
}
List<Contact> c = new List <Contact>();
List<Case> cs = new List <Case>();
ERROR MSG
System.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, portal account owner must have a role: []
ADDED
thanks for the reply!!
So I went ahead and made some editions to both the trigger and the method where the error was reporting from. And now i'm getting this error. Basically I added to my Test Class:
UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
User portalAccountOwner1 = new User(
UserRoleId = portalRole.Id,
ProfileId = profile1.Id,
Username = System.now().millisecond() + 'test3@test.com',
Alias = 'batman',
Email='bruce.wayne@wayneenterprises.com',
EmailEncodingKey='UTF-8',
Firstname='Bruce',
Lastname='Wayne',
LanguageLocaleKey='en_US',
LocaleSidKey='en_US',
TimeZoneSidKey='America/Chicago'
);
This was to insert a user so I could use the system run as method
Next I put this in my actual method:
@future public static void createUser(Id ContactId, string UserRoleId, String email, String firstName, String lastName, String userName, String profileId, boolean IsPortalEnabled) {
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.EmailHeader.triggerUserEmail = true;
Profile p = [SELECT Id FROM Profile WHERE Name='Partner Community User Custom'];
**UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];**
User u = new User(alias = 'standt', email=email, CommunityNickname =lastname,
emailencodingkey='UTF-8', lastname=lastname, languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id , contactId=contactId, **UserRoleId=PortalRole.Id**,
timezonesidkey='America/Los_Angeles', //IsPortalEnabled = false,
username=email);
u.setOptions(dmo);
insert u;
(I only added the SOQL statement and the UserRoleId = PortalRole.Id)
I thought this would be sufficient, but I am now getting this error: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, High Volume Portal Users cannot have a user role: [UserRoleId]