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
SambitNayakSambitNayak 

Create an account every time a contact is created

Hi,
I have a requirement to create a new account(if the entered account doesn't exist already) every time a contact is created/edited from ETL (The ETL profile user) 

    public static void createAutoVendor(List<Contact> contList, boolean isInsert, boolean isUpdate, Map<Id, Contact> oldContacts){
        Set<Id> existingAccs = New Set<Id>();
        Set<String> existingAccNames = New Set<String>();
        List<Account> newAccs = New List<Account>();
        Map<String, Id> mapAccToCont = New Map<String, Id>();
        Id createdBy=[SELECT Id FROM User WHERE Name  = 'ETL User'].id;
                
        //Select all the existing Accounts and add them to the Set of Accounts
        for (Account acc:[SELECT Id, Name FROM Account]){
            existingAccs.add(acc.id);
            existingAccNames.add(acc.Name);
        }
        for (Contact con:contList){
            if(con.Vendor__c <> null){  //Vendor__c is the name of the account in ETL

                if(((isInsert && con.CreatedById==createdBy) ||
                   (isUpdate && con.LastModifiedById==createdBy 
                    && con.Vendor__c<>oldContacts.get(con.Id).Vendor__c))
                  && (!existingAccNames.contains(con.Vendor__c))){
                       Account Acc = New Account(name=con.Vendor__c, Type='Vendor'); //Create a new Account if it's not existing already
                       newAccs.add(Acc);

                   }
                insert newAccs;

            }            
        }
        for (Account acc:newAccs){
            mapAccToCont.put(acc.Name, acc.Id);

        }
        for (Contact con:contList){
            if(mapAccToCont.containsKey(con.Vendor__c)){
                con.AccountId=mapAccToCont.get(con.Vendor__c);

            }
        }        
    }

This code is working when I am running from the "Execute Anonymous Window" as an Admin. But failing from ETL with following error:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ContactTrigger: execution of BeforeInsert

Any help in this regard is really appreciated.
Best Answer chosen by SambitNayak
SambitNayakSambitNayak
Hi @Mukesh. Thanks for your help. 
I see the profile already has this access. However, if you can see-- my code is not bulkified (DML inside FOR loop). I just noticed that.
Trying to fix that.

All Answers

mukesh guptamukesh gupta
Hi Sambit,

Main Reason:-  Your ETL profile don't have permission  to Create a  Account. please grant create account permission on ETL Profile.



if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
SambitNayakSambitNayak
Hi @Mukesh. Thanks for your help. 
I see the profile already has this access. However, if you can see-- my code is not bulkified (DML inside FOR loop). I just noticed that.
Trying to fix that.
This was selected as the best answer