0

I have two custom objects. The first stores transactional data with a externally generated customer ID. The second custom object maps this customer ID to a salesforce account ID.

I'm trying to find the best way to create a relationship between these two objects after the data has been inserted.

Just to be clear, the relevant records for the custom objects are as follows

Object: Transactions__c Fields: customer_id__c (externally generated ID), lookup__c (field I'd like to populate with ID's from other object)

Object: ID_mapping__C Fields: ID, external_customer_id__c (externally generated ID), Salesforce_account_id

The below trigger works, but I'm hitting governor limits as I believe the SOQL query is running for every record (normally around 2000 per time).

Is it possible to covert to an apex class and run the SOQL once, referencing back to this and updating records in bulk. I'm new to coding in general and have been learning as I go. I've never googled a problem so much (I went to the 6th page!), Any help here would be greatly appreciated.

trigger SF_AccountID_on_ikentoo_importv2 on Transactions__c (before insert, before update) {

    Set<String> SellAGIDs = new Set<String>();
    for(Transactions__c tc : trigger.new){
      if(tc.customer_id__c  !=NULL){SellAGIDs.add(tc.customer_id__c);

                                     //this will map the relevant IDs for parent record
        Map <String, ID_mapping__C> SellAG = new Map <String, ID_mapping__C>();
        for (ID_mapping__C obj: [SELECT ID, external_customer_id__c
            from ID_mapping__C
            Where external_customer_id__c IN: SellAGIDs]){SellAG.put(obj.external_customer_id__c,obj);}


                tc.id_lookup__c=SellAG.get(tc.customer_id__c).ID;
       }
    }



}

1 Answer 1

1

You are kind of on the right track. You are creating a Map, which is good, but you are creating a new map for every single lookup for some reason. You do not need a separate Apex class, you just need to properly bulkify your trigger. That means you should only need to run the SOQL statement once for your collection of Customer Ids, then build the map, then reapply it to your items.

Try this:

trigger SF_AccountID_on_ikentoo_importv2 on Transactions__c (before insert, before update) {

  Set<String> SellAGIDs = new Set<String>();
  for (Transactions__c tc : trigger.new) {
    if(tc.customer_id__c != NULL) {
      SellAGIDs.add(tc.customer_id__c);
    }
  }

  // Create Map of Customer Id to Id Mapping Object
  //this will map the relevant IDs for parent record
  List<ID_Mapping__c> idMappings = new List<ID_Mapping__c>([SELECT ID, external_customer_id__c
        from ID_mapping__C Where external_customer_id__c IN: SellAGIDs]);   
  Map<Id, ID_Mapping__c> SellAG = new Map<Id, ID_Mapping__c>();
  for (ID_Mapping__c idMapping : idMappings) {
    SellAG.put(idMapping.External_Customer_Id__c, idMapping);
  }

  // Loop back through Transactions and apply Customer Id   
  for (Transactions__c tc : trigger.new) {
    if(tc.customer_id__c  != NULL) {
      tc.id_lookup__c=SellAG.get(tc.customer_id__c).ID;
    }
  }   
}
1
  • It is still best practice to move trigger code to a class. Among other reasons, it allows you to explicitly set sharing. Triggers default to without sharing which is not necessarily what you want.
    – Adrian Larson
    Commented Mar 24, 2015 at 22:50

You must log in to answer this question.

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