5

Hi I want to skip records base condition in trigger while data import. For ex: Am importing records into accounts, now i want to skip accounts where city is Mumbai, how can i achieve this through trigger?

3 Answers 3

5

Definition of Skip. If by 'skip' during data import you want to avoid them being inserted, as apposed to skipping validation. You will have to use your trigger to mark the records as in error. The platform will not insert those records.

Using addError. The default behaviour of most data loaders is to permit valid records within a batch to be inserted, giving you a detailed report of each record that was flagged as in error that did not get imported. So if you really don't want to insert records that meet certain criteria (either an explicit check for Mumbai or a checkbox field), my suggestion is to use addError to indicate this.

for(Account account : Trigger.new)
{
    if(account.. // Condition preventing insert of Account)
    {
        account.addError('You cannot insert this record, because....');
    }
}
5

If you are using CSV to load the data file you can filter on excel itself to remove rows with city mumbai.

Other way to do this will be using if statements and not collecting those records for update with city ='mumbai'

List<Account> lstacc=new List<Account>();

for(Account acc:trigger.new){
    if(acc.city!='Mumbai'){
       lstacc.add(acc);
    }
}

AccountTrrigerHandler.Accounthelper(lstacc);

And pass lstacc list into handler class

public class AccountTrrigerHandler{

    public static void Accounthelper(List<Account> lstaccs){

    }

 }
3
  • where we are inserting lstacc, if we insert from handler the trigger will become recursive right?
    – Reddy
    Commented Oct 8, 2013 at 5:08
  • in before trigger you need not explicitly update .Use before trigger event and you skip those with mumbai for your logic Commented Oct 8, 2013 at 6:21
  • Can you send me pseudo code, am not getting what exactly you are saying.
    – Reddy
    Commented Oct 8, 2013 at 7:19
3

I would implement this by just adding a checkbox to the field called skip_trigger__c or similar, and then modify the trigger to skip over those records when that field is checked:

for(Object__c obj : trigger.new)
{
  if(obj.skip_trigger__c)
  {
    continue;
  }

  // stuff
}

Then in your source data you can use an Excel formula or similar to set that row to true for records you want to skip in the trigger. This offers more flexibility than testing particular fields for certain values inside the trigger, that said if you've only ever got one specific requirement then you might as well do it in the trigger directly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .