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
Lisa SchutterLisa Schutter 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, entity type cannot be inserted: Person Account: []

Hi All, Hope someone can help me. After the sandbox refresh I am running a script to update accounts. It works for all my objects, but Person Accounts. Does anyone have any Idea how to fix it? 

This is the error that is shown in the last line of my debug:
 
EXCEPTION_THROWN|[47]|System.DmlException: Insert failed. First exception on row 2; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, entity type cannot be inserted: Person Account: []

This is my apex class:
 
global class sandboxRefreshScript implements SandboxPostCopy{
global void runApexClass(SandboxContext context)
{
 
List<Account> accounts = new List<Account>();

Account a1 = new Account();
a1.Name = 'J&T Autolease';
a1.Ownerid = '005w0000005bvNR';
a1.Type = 'LeaseCo';
a1.Status__c = 'Partner';    
a1.Phone = '013 820 07 00';
a1.BillingStreet = 'Kraaivenstraat 4';
a1.BillingCity = 'Tilburg';
a1.BillingPostalCode = '5048 AB';
a1.BillingState = 'Zuid-Holland';
a1.ShippingStreet = 'Kraaivenstraat 4';
a1.ShippingCity = 'Tilburg';
a1.ShippingPostalCode = '5048 AB';
a1.ShippingState = 'Zuid-Holland';    

accounts.add(a1);

Account a2 = new Account();
a2.Name = 'Adler';
a2.Ownerid = '005w0000006WaIF';
a2.BillingCountry = 'Netherlands';
a2.ShippingCountry = 'Germany';   
    
accounts.add(a2);

Account a3 = new Account();
a3.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
a3.Status__c = 'Customer';    
a3.OwnerId = '005w0000004Sz1E';
a3.Salutation = 'Mr.';
a3.FirstName = 'Trafineo';
a3.LastName = 'Trafineo';    
a3.PersonEmail = 'settlement+trafineo@thenewmotion.com';
a3.PersonMailingCity = 'Bochum';
a3.PersonMailingStreet = 'Wittener Straße 56';
a3.PersonMailingPostalCode = '44789'; 
a3.PersonMailingCountry = 'Germany';
a3.hidden_email__c = 'settlement+trafineo@thenewmotion.com';    
    
accounts.add(a3);    
insert Accounts;
    
List<Contact> contacts = new List<Contact>();

Contact b1 = new Contact();
b1.Salutation = 'Mr.';
b1.FirstName = 'Arjen';
b1.LastName = 'Butter';
b1.Email = 'c.leusink@newmotion.com';    
b1.Accountid = a1.ID;    

contacts.add(b1);

insert Contacts;
List<Opportunity> opportunities = new List<Opportunity>();

Opportunity c1 = new Opportunity();
c1.Name = 'Zonnestraal';
c1.Ownerid = '005w0000005bvM4';
c1.StageName = 'Intake';
c1.Accountid = a1.ID;    

opportunities.add(c1);

insert Opportunities;

List<Payment_Account__c> paymentaccounts = new List<Payment_Account__c>(); 

Payment_Account__c d1 = new Payment_Account__c();
d1.Name = 'J&T Autolease*';
d1.Ownerid = '005w0000005bvM4';
d1.Billing_Name__c = 'Financiele administratie';   
d1.Billing_Email__c = 'l.schutter@newmotion.com';
d1.Payment_Terms__c = '30';
d1.Billing_Address__c = 'Postbus 2094';      
d1.Billing_PostalCode__c = '5001 CB';      
d1.Billing_City__c = 'Tilburg';      
d1.Billing_Country__c = 'NL';
d1.Account__c = a1.Id;    

paymentaccounts.add(d1);

insert paymentaccounts;   
}
}


 
Alain CabonAlain Cabon
Hi,

Is this Account record type assigned to your user's profile?

"I am running a script to update accounts."  you run this script with you own user (admin?) 

Before you begin, make sure to:
  1. Create at least one record type for accounts.
  2. Grant read permission on contacts for profiles that have read permission on accounts.
  3. Ensure that the contact sharing organization-wide default is set to “Controlled by Parent.”
  4. Once you complete the preliminary steps, contact Salesforce to enable person accounts and then:

From the object management settings for person accounts, go to Record Types.
  1. Assign person account record types to profiles that require person accounts.
https://help.salesforce.com/articleView?id=account_person_enable.htm&type=0

Regards
Lisa SchutterLisa Schutter
Hi Alain,

Thank you for your awser, we have Person Accounts set up and I am running the script from my account (admin). 

Also checked the profil settings to be sure, but they seem fine. Do you have any idea what could be the solution?

User-added image
Alain CabonAlain Cabon

1) Could you show the record type settings of your profile like below?

User-added image


2) CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY is a scourge of SF (the real reason is sometimes very hard to find).

Have you got some triggers on you account (insert)?  (recursive trigger, a bad conception of your code)

Regards
Lisa SchutterLisa Schutter
Hi Alain, where can I find the record type settings? 

This is our Account trigger:
 
trigger AccountTrigger on Account (after delete, after insert, after undelete, 
after update, before delete, before insert, before update) {
	System.debug('+++trigger.isBefore: '+trigger.isBefore);
	System.debug('+++trigger.isInsert: '+trigger.isInsert);
	System.debug('+++trigger.isUpdate: '+trigger.isUpdate);
	AccountUtil.entry(new TriggerParams(trigger.isBefore, trigger.isAfter, trigger.isInsert, trigger.isUpdate, trigger.isDelete,
                                                            trigger.isUndelete, trigger.new, trigger.old, trigger.newMap, trigger.oldMap));
}



 
Alain CabonAlain Cabon
1) Ok, you have enabled "Enhanced Profile User Interface" and the display is different from mine.

You have already the assigned recordtype "Person Account" for your profile (we don't see the name of the profile nevertheless but if it is yours, it is ok). You could disable "Enhanced Profile User Interface" for the same display (not necessary) ( setup > user interface)

2) AccountUtil.entry(new TriggerParams(trigger.isBefore, trigger.isAfter, trigger.isInsert, trigger.isUpdate, trigger.isDelete, trigger.isUndelete, trigger.new, trigger.old, trigger.newMap, trigger.oldMap)); 

It is the new format of a generic trigger and the code is in AccountUtil, in the static method entry, we could verify the code for the "after insert".
 
Alain CabonAlain Cabon
AccountUtil is a class.  ( setup > Apex Classes )
Lisa SchutterLisa Schutter
Hi Alain,

This is the account util at the moment, where i am puttin it?:
 
public with sharing class AccountUtil {
    public static void entry(TriggerParams triggerParams) {
        List<Account> triggerNew = (List<Account>)triggerParams.triggerNew;
        System.Debug('+++AccountUtil: entry');
        System.Debug('+++triggerParams'+triggerParams);
        System.Debug('+++triggerParams.isBefore: ' +triggerParams.isBefore);
        System.Debug('+++triggerParams.isInsert: ' +triggerParams.isInsert);
        System.Debug('+++triggerParams.isUpdate: ' +triggerParams.isUpdate);
        if (triggerParams.isBefore) {         
        	System.Debug('triggerParams.isBefore');  
            if(triggerParams.isInsert||triggerParams.isUpdate){
            	System.Debug('triggerParams.isInsert||triggerParams.isUpdate');
                //copyInfoFromLead(triggerNew);
            }
        }
    }
     
}

 
Alain CabonAlain Cabon
Ok the trigger does nothing given that: //copyInfoFromLead(triggerNew); is disabled.

You have to verify your rights for each field of the request. Perhaps one field is not visible for you.
List<Account> accounts = new List<Account>();
Account a3 = new Account();
a3.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
a3.Status__c = 'Customer';    
a3.OwnerId = '005w0000004Sz1E';
a3.Salutation = 'Mr.';
a3.FirstName = 'Trafineo';
a3.LastName = 'Trafineo';    
a3.PersonEmail = 'settlement+trafineo@thenewmotion.com';
a3.PersonMailingCity = 'Bochum';
a3.PersonMailingStreet = 'Wittener Straße 56';
a3.PersonMailingPostalCode = '44789'; 
a3.PersonMailingCountry = 'Germany';
a3.hidden_email__c = 'settlement+trafineo@thenewmotion.com';    
accounts.add(a3);    
insert Accounts;

1) Copy paste the code above in the anonymous window ( CTRL + E ) of the Developer Console and "Execute Highlighted"
2) Verify the debug logs generated (double click) in the tab "Logs"  (many processes could have been activated when you create or update an account not just the triggers but flows, workflows (process builders), validation rules, and so on)
3) SELECT id FROM user WHERE Id = '005w0000004Sz1E';

Regards
Lisa SchutterLisa Schutter
Hello,

I am receiving this error when I am pressing the button (before logs). But I think this might be because I am doing this in the production area. 
 
Line: 16, Column: 1
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: hidden_email__c duplicates value on record with id: 001w000001YySip: []
In the sandbox I am receiving:
 
Line: 5, Column: 1
System.StringException: Invalid id: <b>005w0000004Sz1E</b>


 
Alain CabonAlain Cabon
Sorry, tags for bold in the text (copy/paste errror)

a3.OwnerId = '005w0000004Sz1E';

Regards
Lisa SchutterLisa Schutter
Hi Alain,

This is what I find. What to do next? :)

User-added imageUser-added image
Lisa SchutterLisa Schutter
So the account is created. 
Alain CabonAlain Cabon
If the account is created, you can query the object account :

SELECT id , LastName , FirstName , RecordType.Name
FROM account
WHERE PersonEmail = 'settlement+trafineo@thenewmotion.com';

or

SELECT id,LastName,FirstName  
FROM account
WHERE RecordType.Name = 'Person Account'

so you have all the rights

Regards
Lisa SchutterLisa Schutter
Hi Alian, thank you for you help sofar. I can query both queries. Why do you think I can't run the script, but can do this in the anynomous editor?
Alain CabonAlain Cabon
Hi, you have all the permission ("I can query both queries": with the results) and therefore you have created the person account.

How do you launch the script?  "I am running a script"
Regards
Lisa SchutterLisa Schutter
Hi Alain,

When refresh or create a sandbox I indicate this apex class. 

https://releasenotes.docs.salesforce.com/en-us/spring16/release-notes/rn_deployment_sandbox_postcopy_script.htm