You need to sign in to do that
Don't have an account?
Simple trigger to lookup a value from a non-related table
I'm very new to writing triggers, but I think it's time to learn. Here's the scenario:
I'm building an Invoice__c record by adding multiple Invoice_Item__c as Master Detail related list
An Invoice_Item__c consists of an Equipment__c lookup, an Equipment__c.Hourly_Rate__c.
Next, I need to calculate the tax added to this related record. I keep the tax rates in a non-related table
Tax_Rates__c
How would I write a trigger on the Invoice_Item__c object to pull Invoice__c.State then match it to Tax_Rates__c.State and return Tax_Rates__c.Rate into a field on Invoice_Item__c?
Okay. This should be pretty close (It feels a little heavy...but dont have the objects in an org to really test).
Notes.
1) With formula fields this would be a lot simpler...but I understand what you are doing
2) I actually would write this on the invoice level and let the rate trickle down via aformula field
All Answers
Some clarification:
1) Do you simply want to pull the Tax Rate to the Invoice_Item__c table? No calculations?
2) Can I ssume that you are going to pull the Invoice__c.State down to all child Invoice_Items__c via aformula field??
Very good questions. My plan is to learn how to do something like this without adding more fields to learn how I would go about coding this (vs. configure).
1 - I have the calculation already taken care of - right now the tax rate is a number field that the user can type into (highly error prone). I'd rather manage a taxation list and have this field auto-populate based on the "Invoice header"
2 - I would prefer not to pull the state down so I can learn something new (the hard way? :)
I hope my configuration is not too irritating, but I've been exploring this idea for quite a few hours and can't seem to get it to work in this manner due to syntax issues. I think I have the logic correct.
Okay. This should be pretty close (It feels a little heavy...but dont have the objects in an org to really test).
Notes.
1) With formula fields this would be a lot simpler...but I understand what you are doing
2) I actually would write this on the invoice level and let the rate trickle down via aformula field
Awesome! Thank you so much for the help (and the thought process behind each step!) I will give it a whirl and let you know the outcome.
-Aaron
Below is the code that works:
I had to change some of the parts of the final FOR loops, but it's tested and working. I tried to comment my thoughts as I was building each part of the trigger.
Some questions:
1 - Is it rather common to pull IDs into a SET, then build a map of <ID, 'data element'>, then add this info to a List?
Is the List the only way the info can be worked with? I'm not certain I understand the principles.
2 - You mentioned in your help that this would be better written with Classes. If you have some time, could you help me understand why Classes are better in this case and maybe show me the same trigger built with classes instead?
3 - Are my comments correct relating to each part of the trigger code is what is occuring?
Thank you very much for your help!
Here are my thoughts
1. It is common to create a set to get unique set of ids and then use them in a query to create a map or a list. I created a map in this case because they are easy to reference and reduce loops. I converted it to a list simply based on that is how I like to loop things. I assume you can loop through a map...i just dont that often. Bottom line is you to all your data collection outside loops.
2. I dont put logic in triggers for two reasons (A) They get really sloppy once you start adding more and more logic (2) When you stick the logic in a class it is reusable other places. Below is some quick code with it in a class:
3. Not this line
Triggers can happen in a bulk context so you could have up to 200.
Hope this helps!