0

I have created a trigger that on clicking the Sync Quote button it will update Opportunity Products with custom fields our organization needs to put into the accounting system

trigger UpdateOpportunityProductsFromOpportunity on Opportunity (after update) {
List<Quote> quoteList = new List<Quote>();
String primaryQuote = '';

for(Opportunity o : Trigger.new){
    primaryQuote = ( o.SyncedQuoteId==null ? '' : o.SyncedQuoteId );

    quoteList = [ Select Id From Quote Where OpportunityId = :o.Id ];

    for(Quote q : quoteList){q.IsPrimary__c = false;update q;}


    if(primaryQuote.length()==18){
        quoteList = [ Select Id From Quote Where Id = :primaryQuote ];

        for(Quote q : quoteList){q.IsPrimary__c = true; update q;}


    }
}

This is 1 of 2 triggers I need to deploy.

Since our organization's code coverage is not at 75% I need to get it to a point in code coverage to deploy.

I created this test class

@isTest
private class UpdateOPFromOpportunityTest{

  static testmethod void primaryQuote
    {
        // Obtain Apex governor limits and resources for this test
        Test.startTest();

        for(Opportunity o : Trigger.new){primaryQuote = ( o.SyncedQuoteId=='0Q01M0000014FzASAU' ? '' : o.SyncedQuoteId );

        quoteList = [ Select Id From Quote Where OpportunityId = '0061M00000xVYh2QAG'];

        for(Quote q : quoteList){q.IsPrimary__c = false;update q;}



        // Release governor limits and resources
        Test.stopTest();
    }

Two lines with errors I am getting this morning are puzzling me

On Line 7 it is erroring with

Unexpected Token & Invalid Constructor for Test.startTest

One Line 9 Got

Expecting } got for

0

You are missing empty parentheses in your method declaration:

  static testmethod void primaryQuote

This kind of syntax error often produces unexpected errors just below it.

Note also that the @isTest keyword replaced testmethod, and should be used in new code.

Your unit test still won't work because it hard-codes Ids that it cannot see in unit test context, where data isolation is enforced, and because it references Trigger.new, which you cannot access outside a trigger. It would not cause the trigger to execute in test context because it performs no DML on Opportunity, and therefore won't cover the code.

Your unit test should generate 100% of the data it requires in code. It should synthesize that data and perform DML operations to cause your trigger to execute, exercise its logic, and then make assertions to validate the results. See How do I write an Apex unit test? for more and for resource links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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