kindly let me know how to create the test data for the network object in test class. Below is the class and test class, kindly help. There are no methods only constructor code, apart from some variables. Apex:-

public with sharing class Live_Agent_Button_Controller {

    public Live_Agent_Button_Controller() {
        // Get the Live Agent Config Information
        errorMsg = '';
        Live_Agent_Setting__c targetLVS;
        String networkId = Network.getNetworkId();

        if(String.isBlank(networkId)) {
            errorMsg = ERROR_MSG_NETWORK_ID_NULL;
        } else {
            List<Live_Agent_Setting__c> lvss = [
                SELECT Authenticated_User_Chat_Button__c, Community__c, 
                    Deployment_JS_URL__c, Unauthenticated_User_Chat_Button__c,
                    Deployment_Id__c, Deployment_Chat_URL__c, Salesforce_com_Organization_ID__c
                FROM Live_Agent_Setting__c
                WHERE Community__c =: Network.getLoginUrl(networkId)

            if(lvss.isEmpty()) {
                errorMsg = ERROR_MSG_LIVE_AGENT_SETTING;
            } else {
                // We grab the first one because the Community__c is unqiue
                targetLVS = lvss[0];
                deploymentJSURL = targetLVS.Deployment_JS_URL__c;

        if(String.isNotBlank(errorMsg)) {
            //EmailServices.sendEmailToAdmin(ERROR_EMAIL_SUBJECT, errorMsg);
        } else {
            // Get the running user information
            List<User> runningUsers = [
                SELECT Id, ContactId, Contact.FirstName, Contact.LastName, Contact.Email
                FROM User 
                WHERE Id =: UserInfo.getUserId()

            User runningUser = runningUsers[0];

Test class:-

public static testmethod void Test_1(){
    Account    a = new Account(name = 'TEST ACCOUNT');
    insert a;

    contact c = new Contact(AccountId = a.id, lastname = 'lastname');
    insert c;

    Profile p = [select id, name from profile 
                     where usertype = 'CSPLitePortal' limit 1];  

    String testemail = 'puser000@amamama.com';
    User pu = new User(profileId = p.id, username = testemail, email = testemail, 
                           emailencodingkey = 'UTF-8', localesidkey = 'en_US', 
                           languagelocalekey = 'en_US', timezonesidkey = 'America/Los_Angeles', 
                           alias='cspu', lastname='lastname', contactId = c.id);


    //get the network membership to get the correct network ID
    NetworkMember membership = [SELECT networkID, memberID FROM NetworkMember 
                                                    WHERE memberID = :pu.id];

    Live_Agent_Setting__c livagentset= new Live_Agent_Setting__c();
    insert livagentset;
    Live_Agent_Button_Controller agntbut= new Live_Agent_Button_Controller();


Test class is failing at the soql in test class on NetworkMember object.

Error:- List has no rows for assignment

I can understand there is no data created for Network/NetworkMember object.

  • For questions like these, the SOAP API documentation can come in handy (even if you're not using the SOAP API). The SOAP API NetworkMember documentation seems to indicate that we are unable to insert instances of this SObject (create() is not in the list of supported calls). – Derek F Jul 31 '18 at 12:42
  • @derek......hmmmm...i can see that. Thnx for the link. but there has to be a workaround for it. In what other way ,we can do that.? – vineet Jul 31 '18 at 12:46
  • I'm not entirely sure (which is why I did not write an answer for this question). It could be that NetworkMember is a "setup object" for Salesforce communities and you could query existing NetworkMember records in your org. You might need to use test.loadData(). It could also be that code that relies on NetworkMember is essentially untestable (like code that relies on standard history objects). – Derek F Jul 31 '18 at 12:58
  • Is CSPLitePortal, name of a community profile? – Jayant Das Jul 31 '18 at 13:57

You cannot create data for Network as it represents a Community, and your tests will revolve around an existing community. Similarly NetworkMember represents the members of that Community.

This is how I got your code work to achieve what you are trying to. I have utilized similar approaches in the past for testing around Community.

For your test, as a pre-requisite, make sure that the profile that you are trying to deal with in your test class is added as a member in the Community.

I used the Profile Name here instead of the User Type to fetch the related community profile. So your query for fetching profile will be something as:

Profile p = [select id, name from profile where name = 'my community profile name' limit 1]; 

Now, while fetching the NetworkMember, my query will be as:

List<NetworkMember> membership = [SELECT network.name, networkId, memberID FROM NetworkMember WHERE memberID = :pu.id];

Notice that I have used a List here as the profile name as mentioned above can be a member of different community too (remember, it's the profile/permission sets which are member of the community, so all Users on those profile/permission set in turn are members) and thus the membership will always return more than one record.

Now, you can fetch the correct Network (Community) based on your requirements by traversing through this list, something as:

for(NetworkMember m : membership) {
    // get the correct network here
    // perform other operations
  • kewl....yes you are correct ,profile should be of community user. Thnx a lot. – vineet Jul 31 '18 at 14:34

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.