function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sihmeieossihmeieos 

Don't Call Trigger from TestMethod

Hello.

I want to create ApexTrigger(before insert) calls Apex class.

But I try the code run normally in developer organization, don't run in operational organization.

------------------------------------------------------------------------------------

[Trigger]

trigger instCode on CustomObjectA (before insert) {
    CustomObjectA[] a = Trigger.new;
    Class01.addData(a);
}

 

[Class]

 public class Class01 {
    public static void addData(CustomObjectA[] a){
        if(wd.size()>0){
                for(CustomObjectA wdc:wd){
                    CustomObjectB b = [select RunningHour__c from CustomObjectB where Id= :wdc.SerialNum__c];
                    If(a.RunningHour__c > b.RunningHour__c){
                        b.RunningHour__c = a.RunningHour__c;
                        update a;
        }

    //testMethod
    public static testMethod void testAddData(){
        CustomObjectB b = [select Id,Name,RecordTypeId,RunningHour__c from CustomObjectB where Name='10785'];
        b.RunningHour__c = 120.0;
        update b;
        //CustomObjectA>CustomObjectB

       CustomObjectA a = new CustomObjectA(WorkNum__c='a0910000005zwM2AAI',SerialNum__c='a0810000002ZEtGAAW', RunningHour__c=125.5);
        insert a1; //call trigger
        CustomObjectB b01 = [select Id,Name,RunningHour__c from CustomObjectB where Id= :a1.Id];
        System.assertNotEquals(b01.RunningHour__c,a1.RunningHour__c);
        //CustomObjectA<CustomObjectB

       CustomObjectA a2 = new CustomObjectA(WorkNum__c='a0910000005zwM2AAI',SerialNum__c='a0810000002ZEtGAAW',RunningHour__c=119);
        insert a2; //call trigger
        CustomObjectB b02 = [select Id,Name,RunningHour__c from CustomObjectB where Id= :a2.Id];
        System.assertNotEquals(b02.RunningHour__c,a2.RunningHour__c);
    }

}

------------------------------------------------------------------------------------

Testmethod:testAddData run normally,but don't call Trigger: instCode apparently.

Why don't call Trigger.

Best Answer chosen by Admin (Salesforce Developers) 
InsertWittyNameInsertWittyName

With regard to the trigger not being fired, please confirm the following:

 

1. Log into the production org and ensure the trigger is actually there.

2. Ensure that the trigger is marked as active.

All Answers

InsertWittyNameInsertWittyName

I'm assuming you've pasted some 'hacked up' version of the original code, because of the glaringly obvious problems such as this one:

 

 

CustomObjectA a = new CustomObjectA(WorkNum__c='a0910000005zwM2AAI',SerialNum__c='a0810000002ZEtGAAW', RunningHour__c=125.5); insert a1; //call trigger

 

 a1 doesn't exist in your code...

 

Paste your full, unmodified code if you want help.

 

sihmeieossihmeieos

Hi
InsertWittyName
, Thank you for reply and so sorry.You're exactly right.

 

I post original code.

 

[Trigger]

 

trigger instRunningHour on WorkDetail__c (before insert) {
WorkDetail__c[] ed = Trigger.new;
putRunningHour.addRunningHour(ed);
}

 

[Class]

 

public class putRunningHour {
public static void addRunningHour(WorkDetail__c[] wd){
try{
if(wd.size()>0){
for(WorkDetail__c wdc:wd){
ProductNS__c pns = [select RunningHour__c from ProductNS__c where Id= :wdc.SerialNum__c];
If(wdc.RunningHour__c > pns.RunningHour__c){
pns.RunningHour__c = wdc.RunningHour__c;
update pns;
}else{
}
}
}else{
}
}catch( QueryException e ) {
System.debug( e.getMessage() );
}
}
//testMethod
public static testMethod void testAddRunningHour(){
try{
ProductNS__c pnc = [select Id,Name,RecordTypeId,RunningHour__c from ProductNS__c where Name='10785'];
pnc.RunningHour__c = 120.0;
update pnc;
//WorkDetail>ProductNS
WorkDetail__c wdc = new WorkDetail__c(WorkNum__c='a0910000005zwM2AAI',SerialNum__c='a0810000002ZEtGAAW', RunningHour__c=125.5);
insert wdc;
ProductNS__c pnctest = [select Id,Name,RunningHour__c from ProductNS__c where Id= :pnc.Id];
System.assertNotEquals(pnctest.RunningHour__c,pnc.RunningHour__c);
//WorkDetail<ProductNS
WorkDetail__c wdc2 = new WorkDetail__c(WorkNum__c='a0910000005zwM2AAI',SerialNum__c='a0810000002ZEtGAAW',RunningHour__c=119);
insert wdc2;
ProductNS__c pnctest2 = [select Id,Name,RunningHour__c from ProductNS__c where Id= :pnc.Id];
System.assertNotEquals(pnctest2.RunningHour__c,pnc.RunningHour__c);
}catch( QueryException e ) {
System.debug( e.getMessage() );
}
}
}

 

 Please tell me, why TestMehod don't call Trigger before insert.

Thanks.

 

 

InsertWittyNameInsertWittyName
Run the test method on the org and copy & paste the debug output here.
sihmeieossihmeieos

Hi, .

This is Debug Log.(Log Level:Finest)

 

 

 


Debug Log:

*** Beginning Test 1: putRunningHour.public static testMethod void testAddRunningHour()

20100107094000.896:Class.putRunningHour: line 1, column 8: Static initialization: putRunningHour
20100107094000.896:Class.putRunningHour.testAddRunningHour: line 22, column 13: DeclareVar: SOBJECT:ProductNS__c pnc
20100107094000.896:Class.putRunningHour.testAddRunningHour: line 22, column 32: SOQL query with 1 row finished in 25 ms
20100107094000.896:Class.putRunningHour.testAddRunningHour: line 22, column 13:     initial value: ProductNS__c:{Name=10785, RecordTypeId=01210000000Cuf4AAC, Id=a0810000002ZEtGAAW}
20100107094000.896:Class.putRunningHour.testAddRunningHour: line 23, column 13: SOBJECT:ProductNS__c pnc.RunningHour__c <= Literal
20100107094000.896:Class.putRunningHour.testAddRunningHour: line 24, column 4: Update: SOBJECT:ProductNS__c
20100107094000.896:Class.putRunningHour.testAddRunningHour: line 24, column 4:     DML Operation executed in 28 ms
20100107094000.896:Class.putRunningHour.testAddRunningHour: line 26, column 13: DeclareVar: SOBJECT:WorkDetail__c wdc
20100107094000.896:Class.putRunningHour.testAddRunningHour: line 26, column 13:     initial value: WorkDetail__c:{RunningHour__c=125.5, WorkNum__c=a0910000005zwM2AAI, SerialNum__c=a0810000002ZEtGAAW}
20100107094000.896:Class.putRunningHour.testAddRunningHour: line 27, column 13: Insert: SOBJECT:WorkDetail__c
*** Beginning WorkDetail Validation Rule Evaluation for null
Start Time: 20100107094001.030
Rule Name: CheckAccount_Work_Product
Error Condition Formula: WorkNum__r.Account__r.Id  <>   SerialNum__r.DeliverCustomer__r.Id
Value(s) Found: SerialNum__r.DeliverCustomer__r.Id=001100000074F4M , WorkNum__r.Account__r.Id=001100000074F4M
Result: PASS - Continue
End Time: 20100107094001.031
*** Ending WorkDetail Validation Rule Evaluation for null
20100107094000.896:Class.putRunningHour.testAddRunningHour: line 27, column 13:     DML Operation executed in 104 ms
20100107094000.896:Class.putRunningHour.testAddRunningHour: line 28, column 13: DeclareVar: SOBJECT:ProductNS__c pnctest
20100107094000.896:Class.putRunningHour.testAddRunningHour: line 28, column 36: SOQL query with 1 row finished in 9 ms
20100107094000.896:Class.putRunningHour.testAddRunningHour: line 28, column 13:     initial value: ProductNS__c:{Name=10785, RunningHour__c=120, Id=a0810000002ZEtGAAW}
20100107094000.896:Class.putRunningHour.testAddRunningHour: line 29, column 13: System.assertNotEquals(Decimal, Decimal)
System.Exception: Assertion Failed: Same value: 120

Class.putRunningHour.testAddRunningHour: line 29, column 13
External entry point


Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 2 out of 100
Number of query rows: 2 out of 500
Number of SOSL queries: 0 out of 20
Number of DML statements: 2 out of 100
Number of DML rows: 2 out of 500
Number of script statements: 7 out of 200000
Maximum heap size: 0 out of 1000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 10
Number of System.runAs() invocations: 0 out of 20

Total email recipients queued to be sent : 0
Stack frame variables and sizes:
  Frame0

*** Ending Test putRunningHour.public static testMethod void testAddRunningHour()

Cumulative profiling information:

No profiling information for SOQL operations.

No profiling information for SOSL operations.

No profiling information for DML operations.

No profiling information for method invocations.


If call trigger, RunnningHour is "125.5".

 

Thanks.

dmchengdmcheng
I don't know what's causing your exact problem, but you definitely have another problem in that you have SOQL and DML statements (select and update) inside a for loop.  That will cause you to exceed governor limits.  You need to "bulkify" your code.  See documentation and forums and webcasts for pointers on how to write bulk triggers.
InsertWittyNameInsertWittyName

With regard to the trigger not being fired, please confirm the following:

 

1. Log into the production org and ensure the trigger is actually there.

2. Ensure that the trigger is marked as active.

This was selected as the best answer
sihmeieossihmeieos

Thanks for help.

 

I confirm the trigger,that trigger is active.But the trigger can't contained source code.

I can't understand how use to Force.comIDE in the first place. So, I can't deploy that trigger correctly.

I try to deploy trigger & class, that logic run normally.

And I apologize for my poor knowledge.

 

Thanks you. 

sihmeieossihmeieos

Thanks for you advise.

I need to study more, and I'll find out "bulkify".

 

Thank you.