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 

Insert class not working

Hi experts,

I created a class to insert Account List record. The condition is allow duplicate Account List name, but the owner is different from the existing record. 


Exisiting record:
Account List Name: HO_Test
Owner: Marion Legacion

To be inserted record:
1. Account List Name: HO_Test
   Owner: Marion Legacion
Result: This record cannot be inserted since there is a duplicate both Account List Name and Owner.

2. Account List Name: HO_Test
Owner: Noiram Legacion
Result: A record will be created since Marion Legacion is different from Noiram Legacion.

Note: I created a Unique field to be updated by Workflow to populate the Account List Name and Owner (Formula: Account List Name & OwnerId)

Here is my code which is not working.
public with sharing class InsertAccountListRec_TEST_cls {
    List<User> usersIdList; 
	Set<Id> usersIdSet = new Set<Id>(); 
	Set<Id> AffFromAcctSet = new Set<Id>(); 
	Set<String> uniqueFieldSet = new Set<String>();
    List<Affiliation_vod__c> allAffParentRecs = new List<Affiliation_vod__c>();
    List<Account_List_vod__c> newAccListRecList = new List <Account_List_vod__c>();
    List<Account_List_vod__c> xAcctListRecs = new List <Account_List_vod__c>();
    List<Account_List_vod__c> InsertedAccList = new List <Account_List_vod__c>();
    public InsertAccountListRec_TEST_cls(){
        allAffParentRecs = new List<Affiliation_vod__c>([SELECT Id, OwnerId, From_Account_vod__c, From_Account_Value__c, To_Account_vod__c
                                                         FROM Affiliation_vod__c
                                                         WHERE (From_Account_vod__r.Id = '0011200001GNrb0AAD' AND Parent_vod__c = True) 
														 AND OwnerId IN: getActiveUsers()]);
        System.debug('Parent Affiliation Record Count '+ allAffParentRecs.size());
        for(Account_List_vod__c xAcctListRecs  : [Select OwnerId, Unique_Owner_Name__c FROM Account_List_vod__c 
                                                  WHERE Name = 'HO_RADY\'S CHILDREN\'S UCSD'
                                                  AND OwnerId IN: getActiveUsers()])
        for(Affiliation_vod__c allParentAffRecs: allAffParentRecs){
                Account_List_vod__c AccListRec = new Account_List_vod__c();    
                AccListRec.Name = 'HO_' + allParentAffRecs.From_Account_Value__c; 
                AccListRec.Icon_Name_vod__c = '0';	
                AccListRec.OwnerId = allParentAffRecs.OwnerId;
        for(Account_List_vod__c accList : newAccListRecList){
            if(!uniqueFieldSet.contains(accList.Name + accList.OwnerId)){
            insert InsertedAccList ;
            System.debug('New Account List Records: ' + InsertedAccList);
        }catch(DMLException e){
            System.debug('exeption catch ' + e.getMessage()); 
    }//end of 1st block
    public Set<Id> getActiveUsers(){
        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){
        return usersIdSet;
}// End of Class

Unique_Owner_Name__c - is the unique field.

I queried all the Unique_Owner_Name__c field of all Account List record and I checked the unique field set to see if the (accList.Name + accList.OwnerId) is existing, if not then add the Account List record to a list and DML Insert

Please help

deepak balur 19deepak balur 19
Marion: Please refer to the link and refine your code.
I have used this before and can vouch for it.
Yoni LegacyYoni Legacy

Thanks for the link that you provided. I think mine is quite different on the scenario in the link that you gave.

deepak balur 19deepak balur 19
The scenarios are going to be different but the critical blue print is there to extend the logic to your check on Name and Owner. Good luck !!!