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
shankar sudalaimanishankar sudalaimani 

Test_Class For_@future Method

I need to write the test class for below @future method and i already tried it's covered 61% and tried a lot but i can't make it please help me with this am a newbie
Class:
global class UpdateFAPShare{

@future
public static void UdpateFAPSharesfromAccountshares()
{
Map<Id,List<AccountShare>> AccSharesUdpatedMap = new Map<ID,List<AccountShare>>();
Map<Id,List<Foreign_Account_Approval__share>> FAPSharesMap = new Map<ID,List<Foreign_Account_Approval__share>>();

Map<Id,List<Id>> FAPUserSharestoAdd = new Map<ID,List<Id>>();
Map<Id,List<Id>> FAPActiveUserSharestoAdd = new Map<ID,List<Id>>();

Set<Id> AccRelated = new Set<Id>();
Set<Id> FAPSRelated = new Set<Id>();

List<Foreign_Account_Approval__c> FAPs =[Select Id,Customer__c from Foreign_Account_Approval__c];

List<Foreign_Account_Approval__share> FAPShares4add = new List<Foreign_Account_Approval__share>();
List<Foreign_Account_Approval__share> FAPShares4Del = new List<Foreign_Account_Approval__share>();
for (Foreign_Account_Approval__c FAP : FAPs)
{
   AccRelated.add(FAP.Customer__c);
 
}
list<Foreign_Account_Approval__share> FAPShares =[Select Id, UserOrGroupId,AccessLevel,RowCause,ParentId from Foreign_Account_Approval__share where RowCause=: Schema.Foreign_Account_Approval__Share.RowCause.Derived_From_Customer__c];

for (AccountShare[] AccShare : [Select Id, UserOrGroupId, AccountAccessLevel, RowCause, AccountId From AccountShare where AccountId =:AccRelated and RowCause <> 'ImplicitParent']){
  System.debug('Account Shares:'+accshare);
  for(AccountShare acs :AccShare){

     If(AccSharesUdpatedMap.containskey(Acs.accountid))
      {
        AccSharesUdpatedMap.get(Acs.accountid).add(acs);
      }
     else
        AccSharesUdpatedMap.put(acs.accountid,new AccountShare[]{acs});
  }
}
System.debug('AccSharesYpdateMap'+  AccSharesUdpatedMap);
for(Foreign_Account_Approval__share fs : FAPShares)
{
   FAPSRelated.add(fs.Id);
   If(FAPSharesMap.containskey(fs.parentId))
      {
        FAPSharesMap.get(fs.parentId).add(fs);
        
      }
     else
        FAPSharesMap.put(fs.parentId,new Foreign_Account_Approval__share[]{fs});
 } 
for(Foreign_Account_Approval__c FAP : FAPs)
{
 for(AccountShare acs : AccSharesUdpatedMap.get(FAP.customer__c))
  {
      boolean exists =false;  
    if(FAPSharesMap.containskey(FAP.id))  
    {
      for(Foreign_Account_Approval__share fs : FAPSharesMap.get(FAP.id))
     {
      if(fs.UserOrGroupId == acs.UserOrGroupId)
             
             {
                exists = true;
                FAPSRelated.remove(fs.Id);  
             }
     }
    }
  if(!exists) 
      {    String UsrOrGroupId  = acs.UserOrGroupId;
           if(!UsrOrGroupId.startswith('005'))
           {
           Foreign_Account_Approval__Share fs = new Foreign_Account_Approval__Share();
           fs.ParentId = FAP.Id;
           fs.UserOrGroupId = acs.UserOrGroupId;
           fs.Rowcause = Schema.Foreign_Account_Approval__Share.RowCause.Derived_From_Customer__c;
           fs.AccessLevel ='Read';
           FAPShares4add.add(fS);
           }
            else
           {
            If(FAPUserSharestoAdd.containskey(acs.UserOrGroupId))
            {
              FAPUserSharestoAdd.get(acs.UserOrGroupId).add(FAP.Id);
        
            }
            else
              FAPUserSharestoAdd.put(acs.UserOrGroupId,new Id []{FAP.Id});         
           }

           
      }  

}
} 
if(FAPUSerSharestoAdd.keyset().size()>0)
{
for(User u :[Select Id,isActive from User where id =:FAPUSerSharestoAdd.keyset()])
{
if(u.isActive)
{
 for(Id FAPID : FAPUSerSharestoAdd.get(u.ID))
 {
           Foreign_Account_Approval__Share fs = new Foreign_Account_Approval__Share();
           fs.ParentId = FAPId;
           fs.UserOrGroupId = u.Id;
           fs.Rowcause = Schema.Foreign_Account_Approval__Share.RowCause.Derived_From_Customer__c;
           fs.AccessLevel ='Read';
           FAPShares4add.add(fS);

 }
}
}
}

If(FAPShares4add.size()>0)
insert FAPShares4add;
if(FAPSRelated.size()>0)
{
FAPShares4Del =[Select Id from Foreign_Account_Approval__share where id =: FAPSRelated ];
delete FAPShares4Del;
}
}
}

My Test CLass:
@isTest(SeeAllData=true)
public class Test_UpdateFAPShare {
    static testMethod void TestMethodUpdateFAPShare(){
       // Select users for the test.
      List<User> users = [SELECT Id FROM User WHERE IsActive = true LIMIT 2];
      Id User1Id = users[0].Id;
      Id User2Id = users[1].Id;
      Profile p = [SELECT Id,Name FROM Profile WHERE Name = 'DB_Standard User_sales'];
      User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
        System.runAs (thisUser) 
        {  
      Account acc = new Account(Name='TestAcc');
      insert Acc;
        //Create Parent Group
        Group grp = new Group();
        grp.name = Acc.Name;
        grp.Type = 'Regular';
        grp.DoesIncludeBosses=false;
        Insert grp; 
 
        //Create Group Member
        GroupMember grpMem1 = new GroupMember();
        grpMem1.UserOrGroupId = UserInfo.getUserId();
        grpMem1.GroupId = grp.Id;
        Insert grpMem1;
 
       
      
   //   Account acc2 = new Account(Name='TestAcc2',OwnerId=grpMem1.UserOrGroupId);
     // insert Acc2;
     Case c = new Case(OwnerId = grp.Id,Reason='Record Deletion',Status='Done',subject='Test');
      insert c;
      Foreign_Account_Approval__c FAP = new Foreign_Account_Approval__c();
      FAP.Customer__c=Acc.id;
     // FAP.Name=thisuser.Name;
      FAP.OwnerId = thisUser.Id;
      FAP.CA_Name__c=thisUser.Id;
      FAP.Status__c='Pending BSO Approval';
      insert FAP;
      FAP = [select Id, Name,Customer__c from Foreign_Account_Approval__c where id = : FAP.id];
            
        //Create Sub group
        Group subGrp  = new Group();
        subGrp.name = FAP.Name;
        subGrp.Type = 'Regular'; 
        Insert subGrp; 
 
        //Assign Role To Parent Group
        GroupMember grpMem2 = new GroupMember();
        grpMem2.UserOrGroupId = subGrp.Id;
        grpMem2.GroupId = grp.Id;
        Insert grpMem2;   
         // Create new sharing Account for the Account object Account.
      AccountShare accShr  = new AccountShare();
   
      // Set the ID of record being shared.
      accShr.AccountId = FAP.Customer__c;
        
      // Set the ID of user or group being granted access.
      accShr.UserOrGroupId = grpMem1.UserOrGroupId;
        
      // Set the access level.
      accShr.AccountAccessLevel = 'Read';
        
      // Set rowCause to 'manual' for manual sharing.
      // This line can be omitted as 'manual' is the default value for sharing objects.
      accShr.RowCause = Schema.AccountShare.RowCause.Manual;
      
        // Create new sharing object for the custom object Job.
      Foreign_Account_Approval__share fapShr  = new Foreign_Account_Approval__share();
   
      // Set the ID of record being shared.
      fapShr.ParentId = FAP.Id;
        
      // Set the ID of user or group being granted access.
      fapShr.UserOrGroupId = grpMem2.UserOrGroupId;
        
      // Set the access level.
      fapShr.AccessLevel = 'Read';
       
      // Set rowCause to 'manual' for manual sharing.
      // This line can be omitted as 'manual' is the default value for sharing objects.
      fapShr.RowCause = Schema.Foreign_Account_Approval__share.RowCause.Manual;
      UpdateFAPShare.UdpateFAPSharesfromAccountshares();
        }
        UpdateFAPShare.UdpateFAPSharesfromAccountshares();
     }
}

 
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi shankar, 

Call the @future method within , Test.starttest(); and Test.stopTest();

Thanks,
Prosenjit
shankar sudalaimanishankar sudalaimani
Hi Prosenjit,
No,It's not working.Please,tell me how to create a two groups one group assigned to account share and another group assigned to custom object,i need and how to create a different userorgroupid for account share and custom__C share.
In my code account is created and if it is a foriegn account thay need a approval for that account so we created a foriegn account by using Account id .
I tried every thing but i don't know what is the problem.
Thanks,
Shankar S