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)) {
// RAHIM - COMMENT THIS OUT FOR NOW SINCE IT IS PREVENTING CALLOUTS TO OCCUR THAT ARE IN THE SAME EXECUTION AS THIS
//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 = '[email protected]';
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);
Database.insert(pu);
//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();
livagentset.Authenticated_User_Chat_Button__c='chatbutton';
livagentset.Authenticated_User_Chat_Button__c='chatbutton';
livagentset.Community__c=Network.getLoginUrl(membership.networkId);
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.
create()
is not in the list of supported calls).NetworkMember
is a "setup object" for Salesforce communities and you could query existingNetworkMember
records in your org. You might need to usetest.loadData()
. It could also be that code that relies onNetworkMember
is essentially untestable (like code that relies on standard history objects).