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
Anand JeevakanAnand Jeevakan 

iscustomerportal

I'm trying to merge two accounts in apex.

Error: Invalid field IsCustomerPortal for merge. 

Below the method:
Though I try to skip the field in map in line number 14, the merge function still consider this field and throws the error. 
My org do not have communities enabled.
Any help is appreciated.
private static void MergeAccount(Id AccId,Id dupAccId){
        if ((AccId==null)||(dupAccId==null)) return;
        if (AccId==dupAccId) return;
        String querystring = 'select  ';
        String selectedObject = 'Account';
        Map<String, Schema.SObjectType> gdMap = Schema.getGlobalDescribe();
        Schema.Describesobjectresult dsr = gdMap.get(selectedObject).getDescribe();
        
		//Added below  on 15Dec20 - to ignore iscustomerportal while merging
		Map<String, Schema.SObjectField> tempMap = dsr.fields.getMap();
		//Map<String, Schema.SObjectField> fieldMap = dsr.fields.getMap();
		Map<String, Schema.SObjectField> fieldMap = new Map<String, Schema.SObjectField>();
		
		for (String fieldname: tempMap.keySet()){
                if(fieldName != 'IsCustomerPortal')
                    fieldmap.put(fieldname, tempMap.get(fieldname));
		}
		
		
        for(String fieldName : fieldMap.keySet()) {
			   if(fieldMap.get(fieldName).getDescribe().isUpdateable())
					querystring += ' ' + fieldName +',';
		   
        }
        querystring = querystring.left(querystring.length()-1);
        querystring += ' from Account where Id = :AccId OR Id = :dupAccId';
        List<Account> records = new List<Account>();
        List<Account> duplicates = new List<Account>();
		system.debug('querystring '+querystring);
        for (Account acc : (List<Account>)database.query(querystring)){
            if (AccId==acc.Id)
                records.add(acc);
            else if (dupAccId==acc.Id)
                duplicates.add(acc);
        }
        if ((records.isEmpty())||(duplicates.isEmpty())) return;
        
        set<string> billingAddressFields = new set<string>(); 
        billingAddressFields.add('BillingStreet'); 
        billingAddressFields.add('BillingCity');
        billingAddressFields.add('BillingState');
        billingAddressFields.add('BillingPostalCode');
        billingAddressFields.add('BillingCountry'); 
                
        set<string> shippingAddressFields = new set<string>(); 
        shippingAddressFields.add('shippingStreet'); 
        shippingAddressFields.add('shippingCity');
        shippingAddressFields.add('shippingState');
        shippingAddressFields.add('shippingPostalCode');
        shippingAddressFields.add('shippingCountry'); 
        
        if (string.isBlank(records.get(0).Description))
            records.get(0).Description = duplicates.get(0).Description;
        else if (string.isNotBlank(duplicates.get(0).Description))
            records.get(0).Description = records.get(0).Description+'\r\n'+duplicates.get(0).Description;
        for (Account record: records) {
            ////
            String recordid = record.Id;
			record.iscustomerportal = false;
            for (Account d :duplicates) {
               boolean nonBlankBillingAddress = false;
               boolean nonBlankShippingAddress = false;
               for(String fieldName : fieldMap.keySet()) {				   
				  if(fieldMap.get(fieldName).getDescribe().isUpdateable()) {
					// Need to skip address here, but keep track of address fields and merge if ALL address fields blank
					 if (!billingAddressFields.contains(fieldName) && !shippingAddressFields.contains(fieldName)) {
						boolean continueProcessing = true;
						// Don't update parentId field with record id of master
						if (fieldName.equalsIgnoreCase('ParentId'))
						   if (d.get(fieldName) != null)
							  if (d.get(fieldName) == recordid) 
								 continueProcessing = false;
						if (d.get(fieldName) != null && record.get(fieldName) == null && continueProcessing)
						   record.put(fieldName, d.get(fieldName));
						else if(fieldMap.get(fieldName).getDescribe().getType() == Schema.DisplayType.Boolean)
						   if (continueProcessing && d.get(fieldName) == true)
							  record.put(fieldName, d.get(fieldName));
					 }
					 else
						if (record.get(fieldName) != null && billingAddressFields.contains(fieldName))
						   nonBlankBillingAddress = true;
						else if (record.get(fieldName) != null && shippingAddressFields.contains(fieldName))
						   nonBlankShippingAddress = true;
				  }
				   
               }
               // Set Address fields if blank
               if (!nonBlankBillingAddress) {
                    record.put('BillingStreet', d.get('BillingStreet'));
                    record.put('BillingCity', d.get('BillingCity'));
                    record.put('BillingState', d.get('BillingState'));
                    record.put('BillingPostalCode', d.get('BillingPostalCode'));
                    record.put('BillingCountry', d.get('BillingCountry'));
               }
               if (!nonBlankShippingAddress) {
                    record.put('ShippingStreet', d.get('ShippingStreet'));
                    record.put('ShippingCity', d.get('ShippingCity'));
                    record.put('ShippingState', d.get('ShippingState'));
                    record.put('ShippingPostalCode', d.get('ShippingPostalCode'));
                    record.put('ShippingCountry', d.get('ShippingCountry'));                    
               }
               
           }
            ////
            Boolean  completeMerge = false;          
            if (duplicates.size() < 3 && duplicates.size() > 0)    {
                Database.MergeResult[] results = Database.merge(record, duplicates, false);
                
                for(Database.MergeResult res : results) {
                    if (res.isSuccess()) {
                        // Get the master ID from the result and validate it
                        completeMerge  = true;
                        System.debug('Master record ID: ' + res.getId());
                    }
                    else {
                        for(Database.Error err : res.getErrors()) {
                            // Write each error to the debug output
                            System.debug(err.getMessage());
                        }
                    }
                }
            } else if (duplicates.size() < 3) 
                System.debug('Batch Merge: skip records too many for master record: ' + record.Id);
            else
                System.debug('Batch Merge: skip no duplicates for master record: ' + record.Id);
        }
        update records;
    }
    
}

 
SwethaSwetha (Salesforce Developers) 
HI Anand,
Can you provide a simplified code snippet that can be used to reproduce this error from my end?

Checked related: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AeGwIAK
https://salesforce.stackexchange.com/questions/156034/problem-while-merging-a-standard-account-with-a-customer-portal-account
Anand JeevakanAnand Jeevakan
Hi Swetha, 
Thanks for sharing those links. I'm able to filter the field in the keyset. However, Database.merge considers all fields including IsCustomerPortal for merging and throws the same error:
Error: Invalid field IsCustomerPortal for merge. 
 
Map<String, Schema.SObjectField> tempMap = dsr.fields.getMap();
        Map<String, Schema.SObjectField> fieldMap = new Map<String, Schema.SObjectField>();
        
        for (String fieldname: tempMap.keySet()){  
			if(fieldName != 'IsCustomerPortal')
				fieldmap.put(fieldname, tempMap.get(fieldname));
        }
		system.debug('fieldmap IsCustomerPortal : ' + fieldmap.get('IsCustomerPortal')); --> This is printing NULL in the log.

Can we skip fields from getting merged while using database.merge.
 
for(Database.MergeResult res : results) {
                    if (res.isSuccess()) {
                        // Get the master ID from the result and validate it
                        completeMerge  = true;
                        System.debug('Master record ID: ' + res.getId());
                    }
                    else {
                        for(Database.Error err : res.getErrors()) {
                            // Write each error to the debug output
                            System.debug(err.getMessage());
                        }
                    }
                }

Can you please tell me:
What feature brings up this field: IsCustomerPortal
The org do not have communities or person accounts enabled.

Any thoughts will be helpful.