+ Start a Discussion

How TO add a new UserRole Record in test class

I need to create  a UserRole record as part of test class. The field 'PortalType' is a picklist defined field. I've seen several examples of reading the values from the picklist but no examples of how to create a new record containing a picklist field. not a new value, but a existing value within the picklist for this note new UserRole record. For example, this snippet will result in the error :'Field PortalType is not writable'. What is the correct procedure?





UserRole r = new UserRole(RollupDescription =a.Name + ' Customer Manager',PortalType = 'Customer Portal', PortalRole = 'Manager', PortalAccountId = a.Id); insert r;



I'm not sure you can insert UserRole in testmethod (maybe you can).

In this case, as it's saying, you can't specify PortalType by yourself. It's something only SFDC can update. In this case, leave the field to SFDC, and specify only creatable fields and control the un-writable fields from other angle. You can see it's creatable/writable in Force.com IDE schema view (see "Access" atributes in each field).

However, for this specific case for "Portal" information, I don't know how to do.


If your question is how to add a new UserRole in test class, the answer is to write a right insert DML without specifying un-writable field. If your question is how to add a new UserRole with PortalType, sorry, I don't know.



Message Edited by ThomasTT on 09-29-2009 12:26 PM

Thank you for repsonse Thomas. I do need to set the type to Portal user so I'll need to keep investigating. Thanks you again.



You shouldn't have a space in 'Customer Portal'. It should be PortalType = 'CustomerPortal'

Was this resolved?


I am having the same issue, pretty much


Account a = new Account (.....);
insert a;

UserRole ur = new UserRole(PortalType = 'CustomerPortal', PortalAccountId = a.Id);
insert ur

 Here is the error it returns:


|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PortalType (Account is not enabled for this portal type): [PortalType]



David JorjaniDavid Jorjani
Did anyone resolve this issue? I'm having the same problem.