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
EdLEdL 

Simple trigger issue

Hey all,
 
I'm having a lot of trouble with a trigger I wrote.  It works *perfectly* within salesforce, however when I use the Data Loader to get data from a .CSV file, it's like the trigger never even fires.
 
It's designed to change a relationship by moving some information around.  For reference, the Project1 object has a Master-Detail relationship based upon a field called "Project_BEN_Number".  When I perform a bulk data load there are no errors given, the rows insert fine.  The problem is that the code is never executed, or at least the results are never saved.
 
Help!
 
Thanks,
 
EdL
 
 
Trigger Code:
Full disclosure:  I use an upsert to load the data, but it still doesn't work even with a regular insert.
 
Code:
trigger fixType on Resource_Data_Item__c (before insert) {

private Resource_Data_Item__c[] ti = Trigger.new;
try
{
    string initiative = ti[0].Initiative_Number__c;
    if(initiative == 'INIT_NA')
    {
        ti[0].Initiative_Number__c = ti[0].Project_Name__c;
        ti[0].Initiative_Name__c = ti[0].Project_Name__c;
        ti[0].Project_Name__c = ti[0].Task_Name__c;
        ti[0].Project_Number__c = ti[0].Task_Number__c;
        string pID = [select ID from Project1__c where Name = :ti[0].Project_Name__c].ID;
        ti[0].Project__c = pID;
    }
}
catch(Exception ex)
{
    ti[0].addError('Insert error: INIT_NA Task ' + ti[0].Task_Name__c + ' does not exist as a project.' + ex.getMessage());
}


}

 
mikefmikef
EdL:

Your Trigger is firing but it's only firing on the first record in your csv file.
You are setting the array with Trigger.new but you are evaluating you logic with the first record in your array. So if the first record passes the test you will update it, and ignore the rest. Or if the first record doesn't pass your test then you will ignore all the records in your insert.

Try this:
Code:
trigger fixType on Resource_Data_Item__c (before insert) {

private Set<String> projectName = new Set<String>();
private Map<String, Id> projectNameMap = new Map<String, Id();

for(Resource_Data_Item__c rdi : Trigger.new){
   projectName.add(rdi.Project_Name__c);
}

for(Project1__c project : [select ID, Name from Project1__c where Name in : projectName]){
   projectNameMap.put(project.Name,project.Id);
}

for(Resource_Data_Item__c rdi : Trigger.new){
try
{
    string initiative = rdi.Initiative_Number__c;
    if(initiative == 'INIT_NA')
    {
        rdi.Initiative_Number__c = rdi.Project_Name__c;
        rdi.Initiative_Name__c = rdi.Project_Name__c;
        rdi.Project_Name__c = rdi.Task_Name__c;
        rdi.Project_Number__c = rdi.Task_Number__c;
        rdi.Project__c = projectNameMap.get(rdi.Project_Name__c);
    }
}
catch(Exception ex)
{
    rdi.addError('Insert error: INIT_NA Task ' + rdi.Task_Name__c + ' does not exist as a project.' + ex.getMessage());
}
}


}

 Please look at this post for further explanation.

EdLEdL
Ah, what a novice mistake!  I was able to adapt your code and it *looks* at this point like it's working perfectly.  This also helped expose errors in some of my other triggers as well.
 
Thanks a ton!
EdLEdL
Oh and for the record:  I needed to modify
 
for(Resource_Data_Item__c rdi : Trigger.new){
   projectName.add(rdi.Project_Name__c);
}
 
to say projectName.add(rdi.Task_Name__c_); since that was the actual information that I needed -- I failed to realize that I was just copying the Project ID over later.
 
Thanks again.