You need to sign in to do that
Don't have an account?

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; } } }
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.
[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