+ Start a Discussion
Febrian Tarigan 9Febrian Tarigan 9 

My trigger not working in production but fine in sandbox

i have an issue , i create trigger to create child record under opportunity object, it work fine in sandbox but not working in production

i try to see debug logs of my trigger but my trigger not detected in log my trigger is already active and working fine in sandbox

can somebody help me ?

here's my code
trigger paymentSyncCreateAndDelete on Payment_Term__c (before update) { String opptyId; Date achieveDate;

for(Payment_Term__c sourcePayment : Trigger.new){            

    opptyId = sourcePayment.Opportunity__c;
    achieveDate = sourcePayment.Achievement_Payment_Date__c;

     //When Paid change from false to true, CREATE payment on target
    if(sourcePayment.Paid__c ==true){

        //get oppty payment information
        Opportunity sourcePaymentOppty = [SELECT ID, Sales_Segment__c, Target_Owner_AM__c FROM Opportunity WHERE Id=:sourcePayment.Opportunity__c];

        //search oppty target sales segment with same year and month
        Opportunity[] targetPaymentOppty = [SELECT Id,RecordTypeId, Achievement_Date__c, Target_Owner_NB__c, Target_Owner_AM__c,
                                          Sales_Segment__c, Other_Sales_Segment__c, Product_Name__c FROM Opportunity WHERE 
                                          RecordType.Name='Sales Segment Target' AND FISCAL_MONTH(Achievement_Date__c)=:achieveDate.month()
                                          AND FISCAL_YEAR(Achievement_Date__c)=:achieveDate.year() AND 
                                          Sales_Segment__c=:sourcePaymentOppty.Sales_Segment__c];

        //search oppty target individual with same year and month                                  
        Opportunity[] targetAMPaymentOppty = [SELECT Id,RecordTypeId, Achievement_Date__c, Target_Owner_NB__c, Target_Owner_AM__c,
                                          Sales_Segment__c, Other_Sales_Segment__c, Product_Name__c FROM Opportunity WHERE 
                                          RecordType.Name='Individual Target' AND FISCAL_MONTH(Achievement_Date__c)=:achieveDate.month()
                                          AND FISCAL_YEAR(Achievement_Date__c)=:achieveDate.year() AND 
                                          Target_Owner_AM__c=:sourcePaymentOppty.Target_Owner_AM__c];


        if(targetPaymentOppty.size()>0 && sourcePayment.createdToTarget__c==false){
            Payment_Term__c targetPayment = new Payment_Term__c(Term__c = sourcePayment.Term__c,Opportunity__c = targetPaymentOppty[0].Id, Conversion_Rate_USD__c = sourcePayment.Conversion_Rate_USD__c, Actual_Payment_Date__c= sourcePayment.Actual_Payment_Date__c, Actual_Payment_Amount_USD__c= sourcePayment.Actual_Payment_Amount_USD__c, Actual_Payment_Amount_IDR__c= sourcePayment.Actual_Payment_Amount_IDR__c, Plan_Payment_Amount_IDR__c= sourcePayment.Plan_Payment_Amount_IDR__c,  Plan_Payment_Amount_USD__c= sourcePayment.Plan_Payment_Amount_USD__c,  Plan_Payment_Date__c= sourcePayment.Plan_Payment_Date__c, Debit_Note_Created__c = true,  Debit_Note_Number__c = sourcePayment.Debit_Note_Number__c,  Debit_Note_Submitted__c = sourcePayment.Debit_Note_Submitted__c,  sourcePayment__c= sourcePayment.Id,Paid__c = true);

            insert targetPayment;   

            if(targetPayment.Id!=null)
            sourcePayment.createdToTarget__c=true;            
        }

        if(targetAMPaymentOppty.size()>0 && sourcePayment.createdToTargetAM__c==false){

            //Payment_Term__c targetPayment = new Payment_Term__c();
            Payment_Term__c targetPayment = new Payment_Term__c(Term__c = sourcePayment.Term__c,Opportunity__c = targetAMPaymentOppty[0].Id,Conversion_Rate_USD__c = sourcePayment.Conversion_Rate_USD__c, Actual_Payment_Date__c= sourcePayment.Actual_Payment_Date__c, Actual_Payment_Amount_USD__c= sourcePayment.Actual_Payment_Amount_USD__c, Actual_Payment_Amount_IDR__c= sourcePayment.Actual_Payment_Amount_IDR__c, Plan_Payment_Amount_IDR__c= sourcePayment.Plan_Payment_Amount_IDR__c, Plan_Payment_Amount_USD__c= sourcePayment.Plan_Payment_Amount_USD__c, Plan_Payment_Date__c= sourcePayment.Plan_Payment_Date__c, Debit_Note_Created__c = true, Debit_Note_Number__c = sourcePayment.Debit_Note_Number__c, sourcePayment__c= sourcePayment.Id, Paid__c = true,Debit_Note_Submitted__c = sourcePayment.Debit_Note_Submitted__c);

            insert targetPayment;  

            if(targetPayment.Id!=null)
            sourcePayment.createdToTargetAM__c=true;             
        }
    }
    //When Paid change from true to false, DELETE payment on target
    if(Trigger.oldMap.get(sourcePayment.Id).Paid__c==true && sourcePayment.Paid__c ==false){

        //search same Payment on target sales segment
        Payment_Term__c[] targetPayment = [SELECT Id FROM Payment_Term__c WHERE sourcePayment__c=:sourcePayment.Id AND Opportunity__r.RecordType.Name='Sales Segment Target'];


        if(targetPayment.size()>0){
            delete targetPayment[0];
            sourcePayment.createdToTarget__c = false;
        }
        //search same Payment on target sales segment
        Payment_Term__c [] targetPaymentAM = [SELECT Id FROM Payment_Term__c WHERE sourcePayment__c=:sourcePayment.Id  AND Opportunity__r.RecordType.Name='Individual Target'];


        if(targetPaymentAM.size()>0){
            delete targetPaymentAM[0];
            sourcePayment.createdToTargetAM__c = false;
        }
    }
}
}

my test class
@isTest public class testPaymentUpdate{ static testMethod void testPayment() {

   Id recordTypeId = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' AND Name = 'Donation' LIMIT 1].id;
   Id ITrecordTypeId = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' AND Name = 'Individual Target' LIMIT 1].id;
   Id SSTrecordTypeId = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' AND Name = 'Sales Segment Target' LIMIT 1].id;
   //CREATE RECORD FROM SCRATCH

   //create account
   Account acc = new Account();
   acc.Name = 'test acc';
   acc.Sales_Segment__c = 'Corporate';
   insert acc;

   //create opportunity donation
   Opportunity opptySource = new Opportunity();
   opptySource.AccountId=acc.Id;
   opptySource.Name ='test oppty';
   opptySource.CloseDate = Date.newInstance(2015, 9, 17);
   opptySource.StageName ='6 - MoU in Process';
   opptySource.Target_Owner_AM__c=UserInfo.getUserId();
   opptySource.RecordTypeId= recordTypeId;
   insert opptySource;

   //create opportunity individual target
   Opportunity opptyIndividualTarget = new Opportunity();
   opptyIndividualTarget.Name ='test oppty';
   opptyIndividualTarget.CloseDate = Date.newInstance(2015, 9, 1);
   opptyIndividualTarget.Target_Owner_AM__c=UserInfo.getUserId();
   opptyIndividualTarget.Amount_AM__c=50000;
   opptyIndividualTarget.StageName ='Target'; 
   opptyIndividualTarget.RecordTypeId= ITrecordTypeId;
   insert opptyIndividualTarget;

   //create opportunity sales segment target
   Opportunity opptySalesSegmentTarget= new Opportunity();
   opptySalesSegmentTarget.Name ='test oppty';
   opptySalesSegmentTarget.CloseDate = Date.newInstance(2015, 9, 1);
   opptySalesSegmentTarget.Sales_Segment__c = 'Corporate';
   opptySalesSegmentTarget.Amount_AM__c=50000;
   opptySalesSegmentTarget.StageName ='Target'; 
   opptySalesSegmentTarget.RecordTypeId = SSTrecordTypeId;
   insert opptySalesSegmentTarget;

   //create source Payment
   Payment_Term__c sourcePayment = new Payment_Term__c();
   sourcePayment.Plan_Payment_Amount_IDR__c = 1000000;
   sourcePayment.Plan_Payment_Date__c = Date.newInstance(2015, 9, 17);
   sourcePayment.Opportunity__c = opptySource.Id;
   sourcePayment.Term__c = '1';
   insert sourcePayment;

   sourcePayment.Actual_Payment_Amount_IDR__c = 1000000;
   sourcePayment.Actual_Payment_Date__c = Date.newInstance(2015, 9, 17);
   sourcePayment.Debit_Note_Created__c = true;
   sourcePayment.Debit_Note_Number__c = '123810283';
   sourcePayment.Debit_Note_Submitted__c = Date.newInstance(2015, 9, 17);
   sourcePayment.Paid__c = true;
   sourcePayment.createdToTarget__c=true;
   sourcePayment.createdToTargetAM__c=true;
   update sourcePayment;

   Payment_Term__c targetPayment = new Payment_Term__c();
   targetPayment.Opportunity__c = opptySalesSegmentTarget.Id;
   targetPayment.sourcePayment__c = sourcePayment.Id;
   targetPayment.Term__c = sourcePayment.Term__c;
   targetPayment.Plan_Payment_Amount_IDR__c = sourcePayment.Plan_Payment_Amount_IDR__c;
   targetPayment.Plan_Payment_Date__c = sourcePayment.Plan_Payment_Date__c;
   targetPayment.Actual_Payment_Amount_IDR__c = sourcePayment.Actual_Payment_Amount_IDR__c;
   targetPayment.Actual_Payment_Date__c = sourcePayment.Actual_Payment_Date__c;
   targetPayment.Debit_Note_Created__c = true;
   targetPayment.Debit_Note_Number__c = sourcePayment.Debit_Note_Number__c;
   targetPayment.Debit_Note_Submitted__c = sourcePayment.Debit_Note_Submitted__c;
   targetPayment.Paid__c = true;
   insert targetPayment;

   Payment_Term__c targetPaymentAM = new Payment_Term__c();
   targetPaymentAM.Opportunity__c = opptyIndividualTarget.Id;
   targetPaymentAM.sourcePayment__c = sourcePayment.Id;
   targetPaymentAM.Term__c = sourcePayment.Term__c;
   targetPaymentAM.Plan_Payment_Amount_IDR__c = sourcePayment.Plan_Payment_Amount_IDR__c;
   targetPaymentAM.Plan_Payment_Date__c = sourcePayment.Plan_Payment_Date__c ;
   targetPaymentAM.Actual_Payment_Amount_IDR__c = sourcePayment.Actual_Payment_Amount_IDR__c;
   targetPaymentAM.Actual_Payment_Date__c = sourcePayment.Actual_Payment_Date__c;
   targetPaymentAM.Debit_Note_Created__c = true;
   targetPaymentAM.Debit_Note_Number__c = sourcePayment.Debit_Note_Number__c;
   targetPaymentAM.Debit_Note_Submitted__c = sourcePayment.Debit_Note_Submitted__c;
   targetPaymentAM.Paid__c = true;
   insert targetPaymentAM;

   sourcePayment.Actual_Payment_Amount_IDR__c = null;
   sourcePayment.Actual_Payment_Date__c = null;
   sourcePayment.Debit_Note_Created__c = false;
   sourcePayment.Debit_Note_Number__c = '';
   sourcePayment.Debit_Note_Submitted__c = null;
   sourcePayment.Paid__c=false;         
   update sourcePayment;
}
}

 
ManojjenaManojjena
Hi Fabrian,

Check once in production,your trigger is active or not  if active then filter your debug log .Go to the debug log and and click on the filter link which is available in teh Monitor User Section .You will find three links Delete | Reset | Filters  click on filters and make all none except Apex and System .

Then update the record as your event is before update .

Let me know if it helps .

Thanks
Manoj