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
GorkyGorky 

Apex Trigger Test Coverage

Hi Everyone,


I'm trying to deploy my first Apex trigger to PRODUCTION but not pases the validation I only made the 69% of 75%.

I don't know what I'm missing.

First I check the user who's launched the trigger. Then if is not a administrator I call the method MyMethod in the class ThisClass, and I pass the values of the Orders table who has been changed.

The class is tested and is correctly deployed.

Thanks in advance.

The code --> :

trigger Order_UPDATE on Orders (before update) {

    string userrole = UserInfo.getUserRoleId(); --> Get UserRoleId
   
    if (userrole != 'Administrator'){   --> If is not administrator enter the IF
       
        Orders[] accs = Trigger.new; --> Get the values

        This_Class.MyMethod(accs);  --> Pass it to the method
   
    }

}

David VPDavid VP
You need to run your trigger and test that too ... I have the impression that you're just testing the class that is used by the trigger.

In your test class you would need to have something like :

Code:
Orders o  = new Orders();
o.xx = ... //set the required field

insert o; //save it to SFDC (it will rollback after your test

o.xx = ... // change a value

update o; //THIS WILL FIRE YOUR UPDATE TRIGGER, and make sure it's included in the test

 

Hope this helps,


David


Message Edited by David VP on 10-09-2008 05:10 PM
GorkyGorky
Yes, it works.

Thanks again David VP

Fra08Fra08
Hello,
I modified an apex class and I am trying to save it in Salesforce.
The strange thing is that the "deployment result" is failed and the errors are referring to triggers already existing in Salesforce! The message says: "test coverage of selected apex trigger is 0%".
How can this be possible?
justinpauldavis10justinpauldavis10

I have a similar question that I'm hoping someone will help me with, I'm new to Apex.


I have a very simple lookup-field trigger that places a record ID in that field, on a custom "Accounts" object, not the standard object:


trigger SetAccountField on Account__c (before insert, before update) {
    for (Account__c Account__c : Trigger.new) {
        a.Scoring__c = 'a00A0000003MnLw';
    }
 }



How can I create a test class, for something like this? Can you give me an example of the best way to test something this simple?