Jrud
Jrud

Reputation: 1004

How do I add a salesforce community user using the API?

I've downloaded the API WSDL and added it to my C# project and have many successful integrations running throught it both bringing down and upserting data. I'm having a lot of trouble with creating community user accounts for our learning management system though.

I can't tell if I need to supply an account, or a contact, or a user type, and no matter what I try I end up with an error that doesn't lead me to a logical fix.

I think i'm closest with the below configuration, which is giving me this error: "You can't create a contact for this user because the org doesn't have the necessary permissions. Contact Salesforce Customer Support for help."

This is my C# code I'm trying to create a User account with:

    public UpsertResponse UpsertLearningUser()
    {
        SFProd.User U = new SFProd.User();

        U.Id = UserID;
        U.Username = UserName;
        U.Alias = TechCode;
        U.CommunityNickname = FirstName + "." + LastName;
        U.FirstName = FirstName;
        U.LastName = LastName;
        U.MiddleName = MiddleName;
        U.Title = Title;
        U.Department = Department;
        U.Division = Division;
        U.IsActive = Active;
        U.IsActiveSpecified = true;
        U.UserPermissionsChatterAnswersUser = true;
        U.UserPermissionsChatterAnswersUserSpecified = true;
        //U.UserPermissionsMobileUser = true;
        //U.UserPermissionsMobileUserSpecified = true;
        U.Phone = Phone;
        U.EmployeeNumber = ADPNumber;
        U.NS_Internal_ID__c = NSEmployeeID.ToString();
        U.Employee_ID__c = double.Parse(EmployeeID.ToString());
        U.Employee_ID__cSpecified = true;
        U.Zone__c = Department;
        U.Region__c = Division;
        U.Certification_Level__c = CertificationLevel;
        if (CertificationLevelDate != DateTime.Parse("1/1/1900"))
        { 
            U.Certification_Level_Date__c = CertificationLevelDate;
            U.Certification_Level_Date__cSpecified = true;
        }
        U.Country = Country;
        U.State = State;
        U.City = City;
        U.PostalCode = PostalCode;
        U.Street = Street;
        U.Email = Email;
        U.TimeZoneSidKey = "America/Chicago";
        U.EmailEncodingKey = "ISO-8859-1";
        U.ProfileId = "00ef10000016tfvAAA";
        U.LanguageLocaleKey = "en_US";
        U.ContactId = ContactID;

        List<SFProd.sObject> Objs = new List<SFProd.sObject>();

        Objs.Add(U);

        return new UpsertResponse(SFConnection.RunUpsert("Username", Objs)[0], UserID);
    }

Has anyone else done this sucessfully? or have any idea what I'm doing wrong?

Upvotes: 0

Views: 1827

Answers (1)

eyewell
eyewell

Reputation: 51

There is a special function for creating external users. Try a variation of the CreateExternalUser() function: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_sites.htm

Upvotes: 2

Related Questions