You need to sign in to do that
Don't have an account?
Smike25
Help with writing Trigger to update default value of master-detail relationship
Looking for some help on writing my first trigger. I have two custom objects ( Payroll [Payroll__c] and Commissions [Commissions__c] ). The commissions object has the Master-Detail(Payroll) rollup field (Bonus Program [Bonus_Program__c]).
Payroll has two records record ID's in it, but i'm only concerned with one of them. Any commission record created now will be eligible for a Bonus, and needs to be stamped with the Bonus record ID from Payroll (a0s230000008Rft). The other ID contains all the records that were not eligible for a bonus. I have a process that automatically creates a commission record based upon certain criteria, however now the process wont fire due to the "Bonus_Program__c" field being blank. I can't seem to use process builder to automaticcaly assign the ID, so i'm looking at trying to create the trigger. I've been trying some of the help posts through the community, but no luck so far getting much of anywhere.
Payroll has two records record ID's in it, but i'm only concerned with one of them. Any commission record created now will be eligible for a Bonus, and needs to be stamped with the Bonus record ID from Payroll (a0s230000008Rft). The other ID contains all the records that were not eligible for a bonus. I have a process that automatically creates a commission record based upon certain criteria, however now the process wont fire due to the "Bonus_Program__c" field being blank. I can't seem to use process builder to automaticcaly assign the ID, so i'm looking at trying to create the trigger. I've been trying some of the help posts through the community, but no luck so far getting much of anywhere.
Easiest
Better NOTE: This code has not been tested and may contain typographical or logical errors
The second trigger is safer because it does not use a hardcoded Id and would still work in a sandbox as long as someone creates a Payroll record with a name of Bonus.
All Answers
Easiest
Better NOTE: This code has not been tested and may contain typographical or logical errors
The second trigger is safer because it does not use a hardcoded Id and would still work in a sandbox as long as someone creates a Payroll record with a name of Bonus.
- @isTest
- public class TestBonusMapping {
- static testmethod void insertCommission() {
-
- Commissions__c c = new Commissions__c();
-
- c.Bonus_Program__c = 'a0s230000008Rft';
-
- insert c;
-
- }
- }
For the second one, i'm not sure where to start since i've never made a trigger with List<> command. Suggestions on where to start on that one? Code coverage states 40% using the same test as trigger 1, but i'm not defining anything through Payroll__c.This test creates the data you need for your test, then inside a start/stop test it inserts the Commission__c object. Then we fetch our data back and assert our trigger did the right thing. If you are using API version 24+ you will need to create all data inside of your test that you need for you classes / triggers. Unless you have a valid reason you should be using the current API version (35) for your triggers.
Error: Compile Error: unexpected token: static at line 1 column 0 when attempting to save the class.
"Error: Compile Error: sObject type 'Commission__c' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 18 column 22"
With code: