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
JustAGirlyGeekJustAGirlyGeek 

Help with test coverage for trigger

I was wondering if someone would be able to help me create a test class for the following code as I am programmatically challenged and have very limited developer experience :) any help would be greatly appreciated!!! Thanks in advance!!!

 

trigger OpportunityValidations on Opportunity(before update){

    Set<Id> oppsToBeChecked = new Set<Id>();
    Profile adminId = [SELECT Id from Profile where Name='System Administrator' LIMIT 1];
    
    for(Opportunity o : trigger.new){
        if(o.Probability >= 70 && UserInfo.getProfileId() != adminId.Id){
            oppsToBeChecked.add(o.Id);
        }
    }
    // check their products in greater detail
    if(!oppsToBeChecked.isEmpty()) { // maybe we don't need to run the query at all?   
        for(OpportunityLineItem oli : [SELECT OpportunityId
        FROM OpportunityLineItem
        WHERE OpportunityId IN :oppsToBeChecked
            AND PriceBookEntry.Product2.RecordType.DeveloperName = 'Prospect_Product'
            AND PriceBookEntry.Product2.Approved__c = false]){

        trigger.newMap.get(oli.OpportunityId).addError('Opportunities with => 70% must not include Prospect Products. Please replace these with real SAP Products to continue.');
        }
    }
}

Best Answer chosen by JustAGirlyGeek
Vinit_KumarVinit_Kumar

Hi ,

 

My bad try the below one,this should work:-

 

 @IsTest(SeeAllData=true)

public class OpportunityValidations_Test{

static testmethod void MyUnitTest(){

 

   Account acc = new Account(name='ABS');

   insert acc;

 

   User u = [select id from User where profile.name ='Read Only' limit 1];

 

  system.RunAs(u){

   Opportunity opp = new Opportunity(Name='Test opp'.StageName='Prospecting',CloseDate=system.today(),Probability=80,AccountId=acc.id);

insert opp;

 

  }

 


}

}

All Answers

Vinit_KumarVinit_Kumar

Hi ,

 

Try below 

syet

 

@IsTest(SeeAllData=true)

public class OpportunityValidations{

static testmethod void MyUnitTest(){

 

   Account acc = new Account(name='ABS');

   insert acc;

 

   User u = [select id from User where profile.name ='Read Only'];

  

  system.RunAs(u){

   Opportunity opp = new Opportunity(Name='Test opp'.StageName='Prospecting',CloseDate=system.today();Probability=80,AccountId=acc.id);

insert opp;

 

 

  }

 

   

 

}

}

JustAGirlyGeekJustAGirlyGeek

Thanks. I am getting the following error:

 

Error: Compile Error: expecting a right parentheses, found ';' at line 17 column 98 -- figured this one out. Now getting this:

 

Error: Compile Error: Initial term of field expression must be a concrete SObject: String at line 17 column 54 -- figured this one out too. Now when I run test I get this error:

 

System.QueryException: List has more than 1 row for assignment to SObject

Vinit_KumarVinit_Kumar

Hi ,

 

My bad try the below one,this should work:-

 

 @IsTest(SeeAllData=true)

public class OpportunityValidations_Test{

static testmethod void MyUnitTest(){

 

   Account acc = new Account(name='ABS');

   insert acc;

 

   User u = [select id from User where profile.name ='Read Only' limit 1];

 

  system.RunAs(u){

   Opportunity opp = new Opportunity(Name='Test opp'.StageName='Prospecting',CloseDate=system.today(),Probability=80,AccountId=acc.id);

insert opp;

 

  }

 


}

}

This was selected as the best answer