2

I am working on an integration scenario where records are loaded into an custom object and The custom object is loaded twice due to the fact that data resides in two separate systems.

  • First customer information is loaded. Assume field1, field2 and
    field3 is updated.

  • Second financial information is loaded. This is when the trigger
    should fire and do some calculations/aggregations. In this case
    field4 is updated.

I don't want the trigger should fire in first case so I am comparing old values and new values of field4 and skipping the triiger logic but in few cases, the old value and new value of field4 is same when I do the second load. It means old value is 10000 and new value is also 10000. It is an update but same value. Since the updated value is same as old value, The check I put is skipping the trigger logic which I don't want.

Is there anyway to skip the triiger logic during first load and just run it during the second load. I thought of creating a flag field and set it to true during first load and check for it in trigger. If true then skip the trigger.

I would like to know If there is any other way to solve this issue without introducing another field.

4
  • does both the updates happen thru the same api / integration user ? or does each source system use its own dedicated user to update in salesforce ? Commented May 19, 2015 at 11:46
  • The integration happens via same user id.
    – sdondeti
    Commented May 19, 2015 at 12:01
  • does the first system always do an insert of a new record, or is that an update already? Commented May 19, 2015 at 12:19
  • The integration process uses upsert operation in both cases.
    – sdondeti
    Commented May 19, 2015 at 12:34

1 Answer 1

2

You can absolutely only fire your trigger when field4 changes; this can be accomplished via record filtering. You may want to look into the Selector library to help, or you can accomplish it in your code fairly simply:

public static List<MyObject__c> hasField4Changed(List<MyObject__c> newRecords,
    Map<Id, MyObject__c> oldMap)
{
    List<MyObject__c> records = new List<MyObject__c>();
    for (MyObject__c newRecord : newRecords)
    {
        MyObject__c oldRecord = oldMap.get(newRecord.Id);
        if (oldRecord != null && oldRecord.Field4__c != newRecord.Field4__c)
        {
            records.add(newRecord);
        }
    }
    return records;
}

UPDATE

Now that I understand your question better, it seems the ideal course of action would be to have a dedicated User for each integration and when the User for your second integration is the running user, assume the second integration is running.

I would recommend something like the following:

public class MyIntegrationServices
{
    @TestVisible static String INTEGRATION_USERNAME = '[email protected]`;
    public Boolean isRunningIntegration()
    {
        return UserInfo.getUsername() == INTEGRATION_USERNAME;
    }
}
5
  • As I mentioned above, I can't put the above logic because in my case, the old and new values are same during the load. During the second load, The trigger needs to run anytime there is an update regardless of the old and new values.
    – sdondeti
    Commented May 19, 2015 at 16:53
  • This logic will return no records during insert because your oldMap won't have any values. It will only return records whose Field4 value has changed during an update. You said that field was being updated...
    – Adrian Larson
    Commented May 19, 2015 at 17:00
  • OK. Let me explain with an example. In my case, the old value 1000 and new value is also 1000. If I put the above logic, it will skip trigger logic because old and new values are same. I always want to run the trigger logic during second load and want to skip only during first load.
    – sdondeti
    Commented May 19, 2015 at 19:41
  • You need some way to differentiate the options. You may be able to detect running user if your integrations each have a separate one dedicated to them specifically.
    – Adrian Larson
    Commented May 19, 2015 at 19:49
  • Thanks. That is what I am thinking. Either use separate user ids for loading OR create a field and set to true during second load so the logic is triggered and reset to false after the calculation is done.
    – sdondeti
    Commented May 19, 2015 at 19:52

You must log in to answer this question.

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