+ Start a Discussion
tedevangtedevang 

How can I modify this trigger to not run on Dataloader bulk updates?

This trigger updates a custom task field. I need to bulk load values for this field for existing tasks. How do I keep this trigger from running when doing a Dataloader update (which gives me a "too many SOQL queries" error). Any code snippets for this would be very helpful. Thanks.

 

trigger TaskLinkToParentAccount on Task(before insert, before update) {

    for(Task e : Trigger.new) {
        if    (
            e.WhoId != NULL
            )
        {
        String Link_Id = e.WhoId;
        String Account_ID = '';  
        String Account_Name = '';
        if    (
            Link_Id.startsWith('003')//Contact link
            )
        {         
            Account_ID = [SELECT AccountId FROM Contact WHERE id = :Link_Id limit 1].AccountId;
            Account_Name = [SELECT Name FROM Account WHERE id = :Account_ID limit 1].Name;
        }

        e.Account_Name__c = Account_Name;
        }
    }
   }

SuperfellSuperfell

Probably the easiest way is to not use a trigger at all, unless I'm missing something, a formula field with the formula Account.name will give you the same thing.

tedevangtedevang

Simon - as far as I've been able to tell (and also SF Premier support) you cannot pull in the account name into a task using a formula field, which is why I had to do the trigger. Now the trigger works fine in normal use, I just want to suspend it while I bulk import the Account_Name__c value for existing tasks. And it's a pain to have to undeploy the trigger, then redeploy after the bulk update.

 

Any ideas?

 

Thanks,

 

Ted

SuperfellSuperfell

I was able to do it with a formula field using the Account.name formula, it worked fine for me.

tedevangtedevang

That's strange. When I try to do a text formula for an Activity Custom Field and click "Insert Field" I only have Activity with it's fields (which don't include account name), $Api, $Organization, $Profile, $System, $User and $UserRole. If I manually enter Account.name it saves but then the formula field does not display even though it's on my layout.

 

Is this the process you followed: 1) New Activity Custom Field, 2) Set to Formula field, 3) Enter name and select Text, 4) Enter Account.name in formula editor, 5) Save/Place on layout?

 

Thanks,

 

-Ted

SuperfellSuperfell

Yes.

 

 

 

 

 

tedevangtedevang

Sorry - it actually worked when I entered Account.name but since I was in Task Edit mode I didn't see it. Problem solved with formula field. Strange though that it doesn't appear in the Field selection list and Salesforce Premier Support said it couldn't be done.

 

Anyway, thanks for your help.

 

-Ted

SennahSennah

Also, you could switch the DataLoader to a batch size of 1. That should prevent you from the "too many SOQL queries" error.

 

Or you learn how to write bulk-able triggers. There are some good examples out there how to build a map and work with it. For example on Jeff Douglas blog I remember one. Give it a try!