You need to sign in to do that
Don't have an account?

Insert Account List Item error
Hi Experts,
I created a class to insert Account List Item Record for every Affiliation child record.
Here is the code, and it's working in a single record, but when I executed the code manual in developer console I got an error message:

Here is my code below: I think I need to use map but I'm just new and still confused on using map.
Thanks for the help in advance
I created a class to insert Account List Item Record for every Affiliation child record.
Here is the code, and it's working in a single record, but when I executed the code manual in developer console I got an error message:
Here is my code below: I think I need to use map but I'm just new and still confused on using map.
public with sharing class Eisai_InsertAccListItem_cls { List<Affiliation_vod__c> affChildRecsList; List<Account_List_vod__c> acctListInDb; List<Account_List_Item_vod__c> newAccListItem = new List<Account_List_Item_vod__c>(); Set<Id>usersIdSet = new Set<Id>(); public Eisai_InsertAccListItem_cls(){ affChildRecsList = new List<Affiliation_vod__c>([SELECT Id, From_Account_vod__c, To_Account_Value__c, OwnerId FROM Affiliation_vod__c WHERE Parent_vod__c = False AND OwnerId IN :ActiveUsers()]); System.debug('Child Affiliation Records count ' + affChildRecsList.size()); acctListInDb = new List<Account_List_vod__c>([Select Id, Name, OwnerId FROM Account_List_vod__c WHERE OwnerId IN :ActiveUsers()]); System.debug('Account List Records count ' + acctListInDb.size()); for(Affiliation_vod__c affChild : affChildRecsList){ for(Account_List_vod__c acctList : acctListInDb){ if((acctList.Name.substring(3) == affChild.To_Account_Value__c) && (affChild.OwnerId == acctList.OwnerId)){ Account_List_Item_vod__c AccListItem = new Account_List_Item_vod__c(); AccListItem.Account_List_vod__c = acctList.Id; AccListItem.Account_vod__c = affChild.From_Account_vod__c; newAccListItem.add(AccListItem); } } } try{ insert newAccListItem ; System.debug('New Account List Item Records: ' + newAccListItem); }catch(DMLException e){ System.debug('exeption catch ' + e.getMessage()); } }//End of Constructor public Set<Id> ActiveUsers(){ List<User> usersIdList; usersIdList = new List<User>([SELECT Id FROM User WHERE (Profile_Name_vod__c LIKE '%Eisai_Epilepsy%' OR Profile_Name_vod__c LIKE '%Eisai_PrimaryCare%') AND IsActive = TRUE]); for(User users : usersIdList){ usersIdSet.add(users.Id); } return usersIdSet; } } //End of Class
Thanks for the help in advance
In your nested For loop you need to bulkify your code.
Or execution time is high because no. of records which are coming in execution context are more hence total time is exceeding limit.
Please optimize and bulkify your code.\
Thanks,
Can you give me a sample or to convert using MAP if possible?
Thanks, I'm just very new in apex coding actually this is the first class which I created.
Marion
In your original code , call to get user ID list is redundant. Which make 2 SOQL calls to user object.
Active user list population is one time activity and can be done in main object , this will save us from 2 SOQL calls.
See below code, test and let me know.
Thanks,