Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I developed my code in my DE and created a custom profile named "XYZ". I used that profile in my Apex class and test methods.

While installing that managed beta package in my sandbox my test method failed because that profile is not visible.

Can I create a custom profile in the test method to ensure success?

share|improve this question
You can create the profile first in sandbox and then map that profile with the packaged profile during installation. – doga Dec 6 '12 at 6:54
your answer can solve my problem but my question is when i need to use the app in any ORG i need to create first profile in that ORG? or is there any way to create custom profile in test method. – Axaykumar Varu Dec 6 '12 at 7:00
I think salesforce doesn't allow creation of profiles during installation. We can think of this as suppose target org is already having a profile with same name as your profile and if your profile overrides that profile then it might lead some of components not working properly. – doga Dec 6 '12 at 8:12
Is your profile included in the package ? Is anything preventing you from using a standard profile ? – Samuel De Rycke Dec 6 '12 at 8:32
Your test should not be running during install these days unless your at an older API level or using the IsTest(OnInstall=true) annotation. If your happy for this to be a regression test in your packaging org and not something that is needed to be retested in the subscriber org. You can either upgrade to the lastest API for your test (since the default is not to run on install these days) or remove the annotation if your using it. – Andrew Fawcett Dec 7 '12 at 9:21
up vote 3 down vote accepted

No you cannot create profiles in apex code, either through tests or otherwise, this is presumably a security limitation.

The second part of your problem is something I have also encountered. You cannot include a profile in a Managed Package. You can include 'Profile Settings' which can be applied to a subscribers existing profiles. But the name of the subscribers profile will not always match the name of the profile you are referencing in your unit test.

What I have done to eliminate this problem in the past is rather than referencing a profile in code, I use the Schema Describe information to determine the object or field level permissions that implicitly grant the user access. For example if a user has write permission on my Milestone Object, my code lets them perform a priviledged action on the Milestone

share|improve this answer

Have you tried this: @isTest (SeeAllData = true) Here some info

share|improve this answer
yes, but this can not solve the problem because custom profile could not found at the time of installation. – Axaykumar Varu Dec 6 '12 at 7:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.