function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Varsha Pawar 4Varsha Pawar 4 

Partner Account in test class

Hi Team,

In my Org, I have enabled Partner Account. On Account Trigger is has some logic, now I want to write a Test Class for this Trigger.

Can someone please help me how to make IsPartner = True/False in Test Class? Currently it gives error "FATAL_ERROR System.SObjectException: Field is not writeable: Account.IsPartner".

Thanks in Advance! 
SwethaSwetha (Salesforce Developers) 
HI Varsha,

Please see https://developer.salesforce.com/forums/ForumsMain?id=906F000000091RxIAI

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BJCb

that explains the same scenario and fix for the test class.
 
Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful.
 
Thank you
Varsha Pawar 4Varsha Pawar 4
Thanks Swetha! I have already checked this link but not able to understand. I'm new to Salesforce. Can you please help me?
SwethaSwetha (Salesforce Developers) 
HI Varsha,
As per https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_account.htm can you IsPartner Indicates whether the account has at least one contact enabled to use the organization's partner portal (true) or not (false).

Can you alter your test class code to match the below and let me know if it works?

You need to Create User by 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');

Incase you have further queries, please post your narrowed code snippet so that I can take a look.

Thanks