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
Gheorghe Sima 29Gheorghe Sima 29 

Override import recordtype

Hi,
I wrote a trigger (before insert) which put the right record type on a custom object records. I'm using the Data Impord wizard to import these records ( in this step I have to select a record type for the records I want to import). Can someone explain me why my trigger doesn't override the record type selected on the import.( I selected to execute the triggers from the Import wizard).

Thanks!
pconpcon
Can you please include your Trigger here?  It may be that your trigger is not written to handle bulk operations.

NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
Gheorghe Sima 29Gheorghe Sima 29

trigger UpdateRecordTypeLead on Lead (before insert) 
{
    for(Lead myLead: Trigger.New)
    {
        if(myLead.Manually_created__c == false)
        {  
            if(myLead.Company != null || myLead.Company != '')
            {
                if(myLead.Client_Type__c == 'Casnic')
                    myLead.RecordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('LeadPJcasnicReadOnly').getRecordTypeId();
                else
                    myLead.RecordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('LeadPJnoncasnicReadOnly').getRecordTypeId();    
            }
            else
            {
                if(myLead.Client_Type__c == 'Casnic')
                    myLead.RecordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('LeadPFcasnicReadOnly').getRecordTypeId();
                else
                    myLead.RecordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('LeadPFnoncasnicReadOnly').getRecordTypeId();
                
            }    
         }
    }    
}
I wrote a simple trigger. I don't know how to write a trigger to handle bulk operations. I put an error message in my trigger to find out if it is executed and apparently it is executed, but it has no effect.
 
pconpcon
Your trigger is fine for handling bulk operations (you iterate over every object that is being inserted and you are not doing any SOQL/DML in a loop).  You said you put an error message in your trigger and it did get executed.  Where did you put the message in your trigger?  Do you have any other triggers, workflows, process builders, etc that would change the record type?
Gheorghe Sima 29Gheorghe Sima 29
I put this code: myLead.addError('is executed') ;  in the first line of the for loop and I receive that error for every record  I try to import.
When I import the records with Data Import Wizard I have to select a recordtype for them and I think that the recordtype I selected can't be modified. This is the only explanation that I have in mind( but I can't understand why my trigger doesn't override that recordtype.....)
pconpcon
I would try adding that inside of the Manually_Create__c check.  You may have something that is setting this to true when you don't want it to be for your import.  Are you explicitly setting this field to false?
Gheorghe Sima 29Gheorghe Sima 29
I solved the problem. I delete the myLead.Company != '' condition from the first if intruction. Apparently the condition myLead.Company != '' is true when the Company field is null. Because of it my trigger doesn't work good.

Thanks!