• Amol Jadhav 9
  • NEWBIE
  • 20 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies
Hello All,
I have written trigger on opportunity, which will display error message if user try to chnage the opportunity stage and for same opportunity there will be Task available with Status not equal to 'Complted'

Trigger
-------------------
trigger ShowErrorMsgOnOpportunity on Opportunity (after update) {
    if(checkRecursive.runOnce()){
        List<Opportunity> countOpenTask = [Select Id, Name, 
                                           (Select Id, Status, WhoId 
                                            From OpenActivities 
                                            Where Status != 'Completed')OppoTask 
                                           From Opportunity 
                                           where Id IN: Trigger.new
                                          ];
        for(Opportunity op : countOpenTask){
            if(op.OpenActivities.size() != 0){
                Trigger.newMap.get(op.Id).addError('Please Close Open Activities to update...');
            }   
        }
        update countOpenTask;
    }
}


And Test Class is
-----------------------
 
@isTest
public class ShowErrorMsgOnOpportunity_Test {
    
    @isTest
    private static void WhenThereAreNoTaskRelatedToOpportunity_ShouldThroughError(){
        String errorMsg = 'Please Close Open Activities to update...';
        
        Test.startTest();
        try{
            
            Account acc = new Account(Name = 'Test Account');
            insert acc;
            
            Opportunity opp = new Opportunity();
            opp.Name = 'Test Opportunity';
            opp.StageName = 'Prospecting';
            opp.CloseDate = Date.today();
            opp.AccountId = acc.Id;
            insert opp;
            
            Task tsk = new Task(WhatId = opp.Id, Subject = 'Test Subject', Status = 'In Progress');
            insert tsk;
            
            update opp;
        }
        
        catch(Exception exc){
            System.assertEquals(exc.getMessage(), errorMsg);
        }
        Test.stopTest();
    }
}

I m not able to cover " if(op.OpenActivities.size() != 0){ Trigger.newMap.get(op.Id).addError('Please Close Open Activities to update...'); } ". Please let me know where I am getting wrong.
I have Created Trigger on Opportunity which displays error message to the user if there is open task on Opportunity and user try to change the Stage value. It is working fine for this scenario but not for other if there is no open task present on opportunity and user try to change stage value and save the record it shows error as,

Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Please Close Open Activities to update...".

Trigger is As
------------------------
trigger ShowErrorMsgOnOpportunity on Opportunity (before insert, before update) {

    List<Opportunity> newOppo = Trigger.new;
    
    try{
        List<Opportunity> countOpenTask = 
        [Select Id, Name, (Select Id, Status, WhoId From OpenActivities Where Status != 'Completed')OppoTask From Opportunity where Id IN: Trigger.new];
       
        for(Opportunity op : countOpenTask){
            if(op.OpenActivities.size() != 0){
              Trigger.newMap.get(op.Id).addError('Please Close Open Activities to update...');
            }   
        }
     }
     catch(Exception excep)
     {
         System.debug('Error='+excep);
     }  
}
Hello All,
I have written trigger on opportunity, which will display error message if user try to chnage the opportunity stage and for same opportunity there will be Task available with Status not equal to 'Complted'

Trigger
-------------------
trigger ShowErrorMsgOnOpportunity on Opportunity (after update) {
    if(checkRecursive.runOnce()){
        List<Opportunity> countOpenTask = [Select Id, Name, 
                                           (Select Id, Status, WhoId 
                                            From OpenActivities 
                                            Where Status != 'Completed')OppoTask 
                                           From Opportunity 
                                           where Id IN: Trigger.new
                                          ];
        for(Opportunity op : countOpenTask){
            if(op.OpenActivities.size() != 0){
                Trigger.newMap.get(op.Id).addError('Please Close Open Activities to update...');
            }   
        }
        update countOpenTask;
    }
}


And Test Class is
-----------------------
 
@isTest
public class ShowErrorMsgOnOpportunity_Test {
    
    @isTest
    private static void WhenThereAreNoTaskRelatedToOpportunity_ShouldThroughError(){
        String errorMsg = 'Please Close Open Activities to update...';
        
        Test.startTest();
        try{
            
            Account acc = new Account(Name = 'Test Account');
            insert acc;
            
            Opportunity opp = new Opportunity();
            opp.Name = 'Test Opportunity';
            opp.StageName = 'Prospecting';
            opp.CloseDate = Date.today();
            opp.AccountId = acc.Id;
            insert opp;
            
            Task tsk = new Task(WhatId = opp.Id, Subject = 'Test Subject', Status = 'In Progress');
            insert tsk;
            
            update opp;
        }
        
        catch(Exception exc){
            System.assertEquals(exc.getMessage(), errorMsg);
        }
        Test.stopTest();
    }
}

I m not able to cover " if(op.OpenActivities.size() != 0){ Trigger.newMap.get(op.Id).addError('Please Close Open Activities to update...'); } ". Please let me know where I am getting wrong.