+ Start a Discussion
SHKSHK 

What is the Error in the following Log and How to Fix it?

http://pasted.co/53dbc34a
Please view the log and tell me how to fix it and why it occured !!
Abhishek BansalAbhishek Bansal
Hi,

Since you are using multiple add statemnets on the accToInsert list in your code so there are some instances where the same account is being inserted in the list twice. I would recommend you to change the data type of the accToInsert variable to Set<Account>. When you try to insert this set, first convert it into list and then use insert statement.
I have also updated your code as per my sugegstion. Please find the updated code below:
public class IS_PartnerHierarchyController {
    public Account countryAcc{get;set;}
    private String countryAccId;
    public accountWrapper globalAccount{get;set;}
    public accountWrapper cntryGrpAccount{get;set;}
    public accountWrapper cntryAccount{get;set;}
    public accountWrapper siteAccount{get;set;}
    public String globalAccName{get;set;}
    public String cntryGrpAccName{get;set;}
    public String address{get;set;}
    public static Id nonCountryRecordTypeId {get;set;}
    public static String PARTNER_HIERLEVEL_GLOBALPARENT {get { return IS_Constants.PARTNER_HIER_GLOBALPARENT;}set;}
    public static String PARTNER_HIERLEVEL_COUNTRYSET {get { return IS_Constants.PARTNER_HIER_COUNTRYSET;}set;}
    public static String PARTNER_HIERLEVEL_COUNTRY {get { return IS_Constants.PARTNER_HIER_COUNTRY;}set;}
    public static String PARTNER_HIERLEVEL_SITE {get { return IS_Constants.PARTNER_HIER_SITE;}set;}
    public static String CONST_PARTNER_HIER_LEVEL_LABEL {get { return IS_Constants.CONST_PARTNER_HIER_LEVEL_LABEL;}set;}
    public static String CONST_COUNTRY_UCN_LABEL {get { return IS_Constants.CONST_COUNTRY_UCN_LABEL;}set;}
    public static String CONST_GLOBAL_UCN_LABEL {get { return IS_Constants.CONST_GLOBAL_UCN_LABEL;}set;}
    public static String CONST_COUNTRYGRP_UCN_LABEL {get { return IS_Constants.CONST_COUNTRYGRP_UCN_LABEL;}set;}
    public static String CONST_PARTNER_ADDRESS_LABEL {get { return IS_Constants.CONST_PARTNER_ADDRESS_LABEL;}set;}
    public static String CONST_PARTNER_STATUS_LABEL {get { return IS_Constants.CONST_PARTNER_STATUS_LABEL;}set;}
    public static String CONST_PARTNER_SITE_NAME_LABEL {get { return IS_Constants.CONST_PARTNER_SITE_NAME_LABEL;}set;}
    public static String CONST_PARTNER_COUNTRY_LEVEL_NAME_LABEL {get { return IS_Constants.CONST_PARTNER_COUNTRY_LEVEL_NAME_LABEL;}set;}
    public static String CONST_PARTNER_COUNTRY_GROUPING_NAME_LABEL {get { return IS_Constants.CONST_PARTNER_COUNTRY_GROUPING_NAME_LABEL;}set;}
    public static String CONST_PARTNER_GLOBAL_NAME_LABEL {get { return IS_Constants.CONST_PARTNER_GLOBAL_NAME_LABEL;}set;}
    public Boolean isSitePartner;
    public Boolean moveContactsToCountry;
    //Added as part of US1958
    List<Case> casesToCreate = new List<Case>();
    //END of logic US 1958
    public List<Contact> contactList;
    static {
        nonCountryRecordTypeId = [SELECT Id, Name FROM RecordType WHERE Name =: IS_Constants.CONST_PARTNER_ACCOUNT_NONCOUNTRY_RECORDTYPE LIMIT 1].Id;
      }
       
    public IS_PartnerHierarchyController(ApexPages.StandardController controller) {
        countryAccId = (String)controller.getId();
        isSitePartner = false;
        moveContactsToCountry = false;
        countryAcc = [SELECT Id, Name, Partner_Hierarchy_Level__c, Account_UCN__c, BillingCountry, ParentId,HQ_Flag__c,
                      BillingStreet, BillingCity, BillingState, BillingPostalCode, Partnership_Status__c, Partner_Type__c, Legacy_Id__c,
                      Geo_Parent_UCN_MDM_ID__c, WW_Partner_Parent_MDM_ID__c
                     FROM Account 
                     WHERE Id =: countryAccId 
                     LIMIT 1];
        populateAccountWrapper(countryAcc);

    }

    // Method to populate Account Wrapper for Global/Country/Country Grouping and Site Accounts.
    public void populateAccountWrapper(Account a) {
        address = concatenateAddress(a);
        
        populateCountryAccount(a);
        
        // Populating Site Account Data
        if(globalAccount.status != IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_NEW && isSitePartner) {
            siteAccount = new accountWrapper(a);
            siteAccount.acc.RecordTypeId = nonCountryRecordTypeId;
            siteAccount.addr = address;
            siteAccount.acc.Partner_Hierarchy_Level__c = IS_Constants.PARTNER_HIER_SITE;
            siteAccount.accType = IS_Constants.PARTNER_HIER_SITE;
            siteAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_NEW;
            siteAccount.acc.Account_UCN__c = ''; // T-493722
            siteAccount.acc.Geo_Parent_UCN_MDM_ID__c = '';
            siteAccount.acc.WW_Partner_Parent_MDM_ID__c = '';
                        //Populate Geo value on Site Account(INSALES-1215)
            For(Country__c Con:[Select Id,Theatre__c from Country__c where Name =: a.BillingCountry LIMIT 1]){
                        siteAccount.acc.Geo__c = Con.Theatre__c;
                }
        } else {
            siteAccount = new accountWrapper();
            siteAccount.acc.Name = a.Name;
            //siteAccount.acc.Account_UCN__c = ''; T-493722
            //siteAccount.acc.Account_UCN__c = a.Account_UCN__c; T-493722
            siteAccount.acc.RecordTypeId = nonCountryRecordTypeId;
            siteAccount.acc.Partner_Type__c = a.Partner_Type__c;
            siteAccount.addr = address;
            siteAccount.acc.Partner_Hierarchy_Level__c = IS_Constants.PARTNER_HIER_SITE;
            siteAccount.accType = IS_Constants.PARTNER_HIER_SITE;
            siteAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_NEW;
                        //Populate Geo value on Site Account(INSALES-1215)
            For(Country__c Con:[Select Id,Theatre__c from Country__c where Name =: a.BillingCountry LIMIT 1]){
                        siteAccount.acc.Geo__c = Con.Theatre__c;
                }
            if(IS_Constants.RESELLER_PARTNER.equalsIgnoreCase(a.Partner_Type__c)) { //I-222997
                siteAccount.acc.HQ_Flag__c = true; // I-218030
            }            
            populateAddrFields(a, siteAccount);
        } /*else {
            siteAccount = new accountWrapper();
        }*/
        globalAccName = globalAccount.acc.Name;
        cntryGrpAccName = cntryGrpAccount.acc.Name;
    }

    public void populateCountryAccount(Account account) {
        List<Account> matchedGlobalParent = new List<Account>();
        List<Account> matchedCntryGrpAccount = new List<Account>();
        List<Account> matchedCountryAccount = new List<Account>();
        Map<Id,Account> cntryGrpAccountMap = new Map<Id,Account>();
        
        matchedGlobalParent = [SELECT Id, Name, Partner_Hierarchy_Level__c, Account_UCN__c, BillingCountry, ParentId, 
                            BillingStreet, BillingCity, BillingState, BillingPostalCode, Partner_Type__c,
                            Geo_Parent_UCN_MDM_ID__c, WW_Partner_Parent_MDM_ID__c
                            FROM Account WHERE Partner_Hierarchy_Level__c =: IS_Constants.PARTNER_HIER_GLOBALPARENT AND Name LIKE : account.Name+'%' LIMIT 1];
        
        if(matchedGlobalParent.size() > 0) {
            matchedCntryGrpAccount = [SELECT Id, Name, Partner_Hierarchy_Level__c, Account_UCN__c, BillingCountry, ParentId,
                            BillingStreet, BillingCity, BillingState, BillingPostalCode, Partner_Type__c, Geo_Parent_UCN_MDM_ID__c, 
                            WW_Partner_Parent_MDM_ID__c
                            FROM Account WHERE Partner_Hierarchy_Level__c =: IS_Constants.PARTNER_HIER_COUNTRYSET AND ParentId =: matchedGlobalParent.get(0).Id];
            for(Account a : matchedCntryGrpAccount) {
                cntryGrpAccountMap.put(a.Id, a);
            }
        }
        
        
        if(matchedCntryGrpAccount.size() > 0) {
            matchedCountryAccount = [SELECT Id, Name, Partner_Hierarchy_Level__c, Account_UCN__c, BillingCountry, ParentId,
                            BillingStreet, BillingCity, BillingState, BillingPostalCode, Partner_Type__c, Legacy_Id__c, HQ_Flag__c, Partnership_Status__c,
                            Geo_Parent_UCN_MDM_ID__c, WW_Partner_Parent_MDM_ID__c
                            FROM Account WHERE Partner_Hierarchy_Level__c =: IS_Constants.PARTNER_HIER_COUNTRY AND ParentId IN : cntryGrpAccountMap.keyset() 
                            AND BillingCountry =: account.BillingCountry LIMIT 1];
        }
        if(matchedCountryAccount.size() > 0) {
            cntryAccount = new accountWrapper(matchedCountryAccount.get(0));
            cntryAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_EXISTING;
            cntryAccount.addr = concatenateAddress(matchedCountryAccount.get(0));
            globalAccount = new accountWrapper(matchedGlobalParent.get(0));
            globalAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_EXISTING;
            cntryGrpAccount = new accountWrapper(cntryGrpAccountMap.get(matchedCountryAccount.get(0).ParentId));
            cntryGrpAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_EXISTING;
            isSitePartner = true;
        } else {
            cntryAccount = new accountWrapper(account);
            cntryAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_NEW; 
            cntryAccount.acc.Partner_Hierarchy_Level__c = IS_Constants.PARTNER_HIER_COUNTRY;
            cntryAccount.addr = address;
            populateGlobalAccount(account);
        }
        
        cntryAccount.accType = IS_Constants.PARTNER_HIER_COUNTRY;
        globalAccount.accType = IS_Constants.PARTNER_HIER_GLOBALPARENT;
        cntryGrpAccount.accType = IS_Constants.PARTNER_HIER_COUNTRYSET;
    
    }
    
    // Method to populate Global Account Wrapper
    public void populateGlobalAccount(Account account) {
        List<Account> matchedGlobalParent = new List<Account>();
        matchedGlobalParent = [SELECT Id, Name, Partner_Hierarchy_Level__c, Account_UCN__c, BillingCountry, ParentId,
                            BillingStreet, BillingCity, BillingState, BillingPostalCode, Partner_Type__c, Geo_Parent_UCN_MDM_ID__c, WW_Partner_Parent_MDM_ID__c
                            FROM Account WHERE Partner_Hierarchy_Level__c =: IS_Constants.PARTNER_HIER_GLOBALPARENT AND Name LIKE : account.Name+'%' LIMIT 1];
        if(matchedGlobalParent.size() > 0) {
            globalAccount = new accountWrapper(matchedGlobalParent.get(0));
            globalAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_EXISTING;
            populateCntryGroupAccount(matchedGlobalParent.get(0), account);
        } else {
            globalAccount = new accountWrapper();
            // Populating Global Account Data
            globalAccount.acc.Name = account.Name;
            globalAccount.acc.Partner_Hierarchy_Level__c = IS_Constants.PARTNER_HIER_GLOBALPARENT;
            globalAccount.acc.Account_UCN__c = '';
            globalAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_NEW;
            globalAccount.acc.Partner_Locator__c = false;
            globalAccount.acc.Account_UCN__c = account.Account_UCN__c;
            globalAccount.acc.WW_Partner_Parent_MDM_ID__c = account.WW_Partner_Parent_MDM_ID__c;
            globalAccount.acc.Geo_Parent_UCN_MDM_ID__c = account.Geo_Parent_UCN_MDM_ID__c;
            globalAccount.acc.RecordTypeId = nonCountryRecordTypeId;
            globalAccount.acc.Partner_Type__c = account.Partner_Type__c;
            
            createNewCntryGroupAccount(account);
        }
    }
    
    // Method to populate Country Group Account Wrapper
    public void populateCntryGroupAccount(Account globalAccount, Account cntryAccount) {
        List<Account> matchedCntryGrpAccount = new List<Account>();
        String globalAccId = String.valueOf(globalAccount.Id).substring(0, 15);
        matchedCntryGrpAccount = [SELECT Id, Name, Partner_Hierarchy_Level__c, Account_UCN__c, BillingCountry, ParentId,
                            BillingStreet, BillingCity, BillingState, BillingPostalCode, Partner_Type__c, Geo_Parent_UCN_MDM_ID__c, WW_Partner_Parent_MDM_ID__c
                            FROM Account WHERE Partner_Hierarchy_Level__c =: IS_Constants.PARTNER_HIER_COUNTRYSET AND Name LIKE : '%- '+ cntryAccount.BillingCountry AND ParentId =: globalAccount.Id LIMIT 1];
        //system.assert(false,globalAccId);                 
        if(matchedCntryGrpAccount.size() > 0) {
            cntryGrpAccount = new accountWrapper(matchedCntryGrpAccount.get(0));
            cntryGrpAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_EXISTING;
        } else {
            createNewCntryGroupAccount(cntryAccount);
        }
    }

    // Method to create New Country Group Account Wrapper
    public void createNewCntryGroupAccount(Account account) {
        cntryGrpAccount = new accountWrapper();
        cntryGrpAccount.acc.Name = account.Name + ' - ' + account.BillingCountry;
        cntryGrpAccount.acc.Partner_Hierarchy_Level__c = IS_Constants.PARTNER_HIER_COUNTRYSET;
        cntryGrpAccount.acc.Account_UCN__c = '';
        cntryGrpAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_NEW;
        cntryGrpAccount.acc.Account_UCN__c = account.Account_UCN__c;
        cntryGrpAccount.acc.WW_Partner_Parent_MDM_ID__c = account.WW_Partner_Parent_MDM_ID__c;
        cntryGrpAccount.acc.Geo_Parent_UCN_MDM_ID__c = account.Geo_Parent_UCN_MDM_ID__c;
        cntryGrpAccount.acc.RecordTypeId = nonCountryRecordTypeId;
        cntryGrpAccount.acc.Partner_Type__c = account.Partner_Type__c;
        cntryGrpAccount.acc.Partner_Locator__c = false;
                //INSALES-1215 - Partner Hierarachy - Parent Account Auto Creation, fields not populating       
            cntryGrpAccount.acc.BillingCountry = account.BillingCountry;
        For(Country__c Con:[Select Id,Theatre__c from Country__c where Name =: account.BillingCountry LIMIT 1]){
                cntryGrpAccount.acc.Geo__c = Con.Theatre__c;
        }

    }
    
    // Method to populate address fields in partner Accounts.
    public void populateAddrFields(Account acc, accountWrapper accWrap) {
        if(acc.BillingStreet != null && acc.BillingStreet != '') {
            accWrap.acc.BillingStreet = acc.BillingStreet;
        }
        if(acc.BillingCity != null && acc.BillingCity != '') {
            accWrap.acc.BillingCity = acc.BillingCity;
        }
        if(acc.BillingState != null && acc.BillingState != '') {
            accWrap.acc.BillingState = acc.BillingState;
        }
        if(acc.BillingPostalCode != null && acc.BillingPostalCode != '') {
            accWrap.acc.BillingPostalCode = acc.BillingPostalCode;
        }
        if(acc.BillingCountry != null && acc.BillingCountry != '') {
            accWrap.acc.BillingCountry = acc.BillingCountry;
        }
    }

    // Method to concatenate Account Address fields 
    public String concatenateAddress(Account acc) {
        String addrStr = '';
        if(acc.BillingStreet != null && acc.BillingStreet != '') {
            addrStr += acc.BillingStreet+', ';
        }
        if(acc.BillingCity != null && acc.BillingCity != '') {
            addrStr += acc.BillingCity+', ';
        }
        if(acc.BillingState != null && acc.BillingState != '') {
            addrStr += acc.BillingState+'- ';
        }
        if(acc.BillingPostalCode != null && acc.BillingPostalCode != '') {
            addrStr += acc.BillingPostalCode+', ';
        }
        if(acc.BillingCountry != null && acc.BillingCountry != '') {
            addrStr += acc.BillingCountry;
        }
        return addrStr;
    }

    // Method to move the the content of country pane to site pane and clearing out country pane.
    public void moveToSite() {
        contactList = [SELECT Id, Name, AccountId FROM Contact WHERE AccountId =: cntryAccount.acc.Id];
        if(contactList.size() > 0) {
            moveContactsToCountry = true;
        }
        siteAccount.acc = cntryAccount.acc;
        siteAccount.acc.Partner_Hierarchy_Level__c = IS_Constants.PARTNER_HIER_SITE;
        siteAccount.addr = concatenateAddress(cntryAccount.acc);
        siteAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_NEW;
        siteAccount.acc.Account_UCN__c = ''; //T-493722
        cntryAccount = new accountWrapper();
        isSitePartner = true;
    }

    // Method to move the the content of site pane to country pane and clearing out site pane.
    public void moveToCountry() {
        cntryAccount.acc = siteAccount.acc;
        cntryAccount.acc.Partner_Hierarchy_Level__c = IS_Constants.PARTNER_HIER_COUNTRY;
        cntryAccount.addr = concatenateAddress(siteAccount.acc);
        cntryAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_NEW;
        siteAccount = new accountWrapper();
        isSitePartner = false;
    }
    
    // Method to reset Data to the original state.
    public void reset() {
        populateAccountWrapper(countryAcc);
        isSitePartner = false;
    }

    // Method to allow user to go back to partner account detail page
    public PageReference cancel() {
        PageReference pg = new PageReference('/'+countryAccId);
        return pg;
    }
    
    // Method to save partner account records.
    public PageReference accept() {
        List<Contact> contactsToUpdate = new List<Contact>();
        String globalAccountName = ApexPages.CurrentPage().getParameters().get('globalAccountName'); 
        String cntryGrpAccountName = ApexPages.CurrentPage().getParameters().get('cntryGrpAccountName');

        Set<Account> accToInsert = new Set<Account>();
        List<Account> accToUpdate = new List<Account>();
        Account nullAcc = new Account();
        Map<String, Account> partnerHierAccountMap = new Map<String, Account>();
        //Fetching the assignment rules on case
        AssignmentRule AR = new AssignmentRule();
        AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];

        //Creating the DMLOptions for "Assign using active assignment rules" checkbox
        Database.DMLOptions dmlOpts = new Database.DMLOptions();
        dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;
        
        if(cntryAccount.acc == nullAcc) {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.WARNING,System.Label.Country_level_partner_required);
            ApexPages.addMessage(errormsg);
            return null;
        } else if( globalAccount.acc == nullAcc) {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.WARNING,System.label.Global_level_partner_required);
            ApexPages.addMessage(errormsg);
            return null;
        } else if( cntryGrpAccount.acc == nullAcc) {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.WARNING,System.label.Country_Grouping_partner_required);
            ApexPages.addMessage(errormsg);
            return null;
        } else if( siteAccount.acc == nullAcc && globalAccount.status != IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_NEW) {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.WARNING,System.label.Site_level_partner_required);
            ApexPages.addMessage(errormsg);
            return null;
        } 
        
        if(cntryAccount.acc.Partnership_Status__c != IS_Constants.CONST_REGISTERED) {
            cntryAccount.acc.Partnership_Status__c = IS_Constants.CONST_REGISTERED;
            //cntryAccount.acc.HQ_Flag__c = true;
            cntryAccount.acc.isPartner = true;
            cntryAccount.acc.Registration_Date__c = system.now();
            //Added as part of US1958 - To create automatic case when its partner account
            ISecG_Constants_Setting__c cs = ISecG_Constants_Setting__c.getOrgDefaults();
            Case caseToAdd = new Case();
            caseToAdd.Subject = 'New Partner Registered';
            caseToAdd.Request_Type__c = 'Salesforce Support';
            caseToAdd.Request_Detail__c= 'New Account';
            caseToAdd.AccountId = cntryAccount.acc.Id;
            caseToAdd.setOptions(dmlOpts); 
            casesToCreate.add(caseToAdd);//End of Case creation logic for US1958
        }
        if(casesToCreate.size()>0){
            insert casesToCreate;
        }//End if US1958
        
        if(globalAccount.acc != null) {
            if(globalAccountName != null && globalAccountName != '') {
                globalAccount.acc.Name = globalAccountName;
            }
            
            if(globalAccount.status == IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_NEW) {
                accToInsert.add(globalAccount.acc);
            } else if(globalAccount.status == IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_EXISTING) {
                accToUpdate.add(globalAccount.acc);
            }
        }
        
        
        if(cntryGrpAccount.acc != null) {
            if(cntryGrpAccountName != null && cntryGrpAccountName != '') {
                cntryGrpAccount.acc.Name = cntryGrpAccountName;
            }
            
            if(cntryGrpAccount.status == IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_NEW) {
                accToInsert.add(cntryGrpAccount.acc);
            } else if(cntryGrpAccount.status == IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_EXISTING) {
                accToUpdate.add(cntryGrpAccount.acc);
            }
        }
        
        if(cntryAccount.acc != null && (cntryAccount.status == IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_EXISTING || 
                                        cntryAccount.status == IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_NEW)) {
            accToUpdate.add(cntryAccount.acc);
        }
        
        if(siteAccount.acc != null && siteAccount.status == IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_NEW && !isSitePartner) {
            accToInsert.add(siteAccount.acc);
        } else if(isSitePartner) {
            accToUpdate.add(siteAccount.acc);
        }


        //try {
            if(accToUpdate.size() > 0) {
                update accToUpdate;
                system.debug('>>>>>>'+accToUpdate);
                for(Account acc : accToUpdate) {
                    partnerHierAccountMap.put(acc.Partner_Hierarchy_Level__c, acc);
                }
            }

            if(accToInsert.size() > 0) {
                List<Account> insertAccountsList = new List<Account>();
				insertAccountsList.addAll(accToInsert);
				insert insertAccountsList;
                for(Account acc : accToInsert) {
                    partnerHierAccountMap.put(acc.Partner_Hierarchy_Level__c, acc);
                }
            }
            
            // Set Partner Hierarchy of Accounts 
            List<Account> accList = new List<Account>();
            if(partnerHierAccountMap != null) {
                if(partnerHierAccountMap.containsKey(IS_Constants.PARTNER_HIER_SITE)) {
                    Account account = partnerHierAccountMap.get(IS_Constants.PARTNER_HIER_SITE);
                    account.ParentId = cntryAccount.acc.Id;
                    account.Global_Parent__c =globalAccount.acc.Id;
                    accList.add(account); 
                }
                if(partnerHierAccountMap.containsKey(IS_Constants.PARTNER_HIER_COUNTRYSET)) {
                    Account account = partnerHierAccountMap.get(IS_Constants.PARTNER_HIER_COUNTRYSET);
                    account.Global_Parent__c = globalAccount.acc.Id;
                    account.ParentId = globalAccount.acc.Id;
                    accList.add(account);
                }
                if(partnerHierAccountMap.containsKey(IS_Constants.PARTNER_HIER_COUNTRY)) {
                    Account account = partnerHierAccountMap.get(IS_Constants.PARTNER_HIER_COUNTRY);
                    account.Global_Parent__c = globalAccount.acc.Id;
                    account.ParentId = cntryGrpAccount.acc.Id;
                    accList.add(account);
                }
                system.debug('*** Account List'+accList);               
                update accList;
                
            }
            
            
            if(moveContactsToCountry) {
                for(Contact con : contactList) {
                    con.AccountId = cntryAccount.acc.Id;
                    contactsToUpdate.add(con);
                }
            }
            
            if(contactsToUpdate.size() > 0) {
                update contactsToUpdate;
            }
            system.debug('>>>>'+accToInsert);
        /*} catch(DmlException e) {
            if(e.getMessage().contains('DUPLICATES_DETECTED' )) {
                e.setMessage('You\'re creating a duplicate record. We recommend you use an existing record instead.');
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, e.getMessage()));
                return null;
            }
        }*/
        PageReference pg = new PageReference('/'+countryAccId);
        return pg;
    }
    
    // Method to update Account data when edited through the pane.
    public void populateAccountData() {
        List<Account> selectedAccountList = new List<Account>();
        String selectedAccId = ApexPages.CurrentPage().getParameters().get('selectedAccId'); 
        if(selectedAccId != '' && selectedAccId != null)  {
            selectedAccountList = [SELECT Id, Name, Partner_Hierarchy_Level__c, Account_UCN__c, BillingCountry, Partnership_Status__c, 
                                    BillingStreet, BillingCity, BillingState, BillingPostalCode, Partner_Type__c, ParentId, Legacy_Id__c,
                                    Geo_Parent_UCN_MDM_ID__c, WW_Partner_Parent_MDM_ID__c
                                    FROM Account
                                    WHERE Id =: selectedAccId];
        }
        if(selectedAccountList.size() > 0) {
            if(selectedAccountList.get(0).Partner_Hierarchy_Level__c == IS_Constants.PARTNER_HIER_GLOBALPARENT) {
                globalAccount.acc = selectedAccountList.get(0);
                globalAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_EXISTING; 
                cntryGrpAccount = new accountWrapper();
                cntryGrpAccount.addr = concatenateAddress(selectedAccountList.get(0));
            } else if(selectedAccountList.get(0).Partner_Hierarchy_Level__c == IS_Constants.PARTNER_HIER_COUNTRYSET) {
                cntryGrpAccount.acc = selectedAccountList.get(0);
                cntryGrpAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_EXISTING;
                cntryGrpAccount.addr = concatenateAddress(selectedAccountList.get(0));
                if(selectedAccountList.get(0).ParentId != null) {
                    globalAccount.acc = [SELECT Id, Name, Partner_Hierarchy_Level__c, Account_UCN__c, BillingCountry,
                                            BillingStreet, BillingCity, BillingState, BillingPostalCode, Partner_Type__c, ParentId,
                                            Geo_Parent_UCN_MDM_ID__c, WW_Partner_Parent_MDM_ID__c
                                            FROM Account
                                            WHERE Id =: selectedAccountList.get(0).ParentId];
                    globalAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_EXISTING;  
                }
            } else if(selectedAccountList.get(0).Partner_Hierarchy_Level__c == IS_Constants.PARTNER_HIER_COUNTRY) {
                cntryAccount.acc = selectedAccountList.get(0);
                cntryAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_EXISTING;
                cntryAccount.addr = concatenateAddress(selectedAccountList.get(0));
                if(selectedAccountList.get(0).ParentId != null) {
                    cntryGrpAccount.acc = [SELECT Id, Name, Partner_Hierarchy_Level__c, Account_UCN__c, BillingCountry,
                                            BillingStreet, BillingCity, BillingState, BillingPostalCode, Partner_Type__c, ParentId,
                                            Geo_Parent_UCN_MDM_ID__c, WW_Partner_Parent_MDM_ID__c
                                            FROM Account
                                            WHERE Id =: selectedAccountList.get(0).ParentId];
                    cntryGrpAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_EXISTING; 
                }
                if(cntryGrpAccount.acc.ParentId != null) {
                    globalAccount.acc = [SELECT Id, Name, Partner_Hierarchy_Level__c, Account_UCN__c, BillingCountry,
                                            BillingStreet, BillingCity, BillingState, BillingPostalCode, Partner_Type__c, ParentId,
                                            Geo_Parent_UCN_MDM_ID__c, WW_Partner_Parent_MDM_ID__c
                                            FROM Account
                                            WHERE Id =: cntryGrpAccount.acc.ParentId];
                    globalAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_EXISTING;  
                }
                if(cntryAccount.acc.Id != null) {
                    siteAccount = new accountWrapper(countryAcc);
                    siteAccount.acc.RecordTypeId = nonCountryRecordTypeId;
                    siteAccount.addr = address;
                    siteAccount.acc.Partner_Hierarchy_Level__c = IS_Constants.PARTNER_HIER_SITE;
                    siteAccount.accType = IS_Constants.PARTNER_HIER_SITE;
                    siteAccount.status = IS_Constants.CONST_PARTNER_ACCOUNT_STATUS_NEW;
                    isSitePartner = true;
                }
            }
            
        }
    
    }

    // Method to retrieve Partner Account results in edit modal window.
    @RemoteAction
    public static Object searchCountryAccounts( String level, String searchText) {
        String recordtypeName;
        if(level == IS_Constants.CONST_COUNTRY) {
            recordtypeName = IS_Constants.CONST_PARTNER_ACCOUNT_RECORDTYPE;
        } else {
            recordtypeName = IS_Constants.CONST_PARTNER_ACCOUNT_NONCOUNTRY_RECORDTYPE;
        }
        List<Account> relatedAccounts = new List<Account>();
        if(level != '' && searchText != '') {
            relatedAccounts = [SELECT Id, Name, Partner_Hierarchy_Level__c, Account_UCN__c, BillingCountry,
                                    BillingStreet, BillingCity, BillingState, BillingPostalCode, Partnership_Status__c,
                                    Geo_Parent_UCN_MDM_ID__c, WW_Partner_Parent_MDM_ID__c
                                    FROM Account 
                                    WHERE Partner_Hierarchy_Level__c =: level 
                                    AND RecordType.Name =: recordtypeName AND Name LIKE : searchText+'%'];
        }
        return JSON.serialize(relatedAccounts);
    }

    public class accountWrapper{
        public Account acc{get; set;}
        public String status{get; set;}
        public String accType{get; set;}
        public String addr{get;set;}

        public accountWrapper() {
            acc = new Account();
        }

        public accountWrapper(Account acc) {
            this.acc = acc;
        }
 
    }
    
    public void None() {
        
    }

}

Thanks,
Abhishek Bansal.
Abhishek BansalAbhishek Bansal
Hi,

I also found that there is a duplicate quetion with the same error message at this link : https://developer.salesforce.com/forums/ForumsMain?id=9060G000000MQNyQAO
Can you please close this question as it may confuse others.

Thanks,
Abhishek Bansal.
SHKSHK
The Error is at Line 385 in the above code when updating.
The line where error occurs is : 
                update accToUpdate;
Please tell me how to solve this !!
SHKSHK
The error is not while inserting but it is while updating. Please tell me how to fix this??