0

i have scenario like write a trigger avoid insertion of records from data loader,only manual insertion trigger will fire?on any object

while Entering records from data loader trigger should not fire?

1
  • 3
    Hi, welcome to SFSE. What have you done so far towards this requirement? Where are you stuck? Please edit your question and take a quick look at How to Ask.
    – David Reed
    Mar 19, 2019 at 11:44

3 Answers 3

14

To me, this does seem like a valid requirement. Few years back I too asked a similar question.

Making context-aware application are sometimes needed. This is one such use case.

Yes, You can get this done. The trick is to look in URL of the request. Everything in SF happening will have a request.

So I just wrote a trigger on a Custom Object to see, if we can find a pattern in this and use it to our advantage.

trigger MyObjectTrigger on MyObject__c(before insert) {

    SYstem.debug(Url.getCurrentRequestUrl().toExternalForm());
}

Then I run certain tests:

  1. Insert Record in SF Classic:

    DEBUG : https://mydomain.salesforce.com/a2F/e

  2. In Lightning:

    DEBUG : https://mydomain.my.salesforce.com/aura?r=25&ui-force-components-controllers-recordGlobalValueProvider.RecordGvp.saveRecord=1

  3. In Execute Anon:

    DEBUG : https://mydomain.my.salesforce.com/services/data/v45.0/tooling/executeAnonymous/?anonymousBody=Insert+new+MyObject__c%28%29%3B&_=1553015078234

  4. In DataLoader:

    DEBUG : https://mydomain.my.salesforce.com/services/Soap/s/45.0/00D260000008529

  5. Finally In Dataloader using Bulk API:

    DEBUG : http://mydomain.my.salesforce.com/mockRequest/bulkapi/process/00D260000001895/75026000004gcSI/75126000004DSH7

So, technically yes, You can disable insert by DataLoader by checking the if requestURL contains /Soap/s/ or bulkapi

3
  • 3
    +1 Great answer!
    – Raul
    Mar 19, 2019 at 18:12
  • @PranayJaiswal that's an awesome bit of code... to see if the record was updated by a rest call out, would the url contain /services/apexrest Oct 21, 2019 at 15:30
  • @PartOfTheOhana On top of my head yes.. You can also check RestContext.request != null Oct 21, 2019 at 15:34
-1

You can remove the access to data loader by disabling API permission to that user's profile which is not supposed to use Data loader to insert records.

for more details please refer the link

1
  • This would also mean, that user wont be able to access SF mobile app Mar 19, 2019 at 12:19
-1

Other option can be like you can create a custom setting with some Flag Field value like true and false. When you want to insert via data loader, then make it false and after loading make it true. In trigger, make sure code will run only when that Flag is true.

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