0

I have a trigger where I want to lookup values in another table and populate a field. Why doesn't trigger.new contain the new value? The debug shows a null value even though I'm setting the shippingcountry

    trigger mevSetAccountRegion on Account (before insert, before update) {
      list<countryregion__c> crl = [select country__c, region__c from countryregion__c];
      if(trigger.isBefore) {
        for(Account a: trigger.new) {
          system.debug(logginglevel.info,'MEVION DEBUG: shipping country ' + a.shippingcountry); 
          system.debug(logginglevel.info,'MEVION DEBUG: account name and id ' + a.name + ' : ' + a.id);   
          for(countryregion__c cr: crl) {
            if(cr.country__c==a.shippingcountry) { a.region__c = cr.region__c; }
          }
        }
      }
    }
  • 1
    The code shows that you do have values in Trigger.new. Are you sure you're updating shipping and not some other address field? Try debugging the full account object to see what's in it. – Acuariano Nov 12 '18 at 21:42
  • Your debug statement is prior to your assignment. If you want to see the values after your update, you need to move the debug to after your loop. – David Reed Nov 12 '18 at 21:49
  • I edit the existing account record and choose a shipping country the old value should be null and the new value should be the country I chose. I save the trigger debug says the new value is blank? I edit the record and save it making no changes the trigger value now show the new value? – Ohmicron Nov 12 '18 at 21:50
  • The debug is showing the value of shippingcountry not the value of the region__c that I set later. I added a debug for the name and account id and it shows value in the trigger.new 16:49:43.99 (104233058)|USER_DEBUG|[6]|INFO|MEVION DEBUG: shipping country null 16:49:43.99 (104288838)|USER_DEBUG|[7]|INFO|MEVION DEBUG: account name and id Xiangya Hospital : 001d0000027F5HeAAK – Ohmicron Nov 12 '18 at 21:50
  • Have you tried putting the shipping country debug after the id/name debug to see if it still doesn't log it? May sound weird, but I have seen issues around trigger maps and its values. – Jayant Das Nov 12 '18 at 22:00

Your Answer

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

Browse other questions tagged or ask your own question.