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
Vidya H 4Vidya H 4 

plese help me to write test class for this trigger

trigger OppValidation on Opportunity (before insert,before update) {
    
    Id profileId= userinfo.getProfileId();
    String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
    if(Trigger.isupdate){
        for(Opportunity opp:Trigger.new){
            opportunity oldoppy= Trigger.oldmap.get(opp.id);
            
            if(opp.Active__c ==false && oldoppy.Active__c==True && opp.stagename!='Closed Won' && profileName!='System Administrator'){
                opp.adderror('You do not have the access to perform this operation. Kindly contact your system administrator');
            }
        }
        
    }
    
    if(Trigger.isinsert){
        for(Opportunity opp:Trigger.new){
            
            if(opp.Active__c ==false && opp.stagename!='Closed Won' && profileName!='System Administrator'){
                opp.adderror('You do not have the access to perform this operation. Kindly contact your system administrator');
            }
        }
    }
}
Best Answer chosen by Vidya H 4
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Can you try the below test class for the trigger.
Replace the profile name in the query to which ever profile who want to test it withb except system admin profile.
@istest
public class OppValidationTest {
static testmethod void triggerTest(){
    
    User u = new User(
     ProfileId = [SELECT Id FROM Profile WHERE Name = 'sasss'].Id,
     LastName = 'last',
     Email = 'puser000@amamama.com',
     Username = 'puser000@amamama.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'alias',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US'
);
    system.runAs(u){
        account acc = new account();
        acc.name='test';
         insert acc;
        opportunity opp = new opportunity();
        opp.name='test opp';
        opp.stagename='prospecting';
         opp.closeDate=Date.today();
         opp.AccountId=acc.id;
    opp.Active__c=false;
    try{
        insert opp; 
        
    }
    catch(Exception ex){
        Boolean expectedExceptionThrown =  ex.getMessage().contains('You do not have the access to perform this operation. Kindly contact your system administrator') ? true : false;
System.assertEquals(expectedExceptionThrown, true);
    }
        opportunity opp1 = new opportunity();
        opp1.name='test opp';
        opp1.stagename='prospecting';
         opp1.closeDate=Date.today();
         opp1.AccountId=acc.id;
        opp1.Active__c=true;
        insert opp1;
        opp1.Active__c=false;
         try{
        update opp1; 
        
    }
    catch(Exception ex){
        Boolean expectedExceptionThrown =  ex.getMessage().contains('You do not have the access to perform this operation. Kindly contact your system administrator') ? true : false;
System.assertEquals(expectedExceptionThrown, true);
    }
    }  
    }
}

If this solution helps, please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Can you try the below test class for the trigger.
Replace the profile name in the query to which ever profile who want to test it withb except system admin profile.
@istest
public class OppValidationTest {
static testmethod void triggerTest(){
    
    User u = new User(
     ProfileId = [SELECT Id FROM Profile WHERE Name = 'sasss'].Id,
     LastName = 'last',
     Email = 'puser000@amamama.com',
     Username = 'puser000@amamama.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'alias',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US'
);
    system.runAs(u){
        account acc = new account();
        acc.name='test';
         insert acc;
        opportunity opp = new opportunity();
        opp.name='test opp';
        opp.stagename='prospecting';
         opp.closeDate=Date.today();
         opp.AccountId=acc.id;
    opp.Active__c=false;
    try{
        insert opp; 
        
    }
    catch(Exception ex){
        Boolean expectedExceptionThrown =  ex.getMessage().contains('You do not have the access to perform this operation. Kindly contact your system administrator') ? true : false;
System.assertEquals(expectedExceptionThrown, true);
    }
        opportunity opp1 = new opportunity();
        opp1.name='test opp';
        opp1.stagename='prospecting';
         opp1.closeDate=Date.today();
         opp1.AccountId=acc.id;
        opp1.Active__c=true;
        insert opp1;
        opp1.Active__c=false;
         try{
        update opp1; 
        
    }
    catch(Exception ex){
        Boolean expectedExceptionThrown =  ex.getMessage().contains('You do not have the access to perform this operation. Kindly contact your system administrator') ? true : false;
System.assertEquals(expectedExceptionThrown, true);
    }
    }  
    }
}

If this solution helps, please mark it as best answer.

Thanks,
 
This was selected as the best answer
Vidya H 4Vidya H 4
@Sai Praveen 
after run test getting error
System.QueryException: List has no rows for assignment to SObject