+ Start a Discussion
srikanth11srikanth11 

plz help me with this issue

tthis is the trigger i have written and in my trigger i am unable to cover this part   i have given trigger.new and old please help me how to cover this part

 

 

 

    

trigger trgCaptureConsultants on Opportunity (before Insert, before Update) {

    If(Trigger.New.Size() == 1)
    {
        If(Trigger.IsInsert)
        {
            String ProfileName = [SELECT Profile.Name FROM User where Id = :Trigger.New[0].OwnerId].Profile.Name;    
            
            If(ProfileName.contains('Sales'))//&& (Trigger.New[0].Sales_Consultant__c == Null || Trigger.New[0].Sales_Consultant__c == '')
            {
                Trigger.New[0].Sales_Consultant__c = Trigger.New[0].OwnerId;
                If(Trigger.New[0].Sales_Consultan__c == null)
                {
                Trigger.New[0].Sales_Consultan__c = Trigger.New[0].OwnerId;
                }


            }
            if(ProfileName.contains('Process') ) //&& (Trigger.New[0].Process_Consultant__c == Null || Trigger.New[0].Process_Consultant__c == ''))
            {    
                Trigger.New[0].Process_Consultant__c = Trigger.New[0].OwnerId;
                If(Trigger.New[0].Process_Consultant_User__c == null)
                {
                Trigger.New[0].Process_Consultant_User__c = Trigger.New[0].OwnerId;
                }
            }
            if( ProfileName.contains('Evaluation')) 
            {
                Trigger.New[0].Process_Consultant__c = Trigger.New[0].OwnerId;
                Trigger.New[0].Process_Consultant_User__c = Trigger.New[0].OwnerId;
            }
        } 
        Else If(Trigger.IsUpdate)
        {
            String ProfileName = [SELECT Profile.Name FROM User where Id = :Trigger.New[0].OwnerId].Profile.Name;    
            If(Trigger.New[0].
OwnerId != Trigger.Old[0].OwnerId)
            {
                
                
                If(
ProfileName.contains('Sales'))
                {
                    Trigger.
New[0].Sales_Consultant__c = Trigger.New[0].OwnerId;
                    
                    If(
Trigger.New[0].Sales_Consultant__c != null)
                    {
                    Trigger.
New[0].Sales_Consultan__c = Trigger.New[0].OwnerId;
                    }

                }
                if(
ProfileName.contains('Process'))
                 {
                    Trigger.
New[0].Process_Consultant__c = Trigger.New[0].OwnerId;
                    
                    If(
Trigger.New[0].Process_Consultant__c != null)
                    {
                    Trigger.
New[0].Process_Consultant_User__c = Trigger.New[0].OwnerId;
                    }
                 }
                 
            }

            if(ProfileName.contains('Evaluation'))
            {
                Trigger.New[0].Process_Consultant__c = Trigger.New[0].OwnerId;
                Trigger.New[0].Process_Consultant_User__c = Trigger.New[0].OwnerId;
            }
                
                    
        }
        
    
    } 

this is the test case i have written i am getting inly 50 percent plz help me resolve this issue

 

@isTest
private class trgCaptureConsultants_TESTS{
static testMethod void mytestclass(){
      Account a = [Select id, name from account limit 1 ];
   user u = [SELECT Profile.Name FROM User where profile.name = 'Sales Consultant' limit 1 ];
  
   
Opportunity o = new Opportunity();
o.name = 'raja';
o.OwnerId  = u.id;
o.Accountid = a.id;
o.StageName = 'Opened';
o.CloseDate = system.today();
o.Process_Consultant__c = '0053000000203VvAAI';
o.Sales_Consultan__c = null ;
o.StageName = 'Evaluation Sale Made';
o.Apply_For_Exception_Withdrawal_Refund__c = False ;
try
{
insert o;
}
catch (Exception e)
{}

Case c = new Case ();
c.accountid = a.id;
c.OwnerId  = o.Process_Consultant__c;
c.Opportunity_Name__c  = o.id; 
c.Status = 'New';
c.Origin = 'Email';
c.Case_Reason__c = 'Process';
c.Type = 'Suggestions';
      try{
         Insert c;
Update c;
         }
         catch (Exception e)
      {}
            
            
       }  
      }

 


cashworthcashworth

Hey There...

 

First thing to do would be to create  in your within test class. Below is some samples of how this is done. Just change the profiles to match what you need.

 

 Profile p1 = [select id from profile where Name='Standard User']; 
       UserRole ur1 = [select id from UserRole where Name='NAM - Pacific Region South'];
 
       User u1 = new User(alias = 'sales1', email='salesuser1@testorg.com', 
            emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US', 
            localesidkey='en_US', profileid = p1.Id, ForecastEnabled = true,
            timezonesidkey='America/Los_Angeles', username='salesuser1@testorg.com');
         
        insert u1;
        System.assertNotEquals(u1.Id, null);
        


       Profile p2 = [select id from profile where Name='Inside Sales Rep']; 
       UserRole ur2 = [select id from UserRole where Name='ISR - Pacific Region South'];
      
       User u2 = new User(alias = 'isr1', email='isruser1234@testorg.com', 
            emailencodingkey='UTF-8', lastname='Testing2', languagelocalekey='en_US', 
            localesidkey='en_US', profileid = p2.Id, ForecastEnabled = true,
            timezonesidkey='America/Los_Angeles', username='isruser1234@testorg.com1');
         
        insert u2;
        System.assertNotEquals(u2.Id, null);

 

Next make sure you change the owner on the opportunity in the test class from user 1 to user 2 to the exercising the trigger lines not being covered

 

   If(Trigger.New[0].OwnerId != Trigger.Old[0].OwnerId)
            {

Good Luck