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
Surya Chandra Rao GandreddiSurya Chandra Rao Gandreddi 

Network - Communities and No-Communities

I've two thing for Communities & No-Communities.
1 - To post a Feed to a User in Communities - https://help.salesforce.com/apex/HTViewSolution?id=000187667&language=en_US This works fine for Community User & Non-Community User.
FeedItem post = new FeedItem(ParentId = UserInfo.getUserId(), CreatedById = thanks.GiverId, Body = thanks.Message, RelatedRecordId = thanks.Id, Type = 'RypplePost');

        String networkId = Network.getNetworkId();
        if(networkId != null) {
            post.NetworkScope = networkId;
        }

        insert post;

This works fine in Communities, but breaks when Communities are not enabled. If I comment out 'post.NetworkScope = networkId;' - the package installs fine, if not I get the Installation Error as below:
2 - Images in Community How to include images from static resources in lightning components used in communities?
String networkId = Network.getNetworkId();

if(networkId != null) {
 List<Network> networks = [ Select UrlPathPrefix from Network where Id = :networkId ];
 if(networks.size() > 0) {
   return '/' + networks[0].UrlPathPrefix;
 }
}
return '';


I'm appending that UrlPathPrefix to the Images on communities when UrlPathPrefix != ''. I think Select UrlPathPrefix from Network where Id = :networkId is causing the below error.
Missing Organization Feature: Networks null
Missing Organization Feature: NetworksEnabledOnce null

Thanks.
NagendraNagendra (Salesforce Developers) 
Hi Surya,

I think the issue is because even to use Network class ,the Network Object should be available in the org .Also, it depends if your class is using with sharing or without sharing .

With sharing means the package installation context will obey the installing user privileges else it will be a ghost user .

You can follow below methodology to make sure communities is enabled for all community-related logic
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
if(gd.containsKey('Network')){
   system.debug('Communities is enabled!');
  return true;
}else{
  system.debug('Communities is disabled!');
  return false;
}
Kindly mark this post as solved if the information help's so that it gets removed from the unanswered queue and becomes a proper solution which results in helping others who are really in need of it.

Best Regards,
Nagendra.P