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
Aloke Singh 6Aloke Singh 6 

I am getting the below error :-System.QueryException: Inline query has too many rows for direct assignment, use FOR loop

I am new to salesforce and getting below mentioned exception in one of the query :-

public static void buildPartnerTeam (list<SObject> objects)
    {
        FINAL list<String> roleNames = new list<String>
        {
            'Account Partner',
            'Account Partner Parent',
            'Account Partner Manager To Self',
            'Account Partner User To Self',
            'Account Partner Manager',  
            'Account Master Agent',  //  jul 17
            'Account- Sub Agent Super User Access Customer (Authorized)',  //  jul 17
            'Account- Sub Agent Super User Access to Prospect or Customer (Unauthorized)', //  jul 17
            'Account Related Partner Contact' //  MO-66
        };

        list<Partner_Team_Member__c> ptmsToInsert = new list<Partner_Team_Member__c>{};
        map<Id,Account> partnerAccountsMap = new map<Id,Account>{};
        list<Id> partnerAccountIds = new list<Id>{};

        list<Partner_Team_Role__c> partnerTeamRoles = TransactionSupport.getPartnerTeamRoles (roleNames);

//        list<Partner_Team_Role__c> partnerTeamRoles = [SELECT Id, Unique_Name__c FROM Partner_Team_Role__c WHERE Unique_Name__c IN :roleNames];

        list<Id> accountIds = new list<Id>{};

        for (SObject o : objects)
        {
            if (o.get('Partner_Account__c') != null) partnerAccountIds.add ((Id) o.get('Partner_Account__c'));
            if (o.get('CSD_MA__c') != null) partnerAccountIds.add ((Id) o.get('CSD_MA__c'));
            if (o.get('Partner_Of_Record_Cloud__c') != null) partnerAccountIds.add ((Id) o.get('Partner_Of_Record_Cloud__c'));
            accountids.add (o.Id);
        }

        // MO-66: get AccountContactRelations
        map<Id,Account> accountsWithACRsMap = new map<Id,Account>([SELECT Id,(SELECT Id, ContactId, AccountId FROM AccountContactRelations)
                                FROM Account WHERE Id IN :accountIds]);  //This is line where I am getting this error 

       // system.debug('&&&'+accountsWithACRsMap);
        map<Id,Id> contactToUserIdsMap = new map<Id,Id>{};
        set<Id> contactIds = new set<Id>{};
           // list<Account> acc = [SELECT Id,(SELECT Id, ContactId, AccountId FROM AccountContactRelations)
                               // FROM Account WHERE Id IN :accountIds];
           //  accId = accountsWithACRsMap.values();
           
        /*   for(Account acct : acc) {
                                    system.debug('&&&'+acct);
               
                                    integer count=0;
                                    for (AccountContactRelation acr : acct.AccountContactRelations)
            {
                count++;
                if (acr.AccountId != acct.Id) contactIds.add (acr.ContactId);
            }
                                    
                                }  */
            
        for (Account a : accountsWithACRsMap.values())
        {
            for (AccountContactRelation acr : a.AccountContactRelations)
            {
                if (acr.AccountId != a.Id) contactIds.add (acr.ContactId);
            }
        } 
        
      /*  for (Account a : acc)
        {
            
            for (AccountContactRelation acr : a.AccountContactRelations)
            {
                if (acr.AccountId != a.Id) contactIds.add (acr.ContactId);
            }
        } */

        //  MO-66: build map for Contact Id to User Id, for all AccountContactRelation records 

        for (User u : [SELECT Id, ContactId FROM User WHERE ContactId IN :contactIds])
        {
            contactToUserIdsMap.put (u.ContactId, u.Id);
        }

        //  now add partner team members

        for (Account a : [SELECT Id, Type, Partner_Account__c, ParentId, Partner_Type__c,
                            Sub_Agent_Authorized_to_access_cases__c 
                            FROM Account 
                            WHERE Id IN :partnerAccountIds])
        {
            partnerAccountsMap.put (a.Id, a);
        }
        
        for (SObject o : objects)
        {
            for (Partner_Team_Role__c ptr : partnerTeamRoles)
            {
                if (ptr.Unique_Name__c == roleNames[0] ||    //  'Account Partner'
                        ptr.Unique_Name__c == roleNames[4])  //  'Account Partner Manager'
                {
                    Id lookupId = (Id) o.get ('Partner_Account__c');
          
                    if (lookupId != null)
                    {
                        Partner_Team_Member__c ptm = new Partner_Team_Member__c (Partner_Team_Role__c = ptr.Id,
                                        Partner_Account__c = lookupId,
                                        Reason__c = 'Automatic');
                        ptm.put ('Account__c', o.Id);
                        ptmsToInsert.add (ptm);
                    }

                    lookupId = (Id) o.get ('Partner_Of_Record_Cloud__c');
          
                    if (lookupId != null)
                    {
                        Account partnerAccount = partnerAccountsMap.get (lookupId);

                        if (partnerAccount.Partner_Type__c != 'Sub-Agent')  //  jul 17
                        {
                            Partner_Team_Member__c ptm = new Partner_Team_Member__c (Partner_Team_Role__c = ptr.Id,
                                            Partner_Account__c = lookupId,
                                            Reason__c = 'Automatic');
                            ptm.put ('Account__c', o.Id);
                            ptmsToInsert.add (ptm);
                        }
                    }
                }

                if (ptr.Unique_Name__c == roleNames[1])  // 'Account Partner Parent'
                {
                    Account a = partnerAccountsMap.get ((Id) o.get ('Partner_Account__c'));
          
                    if (a != null)
                    {
                        if (a.Partner_Account__c != null)
                        {
                            Partner_Team_Member__c ptm = new Partner_Team_Member__c (Partner_Team_Role__c = ptr.Id,
                                          Partner_Account__c = a.Partner_Account__c,
                                          Reason__c = 'Automatic');
                            ptm.put ('Account__c', o.Id);
                            ptmsToInsert.add (ptm);
                        }

                        if (a.ParentId != null)
                        {   
                            Partner_Team_Member__c ptm = new Partner_Team_Member__c (Partner_Team_Role__c = ptr.Id,
                                          Partner_Account__c = a.ParentId,
                                          Reason__c = 'Automatic');
                            ptm.put ('Account__c', o.Id);
                            ptmsToInsert.add (ptm);
                        }
                    }

                    Id lookupId = (Id) o.get ('ParentId');
          
                    if (lookupId != null)
                    {
                        Partner_Team_Member__c ptm = new Partner_Team_Member__c (Partner_Team_Role__c = ptr.Id,
                                        Partner_Account__c = lookupId,
                                        Reason__c = 'Automatic');
                        ptm.put ('Account__c', o.Id);
                        ptmsToInsert.add (ptm);
                    }
                }

                // 'Account Partner Manager To Self' or 'Account Partner User To Self'

                if ((String) o.get('Type') == 'Partner' && (ptr.Unique_Name__c == roleNames[2] || ptr.Unique_Name__c == roleNames[3]))  
                {
                    Partner_Team_Member__c ptm = new Partner_Team_Member__c (Partner_Team_Role__c = ptr.Id,
                                        Partner_Account__c = o.Id,
                                        Reason__c = 'Automatic');
                    ptm.put ('Account__c', o.Id);
                    ptmsToInsert.add (ptm);            
                }

                if (ptr.Unique_Name__c == roleNames[5])   //  'Account Master Agent'  //  Jul 17
                {
                    Id lookupId = (Id) o.get ('CSD_MA__c');
          
                    if (lookupId != null)
                    {
                        Partner_Team_Member__c ptm = new Partner_Team_Member__c (Partner_Team_Role__c = ptr.Id,
                                        Partner_Account__c = lookupId,
                                        Reason__c = 'Automatic');
                        ptm.put ('Account__c', o.Id);
                        ptmsToInsert.add (ptm);
                    }
                }

                if (ptr.Unique_Name__c == roleNames[6])   //  'Account- Sub Agent Super User Access Customer (Authorized)'  // Jul 17
                {
                    Id lookupId = (Id) o.get ('Partner_Of_Record_Cloud__c');
          
                    if (lookupId != null)
                    {
                        Account partnerAccount = partnerAccountsMap.get (lookupId);

                        if (partnerAccount.Partner_Type__c == 'Sub-Agent' && 
                                partnerAccount.Sub_Agent_Authorized_to_access_cases__c == true)  //  jul 17
                        {
                            Partner_Team_Member__c ptm = new Partner_Team_Member__c (Partner_Team_Role__c = ptr.Id,
                                            Partner_Account__c = lookupId,
                                            Reason__c = 'Automatic');
                            ptm.put ('Account__c', o.Id);
                            ptmsToInsert.add (ptm);
                        }
                    }

                }

                if (ptr.Unique_Name__c == roleNames[7])   //  'Account- Sub Agent Super User Access to Prospect or Customer (Unauthorized)' //  jul 17
                {
                    Id lookupId = (Id) o.get ('Partner_Of_Record_Cloud__c');
          
                    if (lookupId != null)
                    {
                        Account partnerAccount = partnerAccountsMap.get (lookupId);

                        if (partnerAccount.Partner_Type__c == 'Sub-Agent' && 
                                partnerAccount.Sub_Agent_Authorized_to_access_cases__c == false)  //  jul 17
                        {
                            Partner_Team_Member__c ptm = new Partner_Team_Member__c (Partner_Team_Role__c = ptr.Id,
                                            Partner_Account__c = lookupId,
                                            Reason__c = 'Automatic');
                            ptm.put ('Account__c', o.Id);
                            ptmsToInsert.add (ptm);
                        }
                    }
                }

                //  MO-66: Add PTMs for all Related Contacts

                if (ptr.Unique_Name__c == roleNames[8])   //  'Account Related Partner Contact'
                                {
                     Account accountWithACRs = accountsWithACRsMap.get (o.Id);
                   // Account accountWithACRs = acc.Id;

                    for (AccountContactRelation acr : accountWithACRs.AccountContactRelations)
                    {
                        Id userId = contactToUserIdsMap.get (acr.ContactId);

                        if (userId != null)
                        {
                            Partner_Team_Member__c ptm = new Partner_Team_Member__c (Partner_Team_Role__c = ptr.Id,
                                            Partner_User__c = userId,
                                            Reason__c = 'Automatic');
                            ptm.put ('Account__c', o.Id);
                            ptmsToInsert.add (ptm);
                        }
                    }
                }
            }
        }
        insert ptmsToInsert;
    }


Any help would be appreciated 
 
Best Answer chosen by Aloke Singh 6
Rameshwar gaurRameshwar gaur
  This Happen because you calling list of Account record and saving in single Account.

map<Id,Account> accountsWithACRsMap = new map<Id,Account>([SELECT Id,(SELECT Id, ContactId, AccountId FROM AccountContactRelations)
                                FROM Account WHERE Id IN :accountIds])


this is calling a list of record ------     [SELECT Id,(SELECT Id, ContactId, AccountId FROM AccountContactRelations)
                                FROM Account WHERE Id IN :accountIds]

Use these code 

list<Id> accountIds = new list<Id>{};
        list<Account> acclst = [SELECT Id,(SELECT Id, ContactId, AccountId FROM AccountContactRelations)
                                FROM Account WHERE Id IN :accountIds];  
        map<Id,Account> accountsWithACRsMap = new map<Id,Account>();
        integer i =0;
        while(acclst.size()!=null)
        {
            accountsWithACRsMap.put(acclst.get(i).id,acclst.get(i));
            i++;
        }