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
Yoni LegacyYoni Legacy 

How to make a class runs for specific user or profile?

Hi,

I am a beginner in apex coding and honestly don't know if my code will work as expected.

I tried my best to accomplish atleast the insert method for Account List. Below is the code.

public with sharing class createAndMaintainAccountList_cls{
    Set<Id> allParentRecsSet = new Set<Id>();
    Set<Account_List_vod__c> setAccRec = new Set<Account_List_vod__c>();
    Set<Account_List_Item_vod__c> accListItemSet = new Set<Account_List_Item_vod__c>();
    List<Affiliation_vod__c> allAffParentRecs = new List<Affiliation_vod__c>();
    List<Affiliation_vod__c> affParentRecsLst = new List<Affiliation_vod__c>();
    List<Account_List_vod__c> newAccListRecList = new List <Account_List_vod__c>();
    List<Affiliation_vod__c> affChildRecsList = new List<Affiliation_vod__c>();
    List<Account_List_Item_vod__c> accListItemList = new List<Account_List_Item_vod__c>();
    Map<Id,Id> mapAffRecs = new Map<Id, Id>();
    Map<Id, String> acctListMap = new Map<Id,String>();
    Map<Id,Id> affChildRecsMap = new Map<Id,Id>();
    
    //Creation a list of all Parent Affiliation Records, then add to parentAffRecsLst List.
    //Start of 1st Block
    public Set<Id> getParentAffRecs(){
        allAffParentRecs = new List<Affiliation_vod__c>([SELECT Id, From_Account_vod__c, To_Account_vod__c,Business_Account__c 
                                                         FROM Affiliation_vod__c
                                                         WHERE Business_Account__c = 'Group_Practice_Account' AND Parent_vod__c = True]);
        
        //Iterate on the Affiliation records (Professional) 
        //If the From_Account_vod__c field has a value (meaning: Not blank), add the value to allParentRecsSet (Set)
        allParentRecsSet = new Set<Id>();
        for(Affiliation_vod__c aParentRecs: allAffParentRecs){
            if(aParentRecs.From_Account_vod__c != Null){
                allParentRecsSet.add(aParentRecs.From_Account_vod__c);
            }
        }
        return allParentRecsSet;
    }//end of 1st block
    
    //Start of 2nd block
    //List of Affliation records (Group Account) which Business_Account__c is equal to Group Practice 
    //and From_Account_vod__c record is in allParentRecsSet
    public Map<Id,Id> putAffParentIdAndFromAcct(){
        affParentRecsLst = new List<Affiliation_vod__c>([SELECT Id, From_Account_vod__c, To_Account_vod__c,Business_Account__c 
                                                         FROM Affiliation_vod__c 
                                                         WHERE Business_Account__c = 'Group_Practice_Account' 
                                                         AND From_Account_vod__c in:allParentRecsSet]);
        
        //Creation of map (Key: Affiliation Id, Values: From_Account_vod__c)
        mapAffRecs = new Map<Id, Id>();
        for(Affiliation_vod__c affParentRecs : affParentRecsLst){
            if(!allParentRecsSet.contains(affParentRecs.From_Account_vod__c)){
                mapAffRecs.put(affParentRecs.Id, affParentRecs.From_Account_vod__c);
            }
        }
        return mapAffRecs;
    }//end of 2nd block
    
    
    //Start of 3rd block
    //For every Parent Affiliation record, check if the setAccRec (Set) doesn't contains Account list,
    // if true, create an Account List.
    public Map<Id, String> createAcctListRecs(){
        acctListMap = new Map<Id,String>();
        setAccRec = new Set<Account_List_vod__c>();
        
        for(Affiliation_vod__c allParentAffRecs : affParentRecsLst){
            Account_List_vod__c newAccListRec = new Account_List_vod__c();
            newAccListRec.Name = 'HO_' + mapAffRecs.get(allParentAffRecs.Id);
            newAccListRec.Icon_Name_vod__c = '0';
            
            if(!setAccRec.contains(newAccListRec)){
                setAccRec.add(newAccListRec);
                acctListMap.put(newAccListRec.Id, newAccListRec.Name);
            }else{
                System.debug('-----The Account List already exist-----');
            } 
            
            for(Account_List_vod__c acctListRecs : setAccRec){
                newAccListRecList.add(acctListRecs);
            }
            insert newAccListRecList;     
        }
        return acctListMap;
    }//end of 3rd block
    
    //Start of 4th block
    //List of Affliation records (Professional) which To Account is in allParentRecsSet
    public Map<Id,Id> getChildAffRecs(){
        affChildRecsMap = new Map<Id,Id>();
        
        affChildRecsList = new List<Affiliation_vod__c>([SELECT Id, From_Account_vod__c, To_Account_vod__c,Business_Account__c 
                                                         FROM Affiliation_vod__c 
                                                         WHERE Business_Account__c = 'Professional_vod' 
                                                         AND Parent_vod__c = False
                                                         AND To_Account_vod__c IN :allParentRecsSet]);
        
        
        //In every Affiliation Professional, add Child Affiliation Record Id, From Account vod to affChildRecsMap
        for(Affiliation_vod__c affChildRecs : affChildRecsList){
            affChildRecsMap.put(affChildRecs.Id, affChildRecs.From_Account_vod__c);
        }
        return affChildRecsMap;
    }//end of 4th block 
    
    //Start of 5th Block
    public void createAcctListItemRecs(){
        accListItemSet = new Set<Account_List_Item_vod__c>();
        
            for(Affiliation_vod__c affChildRecsFinal : affChildRecsList){
                Account_List_Item_vod__c newAccListItem = new Account_List_Item_vod__c();
                newAccListItem.Account_List_vod__c = acctListMap.get(affChildRecsFinal.Id);
                newAccListItem.Account_vod__c = affChildRecsMap.get(affChildRecsFinal.Id);
                
                if(!accListItemSet.contains(newAccListItem)){
                    accListItemSet.add(newAccListItem);
                }else{
                    System.debug('-----The Account List Item already exist-----');
                }
            }
            
            for(Account_List_Item_vod__c accListItemRecs: accListItemSet){
                accListItemList.add(accListItemRecs);
            }
            insert accListItemList; 
    }//end of 5th block
}

Note: I guess my question is how to make the above code to execute only for specific profile.

Thanks
Marion