You need to sign in to do that
Don't have an account?
Apex Trigger Not Firing
Hi,
I have been working for the last 3 days on a couple of trigger and test classes in the Sandbox. Which I got 100% and was working all fine in Sandbox. Now when I move it too production it doesn't work.
I did a refresh of the sandbox before moving it so the fields / val rules are the same.
I have turned on the debug logs (I am no expert) but can not even see them firing.
I tried to edit the code in sandbox by just removing white space in case thats why it wasn't working and now it is isn't working in the sandbox either.
Even when I put the original code back.Can any help?
trigger cloneElecAMsignOff on Electricity_Meter__c (before update) {
List<Electricity_Meter__c> myList = new List<Electricity_Meter__c>();
for(Electricity_Meter__c a: trigger.new) {
// insert Elec meter if Deal Done
if (
a.Account_Manager_Sign_Off_Date_Time__c != Null &&
a.Clone_Date_Time_Stamp__c == Null &&
(
a.Deal_Done_Not__c == 'Deal Done (Existing client / Existing meter)' ||
a.Deal_Done_Not__c == 'Deal Done (New Meter, New Client)' ||
a.Deal_Done_Not__c == 'Deal Done (New Meter, Exisiting Client)')) {
Electricity_Meter__c b = new Electricity_Meter__c();
b.Name = a.Name;
b.Site_name__c = a.Site_name__c;
b.Contract_arranged_under_the_name_of__c = a.Winning_Contract_Arranged_Under_Name_of__c;
b.Account__c = a.Account__c;
b.Linked_Basket__c = a.Linked_Basket__c;
b.Linked_Monitor_Line__c = a.Linked_Monitor_Line__c;
b.Current_Supplier__c = a.Winning_Supplier__c;
b.Previous_Supplier__c = a.Current_Supplier__c;
b.Meter_Status__c = '(2) Active (Not yet live)';
b.COT_Disc_Deenerg_Date__c = a.COT_Disc_Deenerg_Date__c;
b.Auto_Manual_Term__c = a.Auto_Manual_Term__c;
b.Linked_Affiliate_1__c = a.Linked_Affiliate_1__c;
b.Aff_1_Profit_Split__c = a.Aff_1_Profit_Split__c;
b.Linked_Affiliate_2__c = a.Linked_Affiliate_2__c;
b.Aff_2_Profit_Split__c = a.Aff_2_Profit_Split__c;
b.Linked_Affiliate_3__c = a.Linked_Affiliate_3__c;
b.Aff_3_Profit_Split__c = a.Aff_3_Profit_Split__c;
b.Site_Address_New__c = a.Site_Address_New__c;
b.Site_Postcode__c = a.Site_Postcode__c;
b.Co_Reg_FULL_Partnership_LLP_Sole_Trader__c = a.Co_Reg_FULL_Partnership_LLP_Sole_Trader__c;
b.Billing_Address_New__c = a.Billing_Address_New__c;
b.Billing_Postcode__c = a.Billing_Postcode__c;
b.Number_of_employees__c = a.Number_of_employees__c;
b.Annual_turnover_Euros__c = a.Annual_turnover_Euros__c;
b.Profile_Class__c = a.Profile_Class__c;
b.MTC_LLF__c = a.MTC_LLF__c;
b.Meter_Details__c = a.Meter_Details__c;
b.MOP_CED__c = a.MOP_CED__c;
b.Voltage_HV_LV_LVS__c = a.Voltage_HV_LV_LVS__c;
b.Contract_start_date__c = a.Winning_Contract_Start_Date__c;
b.Contract_expiry__c = a.Winning_Contract_End_Date__c;
b.Payment_Method__c = a.Payment_Method__c;
b.DD_Type__c = a.DD_Type__c;
b.VAT__c = a.VAT__c;
b.Monthly_Quarterly_Billing__c = a.Monthly_Quarterly_Billing__c;
b.Copy_of_the_bills__c = a.Copy_of_the_bills__c;
b.AQ__c = a.Winning_Supply_AQ__c;
b.Total_KVA__c = a.Total_KVA__c;
b.Commission_p__c = a.Winning_Commision_p_kwh__c;
b.Commission_per_year__c = a.Winning_Commission_year__c;
b.Commission_Percent__c = a.Winning_Commission_Contract__c;
myList.add(b);
}
// insert Elec meter if Deal Not Done
else if (a.Account_Manager_Sign_Off_Date_Time__c != Null &&
a.Clone_Date_Time_Stamp__c == Null &&
(
a.Deal_Done_Not__c == 'Deal not done / Not responding - Client retained' ||
a.Deal_Done_Not__c == 'Deal not done / Not responding - Client lost' ||
a.Deal_Done_Not__c == 'Deal not done / Went direct with supplier - Client retained' ||
a.Deal_Done_Not__c == 'Deal not done / Went direct with supplier - Client lost' ||
a.Deal_Done_Not__c == 'Deal not done / Went with another consultant - Client retained' ||
a.Deal_Done_Not__c == 'Deal not done / Went with another consultant - Client lost' ||
a.Deal_Done_Not__c == 'Deal not done / Meter removed - Client retained' ||
a.Deal_Done_Not__c == 'Deal not done / Meter removed - Client lost')) {
Electricity_Meter__c c = new Electricity_Meter__c();
c.Name = a.Name;
c.Site_name__c = a.Site_name__c;
c.Contract_arranged_under_the_name_of__c = a.Winning_Contract_Arranged_Under_Name_of__c;
c.Account__c = a.Account__c;
c.Linked_Basket__c = a.Linked_Basket__c;
c.Linked_Monitor_Line__c = a.Linked_Monitor_Line__c;
c.Current_Supplier__c = a.Winning_Supplier__c;
c.Previous_Supplier__c = a.Current_Supplier__c;
c.Meter_Status__c = '(5) Active';
c.COT_Disc_Deenerg_Date__c = a.COT_Disc_Deenerg_Date__c;
c.Auto_Manual_Term__c = a.Auto_Manual_Term__c;
c.Linked_Affiliate_1__c = a.Linked_Affiliate_1__c;
c.Aff_1_Profit_Split__c = a.Aff_1_Profit_Split__c;
c.Linked_Affiliate_2__c = a.Linked_Affiliate_2__c;
c.Aff_2_Profit_Split__c = a.Aff_2_Profit_Split__c;
c.Linked_Affiliate_3__c = a.Linked_Affiliate_3__c;
c.Aff_3_Profit_Split__c = a.Aff_3_Profit_Split__c;
c.Site_Address_New__c = a.Site_Address_New__c;
c.Site_Postcode__c = a.Site_Postcode__c;
c.Co_Reg_FULL_Partnership_LLP_Sole_Trader__c = a.Co_Reg_FULL_Partnership_LLP_Sole_Trader__c;
c.Billing_Address_New__c = a.Billing_Address_New__c;
c.Billing_Postcode__c = a.Billing_Postcode__c;
c.Number_of_employees__c = a.Number_of_employees__c;
c.Annual_turnover_Euros__c = a.Annual_turnover_Euros__c;
c.Profile_Class__c = a.Profile_Class__c;
c.MTC_LLF__c = a.MTC_LLF__c;
c.Meter_Details__c = a.Meter_Details__c;
c.MOP_CED__c = a.MOP_CED__c;
c.Voltage_HV_LV_LVS__c = a.Voltage_HV_LV_LVS__c;
c.Contract_start_date__c = a.Winning_Contract_Start_Date__c;
c.Contract_expiry__c = a.Winning_Contract_End_Date__c;
c.Payment_Method__c = a.Payment_Method__c;
c.DD_Type__c = a.DD_Type__c;
c.VAT__c = a.VAT__c;
c.Monthly_Quarterly_Billing__c = a.Monthly_Quarterly_Billing__c;
c.Copy_of_the_bills__c = a.Copy_of_the_bills__c;
c.AQ__c = a.Winning_Supply_AQ__c;
c.Total_KVA__c = a.Total_KVA__c;
c.Commission_p__c = a.Winning_Commision_p_kwh__c;
c.Commission_per_year__c = a.Winning_Commission_year__c;
c.Commission_Percent__c = a.Winning_Commission_Contract__c;
myList.add(c);
}
}
}
I think, based on certain conditions you are only creating more Electricity_Meter__c records (the goal here).
However, if we see the code, the new object instances you're creating inside the conditional loops (the if statements), you are simply adding them to a list : Refer
myList.add(b); and myList.add(c);
After that, nothing is done on myList. Maybe you'll want to insert them?
if(myList.size() > 0){
insert myList;
}
All Answers
Hi Alan,
Make sure the triger / triggers are active. This can be done in two ways 1) look in to .meta file if you are using Eclipse 2) under setup > Develop> APEX Triggers -find the trigger and make sure the status is set to active.
Hi Martin,
Thanks for the reply. They are both set to active. Really odd.
Try running the test class for the respective trigger(s) again and let us know if that helps.
Regards,
Lakshman
Alan
You trigger is running on Update event ,also you are checking some condition.Pleae make sure you fulfill it to invoke the Trigger.
Hi Alan,
I was also thinking along the same lines as Vinit. How do you know it's not firing? It that you don't see the expected result or can't you see the entry / exit statements in the debug logs? If the former you could add some
I hope this takes you in the right direction?
I think, based on certain conditions you are only creating more Electricity_Meter__c records (the goal here).
However, if we see the code, the new object instances you're creating inside the conditional loops (the if statements), you are simply adding them to a list : Refer
myList.add(b); and myList.add(c);
After that, nothing is done on myList. Maybe you'll want to insert them?
if(myList.size() > 0){
insert myList;
}
Hey guys,
Thanks for all the replies and suggestions really appreciate. So I have been trying different scenerios and getting intermittent results.
1. Went back into my Sandbox and simplified the trigger for object electricity_meter__c.I took and all if statements and removed one of the 2 record inserts. Ok so this worked the first time. And produced a record on update of a record as expected.
2. I then put just one of the field if conditions back in and it stoppped working again. Obviously making sure that this condition was met. @Vishal I tried adding if(myList.size() > 0){insert myList;} and that didn't work either. I still need to try adding both lists as you suggested myList.add(b); and myList.add(c);
3. I then took it out again to revert back to the trigger with no if conditions at all. Now this is not working even without any if conditions.
4. I then tried moving to the other trigger object gasmeter__c to try and simplify the trigger. This didn't work without and if conditions.
5. I then turn on the debug logs and see if the trigger is being initialised. And yes I can now see the trigger. Debug snippet below
6. Now I decide to move to production and see what kind of results I am getting there on object electricity_meter__c. I turn on the debug logs. The trigger is once again being initialised but not producing the results. Debug log snippet below.
7. @Lakshman - In production: I ran the trigger class and tried the trigger again. And nothing still. (Did also try this before)
8. @Martin I did not attempt the system.debug statements as of yet. Are you able to suggest if I should put this in the trigger or test class, Thanks?
9. @Vinit - I have checked that I am trying to get the trigger to fire on update of a record and also the if conditions. As you will see from my steps above I have tried to strip the trigger down to no if conditions at all and not working.
Can you post the latest trigger code please? I'd like to have a look. Also let me know what are you doing to test it?
Hey,
it is pefectly safe to add the debug statements to both the trigger and the test case -- it will not interfere with anything in production either. I usually keep a couple of debug statments in my triggers and classes even after they work as expected. If you run into issues later on it is good to have them there when you enable debug logs.
Looking at the log you pasted it looks like the triggers are executed and they are doing something. The lines
tells me that there is an object added to the list. But as mentioned before in the thread -- do you do anything with this list later in the trigger. However, your a object changes should still exist since they are acting on the current trigger object and it is a before update.
Hi Vishal,
I have added
the latest code with all if conditions commented out looks like:
I am testing it in multiple ways. Going via list views, custom VF page and on individual records. None are working.
Hi Martin,
I checked to see if the records have been inserted without a relationship and just floating about but nothing.
I will look more into system.debug as new to this.
Thanks for the tips.
I have also tried to copy the trigger code out of this trigger and put it into a new trigger. Deactivating the old trigger.
This is mind boggling
I had my insert statement inside of a try and catch block. Because of time and lack of knowledge I wasn't able to test against this. Which in turn made my trigger 98% instead of 100%. I commented the try catch block out! and this obviously had my insert statement. Thanks for looking at this. Guilty of overlooking the basics!
For the interest of knowledge share: The final trigger looks like...