2

These are the objects

Team Seller Servicer Assignment is a junction object between Team and Account. Team Membership is a junction object between Team and User.

I need to write two triggers, one whenever a Team Seller Servicer Assignment is done between a Team and an Account, then the already existing Team Members should be populated to Salesforce's Native Account Team.

Two, when a Team Member under Team is added after the Team Seller Servicer Assignment is made, then those Team Members should be added to Account Team. Solution should also include data loader scenarios, assuming there is already data in Team, Team membership and Account, whenever Team Seller Servicer Assignments are made between Teams and Accounts using Dataloader, then all related Team Members should be populated in respective Account Teams.

I developed this for first trigger, but facing issues with bulk load using Dataloader, where I am loading 26,000 Team S/S Assignment records. Team records that already exist are 50, and 57,000 Accounts. Team members can be anywhere from 1 to 8 per Team.

Please find my code below. The error I get using dataloader is:

System.LimitException: Too many DML rows: 10001

  public static void createAccountTeam(Map<Id,ECRM_Team_SS_Account__c> 
    newAssignmentMap){
    Set<Id> ssIds = newAssignmentMap.keySet();

    //get team Ids associated with the newly inserted Team S/S assignments object
    // which are associated with an account

    List<ECRM_Team_SS_Account__c> teamSSList = [Select ECRM_Team_Name__c from ECRM_Team_SS_Account__c where Id =: ssIds and ECRM_SS_Account__c != null];

    Map<Id,ECRM_Team__c> teamMap;
    Set<Id> ecrmSet = new Set<Id>();
    if(teamSSList.size() > 0){
     for(ECRM_Team_SS_Account__c e: teamSSList){
          ecrmSet.add(e.ECRM_Team_Name__c);
     }
       teamMap = (new Map<Id,ECRM_Team__c>([select id from ECRM_Team__c where Id =: ecrmSet]));
    }
    //get membership list for Member Name(user lookup), Member Team, Team Member Role
    //List<ECRM_Team_Membership__c> teamMemberships = [select id, ECRM_Member_Name__r.Id, ECRM_Member_Team__c, ECRM_Team_Member_Role__c from ECRM_Team_Membership__c 
        //                                             where ECRM_Member_Team__r.Id =: teamMap.keySet()];

    Map<Id, ECRM_Team_Membership__c> teamMembershipMap = new Map<Id, ECRM_Team_Membership__c>([select Id, ECRM_Member_Name__r.Id, ECRM_Member_Team__c, ECRM_Team_Member_Role__c from ECRM_Team_Membership__c 
                                                                                                                        where ECRM_Member_Team__r.Id =: teamMap.keySet()]);

    //List<Account> accts = [select id from Account where Id IN (Select ECRM_SS_Account__c from ECRM_Team_SS_Account__c where Id =: ssIds and ECRM_Team_Name__c != null )];

    //Create Account Teams
     List<AccountTeamMember> teamMemberList = new List<AccountTeamMember>();

      Map<string,ECRM_Team_Role_Mapping__c> lst = ECRM_Team_Role_Mapping__c.getall();

      Map<String,String> customSettingMap = new Map<String,String>();

      for(ECRM_Team_Role_Mapping__c tr : lst.values()){
         customSettingMap.put(tr.Fannie_Mae_Team_Role__c, tr.Salesforce_Team_Role__c);
      }

    for(Account a: [select id from Account where Id IN (Select ECRM_SS_Account__c from ECRM_Team_SS_Account__c where Id =: ssIds and ECRM_Team_Name__c != null )]){
        for(ECRM_Team_Membership__c t : teamMembershipMap.values()){
            AccountTeamMember member = new AccountTeamMember();
            member.AccountId = a.Id; 
            member.TeamMemberRole = customSettingMap.ContainsKey(t.ECRM_Team_Member_Role__c)? customSettingMap.get(t.ECRM_Team_Member_Role__c).Value : '';
            member.AccountAccessLevel =  'Read';
            member.OpportunityAccessLevel =  'Read';
            member.CaseAccessLevel  =  'Read';
            member.UserId = t.ECRM_Member_Name__r.Id;
            teamMemberList.add(member);
        }
    }

    if(teamMemberList.size() > 0){
        database.insert(teamMemberList);
    }
}

}
  • 2
    This question is pretty confusing, you should ask about how to build your first trigger, then you can figure out the second trigger. But you never mentioned why this isn't working.. – EricSSH May 10 '17 at 15:42
  • Your first query is potentially non-selective. I would recommend you drop the AND ECRM_SS_Account__c != null from the query. You could also be running into account contention issues depending on the load order. Tell us what issue it is that you're encountering. You also probably want to set things for Deferred Sharing Calculation during data loads. – crmprogdev May 10 '17 at 16:10
2

The error is pretty self-explanatory, you are trying to insert too many records at once. Probably the easiest remedy is to disable the trigger while you are uploading the records with data loader, which can be done with something like the following around your trigger:

if(UserInfo.getUserName() != 'Data Loader Service User')
{
    //Rest of trigger
}

Note that this is a very rough way to do it, you should generally try to use a trigger framework that allows you to selectively turn off triggers while performing operations. As Adrian Larson suggests, using a Custom Permission or Hierarchy Custom Setting will allow more flexibility without having to change the code later, since you can add and remove them from service accounts and admin users as needed.

Once you have your data loaded, you can use a batch class to iterate over all the records which you added with Data Loader and update them in small chunks (default is 200). In your case, this would simply mean calling "update" on each batch execution, since you just need to fire the trigger. Each individual run of a batch class is counted separately for governor limits, so you should not run into a DML row error, but if you do you can reduce the size of the batch until all of your records are processed.

Depending on your business needs, this may mean that you need to disable your user's access to the application while you are running this process so they do not try to use the records without the team member data.

  • 1
    Nice find. I had a hard time even picking the error out of the OP! I might recommend a Custom Permission or Hierarchy Custom Setting for this sort of toggle. – Adrian Larson May 10 '17 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.