I'm trying to write an update trigger on custom orders object. There is a field Ship_To__c in orders object which is a text value. Another table SUBSCR_SYSTEMS__c holds data LEGACY_CUST_NUM__c that is same as Ship_To__c. These two fields have no relation.

So I'm updating another field account__c in orders__c object by querying the same from SUBSCR_SYSTEMS__c.

I checked the debug logs, I see that the before update event fires off but there is no change in account__c field in orders. What am I doing wrong?

This is what I have tried so far:

trigger updateAccountName on orders__c (before insert, before update) {
List<orders__c> ord = new List<orders__c> ();
List<SUBSCR_SYSTEMS__c> partnerAcc =new List<SUBSCR_SYSTEMS__c>();

for(orders__c go: Trigger.new)
        partnerAcc=[select Account__c from SUBSCR_SYSTEMS__c where LEGACY_CUST_NUM__c=: go.Ship_To__c];
update ord; 



  • Hi @adrian How did u edit the bold letter into grey highlights? Just curious, looks better too! – MnZ Feb 13 '15 at 17:07

Two Things :

  • First : Do not make query in loop !

  • Second : you're in a before trigger, do not perform an update dml for the records in the trigger.new

And you have a mistake in your code i think : if(go.Ship_To__c=='') should be if(go.Ship_To__c!='')

I definitive, your code should be

trigger updateAccountName on orders__c (before insert, before update) {

    Map<String,SUBSCR_SYSTEMS__c> partnerAcc =new Map<String,SUBSCR_SYSTEMS__c>();
    Set<String> shipTo = new Set<String>();
    List<orders__c> orders = new List<orders__c>();

    for(orders__c go: Trigger.new)

    for(SUBSCR_SYSTEMS__c s:[select Account__c from SUBSCR_SYSTEMS__c where LEGACY_CUST_NUM__c IN: shipTo])

    for(orders__c go: orders)
  • Thanks! My sql query will return only 1 value so I kept in loop. You are correct about the condition too. I remove that! I get this error on trying your suggestion... Error: Compile Error: Variable does not exist: go.Ship_To__c – MnZ Feb 13 '15 at 14:36
  • 1
    Your query get only one record by order no ? So if you have 200 orders record (batch, dataloader...) you will perform 200 SOQL queries witch is very bad ! That's why we move the query outside loops – Yann Gorvel Feb 13 '15 at 14:39
  • 1
    I have make some edit in my code maybe you have copy/paste before that. Your error is on the SOQL query on SUBSCR_SYSTEMS__c object ? I had left a mistake in the query – Yann Gorvel Feb 13 '15 at 14:40
  • Hi I tried and error pointed to the SOQL with ERROR as Execution Of BeforeUpdate Caused By: System.QueryException: Non-selective Query Against Large Object Type... – MnZ Feb 13 '15 at 14:52
  • 1
    Thanks :). Don't forget to mark you question as answered :) – Yann Gorvel Feb 13 '15 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.