This is my code.But am getting an error i.e

FATAL_ERROR|System.TypeException: That operation is only allowed from within an active site. Class.System.Site.createPortalUser.

Please correct my error

global with sharing class portalcreatenew
global static string newportalcreation()
    RestRequest req=RestContext.request;
   // req.addParameter('userId',userId);
    RestResponse res=RestContext.response;
   // req.addParameter('userId',userId);

    Blob Body = req.requestBody;
    Wrapperclss reg = portalcreatenew.parse(Body.tostring());
   // system.debug('InputRequest::'+Body.tostring());
    Wrapperclss container = (Wrapperclss)System.JSON.deserialize(RestContext.request.requestBody.tostring(),Wrapperclss.class);
    User thisUser = [select Id from User where Id = :UserInfo.getUserId()];
    ID ProfileId = [Select id from Profile where name = 'Customer Portal Manager Custom'].id;
    Account A1 = new Account(Name = 'Test Acct', OwnerId=thisUser.Id );
    insert A1;
    Contact C1 = new Contact( AccountID = A1.id, firstName = 'TestTest', lastName = 'UserUser',email = 'harish.cherry484@gmail.com');
    insert C1; 
    User newUser = new User();
    newUser.Username = reg.Username;
   // newUser.LastName = C1.lastname;
    newUser.LastName = reg.Lastname;
    newUser.Email = reg.Email;
    //newUser.password = reg.password;
    newUser.Alias = reg.Alias;
    newUser.CommunityNickName = reg.CommunityNickName;
    newUser.TimeZoneSidKey = reg.TimeZoneSidKey;
    newUser.LocaleSidKey = reg.LocaleSidKey;
    newUser.EmailEncodingKey = reg.EmailEncodingKey;
    newUser.ProfileId = ProfileId;
    newUser.LanguageLocaleKey = reg.LanguageLocaleKey;
    newUser.ContactId = C1.id;
    string userId = Site.createPortalUser(newUser, A1.id, 'harish.8392', true);
    insert newUser;
    return 'User Created! Id Is: ' + newUser.Id;
    public static Wrapperclss parse(string reg)
    global class Wrapperclss
            public String Username{get;set;}
            public String LastName{get;set;}
            public String Email{get;set;} 
            public String password{get; set {password = value == null ? value : value.trim(); } }
            public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
            public String Alias{get;set;}
            public String CommunityNickName{get;set;}
            public String TimeZoneSidKey{get;set;}
            public String LocaleSidKey{get;set;}
            public String EmailEncodingKey{get;set;}
            public String ProfileId{get;set;}
            public String LanguageLocaleKey{get;set;}
            public String ContactId{get;set;}
  • Make the community active . Open active community URL in browser and test it from there, it will work. Createportaluser will not work, when you directly open the VF page in your salesforce org session.
    – Amol
    Dec 18, 2017 at 5:54

1 Answer 1


When you tried to execute site methods as a Internal user then community will throw exception.

Make the community as active. open the community URL in browser and test it from community site page. It will not throw error.

Site.createPortalUser() is only work from community site pages.

Follow the Steps to Activate the Community:

Setup-->All Communities--> click on Workspaces for respective community --> Administraton --> Setting --> Click on Active Community button.

You can access it by hitting community URL and try registration functionality.

  • Am Creating Customer Portal .......,what is the need to activate communities
    – Harish
    Dec 18, 2017 at 7:29
  • @Harish check my updated answer
    – Amol
    Dec 18, 2017 at 8:30
  • HI Amol....,I activated communities but still am getting same error only. could u check this code in your DEV edition
    – Harish
    Dec 18, 2017 at 10:02
  • @Harish make sure you are testing it from community link. you need to pass existing account Id and portal create a contact for you, you don't need to create contact code. you can compare your code with developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – Amol
    Dec 18, 2017 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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