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
Shikha AgashiShikha Agashi 

network.getnetworkid() = null in test class. How to define it for trigger?

trigger EventTrigger on Event (before Insert, before update) {
        public static String communityId=Network.getNetworkId();
        
        if(trigger.isInsert){
            if(communityID!=null){ 
                String CommunityName=ConnectApi.Communities.getCommunity(communityId).name;
                //Update public calendar events for HR4HR community
                if(CommunityName=='HR4HR'){
                   publicCalendar__c pc = [SELECT ID__c FROM publiccalendar__c WHERE Name=: 'HR4HR Community Calendar'];
                   
                   for(Event e:trigger.new){
                       e.OwnerID=pc.ID__c;
                   }
                }
                
                //Update public calendar events for Tech Exchange community
                if(CommunityName=='TechExchange'){
                   publicCalendar__c pc = [SELECT ID__c FROM publiccalendar__c WHERE Name=: 'TechExchange Community Calendar'];
                   for(Event e:trigger.new){
                       e.OwnerID=pc.ID__c;
                   }
                }
            }
        }
        
        if(trigger.isUpdate){
           if(CommunityID!=null){
                for(Event e:trigger.New){
                    if(!(Trigger.OldMap.get(e.id).ownerId==e.ownerId)){
                        e.ownerID.addError('Assigned To field cannot be edited');
                    }    
                }
                
               
            }
            
        }

}

Above is my class and below is my test class.
 
@isTest(seeallData=true)
 public class EventTriggerTest{
  
  
  public static testMethod void testEventTrigger(){
        Network community;
        String communityId;   
        
        Id currentUserId = UserInfo.getUserId();    
        User usr = [select id,Name from User where id =: currentUserId]; 
        
   
     system.runAs(usr){
    community = [SELECT id, Name,OptionsReputationEnabled FROM Network where name =: 'TechExchange'];
        communityId = community.id;
     
    system.debug('Test User Inside Network'+network.getnetworkid());
       
      Event eve = new Event(
            // OwnerId= usr.Id,
             ActivityDate=system.today(),
             StartDateTime=system.now().addhours(35),
             EndDateTime=system.now().addhours(36),
             Subject='Test',
             Description='Testing the event controller'
                               );
      insert eve;
      
      Event e = [Select id, Subject from Event where id=:eve.id];
       
       System.assertEquals('Test', e.subject);
       e.subject='New';
       update e;
       
         
    }

     
  
  }
   
 
  

 }

 
Rishabh Agrawal 18Rishabh Agrawal 18
Understood.  The problem is that the user you are using is not associated with a network.  What I would suggest is that -
You have to modify your user query so that you can get the user record with valid network.
                                            OR
You'll need to create createNetwork, createPortalUser, and createNetworkMember methods that create a new Network, a new User and a new link between them.  Then use that User in your test.​
Saurabh Tripathi 18Saurabh Tripathi 18
I had similar issue, had to query on NetworkMember object in order to pass network Id for external uisers in test class, hope it helps other who have similar issue - 

[SELECT NetworkId FROM NetworkMember limit 1][0].NetworkId;

The above query will give you the network Id you can use, instead of Network.getNetworkId()

Thanks