+ Start a Discussion
Hari Sfdc 7Hari Sfdc 7 

Trigger to count number of Account PublicGroups

Dear Team,
 
I want to know count the number of public group(s)(starts with P) an account is shared with. Can anyone please help us to creating a new trigger.
 
Let ‘say: In one account we have number of public group’s start with P. So we want to count the public groups which are start with P. The count will be populate field called as "PublicGroup_Count__c"
Thanks
 
GauravGargGauravGarg
Hi Hari,

Public Groups are not linked with any Object. How can we Identity if Public Group (p) is on account?

Thanks,
Gaurav
 
Hari Sfdc 7Hari Sfdc 7
Hi Guarav,

Thaks for your reply.Based on Account share object can we i think so?

Thanks,

 
Akshay_DhimanAkshay_Dhiman
Hi Hari,
Here is the solution according to what I have understood about your problem, I have created a custom object Public_Group__c which has the lookup of account and as per your requirement there is a custom field PublicGroup_Count__c on Account object. PublicGroup_Count__c  field stores the number of Public_Group__c (Name starts with p) in a particular account.
Try this code.

Trigger Apex Class:
public with sharing class PublicGroupOnAccount {
    Public static void publicGroupMethod(List<Public_Group__c> pgList)
    {
      set<Id> acIdList = new set<Id>();
      for(Public_Group__c pg : pgList)
      {
        acIdList.add(pg.Account__c);
      }
      system.debug('>>>>>>>Account Ids<<<<<<<<<<>'+acIdList);
      map<Id,Account> acMap=new map<Id, Account>([select Id,Name,PublicGroup_Count__c from Account where Id IN :acIDList]);
      
      List<Public_Group__c> publicGroups = [SELECT Id , Name, Account__c from Public_Group__c where Account__c =: acIdList AND Name like 'p%'];
      system.debug('>>>>>>>>>>>>Public Groups<<<<<<<<<<<<<>'+publicGroups);
      List<Account> accList = new List<Account>();
     
      map<Id , List<Public_Group__c>> publicGroupMap = new Map<Id , List<Public_Group__c>>();
      for(Public_Group__c pgs : publicGroups)
      {
        if(publicGroupMap.get(pgs.Account__c) == null)
        {
          publicGroupMap.put(pgs.Account__c , new List<Public_Group__c>());
        }
        publicGroupMap.get(pgs.Account__c).add(pgs);
      }
      system.debug('>>>>>>>>Map<<<<<<<<<<<<>'+publicGroupMap);
      decimal countt = 0;
      for(Public_Group__c  pg : pgList)
      {
        List<Public_Group__c> listOfPublicGroup = publicGroupMap.get(pg.Account__c);
        countt = listOfPublicGroup.size(); 
        system.debug('>>>>>>>>>>>>Countt<<<<<<<<<<<>'+countt);
        
        if(countt==null)
        {
         countt=0;
        }
        else
        {
         Account ac=acMap.get(pg.Account__c);
        ac.PublicGroup_Count__c = countt;
       accList.add(ac);
      }
      }
      if(accList.size()>0)
      {
      system.debug('>>>>>>>>ACCList<<<<<<<<<<<<>'+accList);
      update accList;
      }
    }
}
Apex Trigger:
trigger TriggerOnPublicGroup on Public_Group__c (after insert, after update) {
    List<Public_Group__c> pgList = Trigger.new;
    if(pgList!=null)
    {
      PublicGroupOnAccount.publicGroupMethod(pgList);
    }
}
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.

Regards,
Akshay
VIGNESH BALASUBRAMANIANVIGNESH BALASUBRAMANIAN
Hi Hari,
Following code works for me.Try this,
trigger CountPG_AT on Account (before insert,before update) 
{
    set<id> acId=new set<id>();
    for(Account ac:trigger.new)
    {
        acId.add(ac.id);
    }
    set<id> pgId=new set<id>();
    List<Group> grpquy=[select id from group where type='Regular' and Name like 'd%'];
    for(Group g:grpquy)
    {
        pgId.add(g.id);
    }
    map<id,Integer> pgcount=new map<id,Integer>();
    AggregateResult[] agquy=[SELECT AccountId,count(UserOrGroupId)sum1 FROM AccountShare group by AccountId];
    
    for(AggregateResult ag:agquy)
    {
        pgcount.put((Id)ag.get('AccountId'),(Integer)ag.get('sum1'));
    }
    for(Account ac1:trigger.new)
    {
        if(pgcount.containsKey(ac1.Id))
        {
            ac1.PublicGroup_Count__c=pgcount.get(ac1.Id);
        }
    }
}
Kindly Edit and Save the Account record to check the PublicGroup_Count__c value.
Hope it will satisfy your requirement.

Thanks,
Vignesh.B
 
Hari Sfdc 7Hari Sfdc 7
Hi Vignesh, 

Thanks for your Reply.

I have tried below trigger count is not updating. Can you please help me where i made the mistake?
 
​Trigger:

trigger PartnerGroups on Account (before insert,before update) 
{
    set<id> acId=new set<id>();
    for(Account ac:trigger.new)
    {
        acId.add(ac.id);
    }
    set<id> pgId=new set<id>();
   // List<Group> grpquy=[select id from group where type='Regular' and Name like 'd%'];
   List<PartnerSharing__c> PRSquy =[SELECT Account_Name__c,Group_Name__c,Id,Name FROM PartnerSharing__c Where Group_Name__c like 'P_%' ];
    //for(Group g:grpquy)
    for(PartnerSharing__c p:PRSquy)    
    {
        pgId.add(p.id);
    }
    map<id,Integer> pgcount=new map<id,Integer>();
   // AggregateResult[] agquy=[SELECT AccountId,count(UserOrGroupId)sum1 FROM AccountShare group by AccountId];
    AggregateResult[] agquy=[SELECT AccountId,count(UserOrGroupId)sum1 FROM AccountShare group by AccountId LIMIT 1];
    
    for(AggregateResult ag:agquy)
    {
        pgcount.put((Id)ag.get('AccountId'),(Integer)ag.get('sum1'));
    }
    for(Account ac1:trigger.new)
    {
        if(pgcount.containsKey(ac1.Id))
        {
            ac1.PartnerGroup_Count__c = pgcount.get(ac1.Id);
        }
    }
}

Thanks


 
VIGNESH BALASUBRAMANIANVIGNESH BALASUBRAMANIAN
Hi Hari,

Follow the steps:
1.Use my code as it is.Just change 'P' instead of 'd' in query (List<Group> grpquy=[select id from group where type='Regular' and Name like 'd%'];)
2.Goto Account Record Click Manual sharing button and choose Public group which name starts with 'P' and add.
3.Come back to the Account Record.Edit and save the record and see the count Value.

Note: Account should be private in Sharing Settings then only you can see Manual sharing button.

Try this and let me know the result

Thanks,
Vignesh.B
Hari Sfdc 7Hari Sfdc 7
Hi Vignesh,

I have follwed your steps.

2.Goto Account Record Click Manual sharing button and choose Public group which name starts with 'P' and add. Means hear we need to add any groups?

Can we have a call if you are free?If ok please send me your number to my gmail -> "HariSfdc299@Gmail.com"

Thanks
rajat Maheshwari 6rajat Maheshwari 6

Hi Hari,

As per your requirement, I would suggest you to use Schedule Apex Class to update count of public groups on Account, because we can not create the trigger on Account Share (Standard Object) and Trigger on Account is meaningless.

If you want to utilize the trigger on Account, then let me know, I will develop the code for you :)

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

Hari Sfdc 7Hari Sfdc 7
Hi raja,

Whatever can you please help me that would be great help for us.

Thanks,
Hari
rajat Maheshwari 6rajat Maheshwari 6

Hari,

Yeah I can help you :)

please connect with me on gmail Id

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com