My need would be to create a Partner Account (that is: an Account with field isPartner set to true!) in test class, having checkbox PartnerAccount set to true, but I'm not having success with this requirement..and my debug code returns "false". Any suggestions?

Account acc= new Account(Name='Partner Account');
    insert acc;     
    Contact con = new Contact (LastName='TestContact', AccountId=acc.Id); 
    insert con;     

    Profile p2 = [SELECT Id From Profile where Name='ETH Partner Community User'];

    User u2 = new User(
    Alias = 'newUser',
    IsActive = true,
    ProfileId = p2.Id, 
    ContactId = con.Id);

    insert u2;
    system.debug('@@' + u2.Contact.Account.isPartner);
  • 1
    Your code makes it appear as though you're trying to create a partner community user. To do that, the contact needs to be related to a partner account. You first need to create the account and convert the account to a partner. Right now, you're trying to apply a partner profile to a contact that's related to a non partner account.
    – crmprogdev
    Feb 25, 2015 at 16:19
  • So my question is : how to make the Account become Partner by code?
    – user9959
    Feb 25, 2015 at 16:36
  • It depends on the kind of partner account you want. See the Object Reference for more on Accounts, AccountPartner, OpportuntyPartner, Partner, PartnerRole and other objects like Contact and User that might be relevant to your issue.
    – crmprogdev
    Feb 25, 2015 at 16:55
  • I need only that the Account has the isPartner (standard field) checkbox set to "true". This is a class test for a trigger that performs action on this checkbox value basis
    – user9959
    Feb 25, 2015 at 17:13
  • If you think that's all you need, then try setting it to true and updating the account in your test class.
    – crmprogdev
    Feb 25, 2015 at 18:50

3 Answers 3


Per the documentation the isPartner field:

Indicates whether the account has at least one contact enabled to use the organization's partner portal (true) or not (false).

So if you create the user correctly then associate them with an account you will be good to go:

Create User - Passing in the contact ID

Account a = new Account();
insert a;
Contact c = New Contact(LastName = 'Test', AccountID = a.id);
insert c;
User newUser = createPartnerUser(c.id);

private static user createPartnerUser(ID cId){
    Profile p = [Select ID, Name from Profile Where Name = 'YOUR PARTNER PROFILE NAME'];

    user u = New User(
        UserName = 'test_' + math.random() + '@test.com',
        FirstName = 'Test-First',
        LastName = 'Test-Last',
        Alias = 'test',
        email = 'test' + math.random() + '@test.com',
        CommunityNickName = string.valueOf(math.random()).substring(0,6),
        ProfileID = p.id,
        TimeZoneSidKey = 'America/New_York', 
        LocaleSidKey = 'en_US', 
        EmailEncodingKey = 'UTF-8', 
        LanguageLocaleKey = 'en_US',
        ContactID = cId


    insert u;

    return u;


a = [Select isPartner From Account where ID = :a.id];
system.assert(a.isPartner,'Is Partner flag was not set to true');

Modify per your needs


After creating first user with profile equals to 'Partner Community User', and linking this user contact to account, IsPartner field will turn to true automatic


You can set IsPartner in a second update. You can't set it on creation of the account but after it's inserted you can.

Account a = new Account('My account'); insert a; a.IsPartner = true; update a;

I've had issues where if I don't do this and rely on the contact being a partner user the account will have IsPartner set to true but trying to assign it to records that require a partner account fails. For example trying to set the PartnerId on PartnerFundAllocation will fail unless you explicitly update IsPartner to true.

Your Answer

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