Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I run a test in developer I get 77% apparently. This is the entire apex trigger and the only thing that is being attempted to be deployed in the org:

trigger Requestor on Case (before insert) {
    // user defaultuser = [select id from user where name = 'default user'];
    for (Requestor__c record:trigger.new) {
         if(record.Product_Owner__c ==null) {
             record.Product_Owner__c = userinfo.getUserId();
        }
    }
}

Here are the 4 Apex Test Failures:

LeadTriggerTesttestMcSubscriberSyncDateOnUpdateSystem.AssertException: Assertion Failed: Expected: null, Actual: 2016-11-16 14:31:03 Stack Trace: Class.MC4SF.LeadTriggerTest.testMcSubscriberSyncDateOnUpdate: line 101, column 1

MappingsControllerTesttestMappingsControllerSystem.QueryException: List has no rows for assignment to SObject Stack Trace: Class.MC4SF.MappingsControllerTest.testMappingsController: line 28, column 1

MembershipControllerTesttestContactMembershipSystem.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, You're creating a duplicate record. We recommend you use an existing record instead.: [] Stack Trace: Class.MC4SF.MembershipControllerTest.testContactMembership: line 13, column 1

UserTriggerTesttestDeactivatingHourlyJobOwnerSystem.AsyncException: The Apex job named "MailChimp Hourly Sync" is already scheduled for execution. Stack Trace: Class.MC4SF.UserTriggerTest.testDeactivatingHourlyJobOwner: line 43, column 1

How do I make sure I am at over 75% coverage, also can the code be left as is, or does it have to be more complex to be approved for our organization? We don't have any other triggers running so I am not sure why this wouldn't just work.

share|improve this question

put on hold as off-topic by Eric, Brian Mansfield, Boris Bachovski, Lance Shi, Himanshu Nov 17 at 5:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Eric, Brian Mansfield, Boris Bachovski, Lance Shi, Himanshu
If this question can be reworded to fit the rules in the help center, please edit the question.

2  
We probably wont be able to help you much without also seeing the test class, but it looks like theres a number of issues with the test class. You should try the Testing Apex Triggers Trailhead if you haven't. – battery.cord Nov 16 at 16:46
    
Thank you for replying. I'm not sure what you mean by seeing the test class. I've tested this trigger in the sandbox and it functions exactly how I wanted it to and gets a 77% code coverage. But the status at the end says 'failed' (little red x). – Artem Skobrev Nov 16 at 17:04
    
How did you test it without writing a test class for it? – battery.cord Nov 16 at 17:17
    
I just saved the code as an Apex Trigger, saved that in developer and the trigger is working flawlessly in the sandbox. Testing wise, I just looked at what was happening under the Test tab (the one next to Logs). That automatically runs and it's showing up as Failed, but overall code coverage is showing as 77%. Are there additional steps that I need to take to make sure that this deploys? Not sure why this is proving to be so difficult for such a simple trigger. – Artem Skobrev Nov 16 at 19:25
    
@ArtemSkobrev - Read the below answers. You are not going to be able to deploy unless you write your own test class for this trigger or fix the existing test classes. Strongly advised you write a test class specific for this trigger whatever it is because it is not what you posted as that cannot be compiled – Eric Nov 16 at 21:49

You issue is with other tests in your org and you will need to get them resolved at some point.

For, now, when deploying, select "Run Specified Tests" and enter the name of the test class that tests your trigger.

Then when you deploy only that test class will be ran and only coverage for the trigger will be considered

As pointed out in other answer, there is no way you have saved the trigger posted in your question and written a test for it as the trigger will not even compile. Maybe a typo in your question?

enter image description here

share|improve this answer

There are a number of things going wrong here.

  1. Without deploying anything, run all tests in your Production Org and determine that there are no failing tests. If you have tests that are failing in Production you should fix those first. Otherwise you will have a lot of trouble deploying anything.
  2. If you don't have tests failing in production, somehow this trigger seems to be causing tests to fail.
  3. This code that you provided us, does it even compile? It looks like a trigger on the Case object, but in your for loop you are using Requestor__c as your Trigger.new object?
  4. This is a very simple trigger, and could be done using Process Builder, or maybe even Workflow.
  5. You did not actually test your trigger. By running all tests and determining that you have greater than 75% code coverage without writing tests for your code you are being lazy and making more work for whomever comes after you. Furthermore You have not determined definitively that your code actually works. If you are going to keep this as a trigger you should write a Test Class that actually tests the logic of your trigger to determine if it correctly assigned the User Id to the Product_Owner__c field.
share|improve this answer
    
You are making a lot of assumptions on the OP test method when they have not posted it.....Checking to see if the org is > 75% is not lazy, writing tests that only do coverage however, well.........+1 on the WFR though – Eric Nov 16 at 21:13
    
@Eric OP's comment "Testing wise, I just looked at what was happening under the Test tab (the one next to Logs)." very much implies that no test class was written. But that is my opinion, nothing more. – dBeltowski Nov 16 at 21:45
    
Good catch on the reading into the question. It does look like the OP is trying to deploy something (no idea what since code does not compile) without a test........and the coverage is purely organic – Eric Nov 16 at 21:48
    
This is the functionality I'm looking for: When a new Case is created, the Requester custom field is populated with the creators username. That's it. I looked online for the easiest way and this Apex Trigger seems to do the trick in my Sandbox, but I can't deploy it. – Artem Skobrev Nov 16 at 21:57
    
@ArtemSkobrev - Easiest way it a WFR. Besides you say you wrote a trigger and are trying to deploy it. That cannot be true with what you posted. So are you just saying you wrote it to try and get someone to actually write it for you?? – Eric Nov 16 at 22:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.