1
public void updateAccessLevelsOnUpdate(final List<Opportunity> newOppList, Map<Id, Opportunity> oldOppMap){
        if(Istrue == true){
        try{
            Map<Id, Id> opportunityAssignedToMap = new Map<Id, Id>();
            Set<String> uniqueOppUserCombination = new Set<String>();

            for(Opportunity opp :newOppList){
                **if(opp.Assigned_To__c != oldOppMap.get(opp.Id).Assigned_To__c && opp.Assigned_To__c != null){
                    opportunityAssignedToMap.put(opp.Id, opp.Assigned_To__c);**
                    uniqueOppUserCombination.add(opp.Id+'_'+opp.Assigned_To__c);
                }
            }

            List<OpportunityTeamMember> oppTeamMemberList = new List<OpportunityTeamMember>();
            oppTeamMemberList = getOpportunityTeamMember(opportunityAssignedToMap);

            if(oppTeamMemberList.size() > 0){
                Insert oppTeamMemberList;
            }    

            List<OpportunityShare> opportunityShareList = new List<OpportunityShare>();
            opportunityShareList = getOpportunityShareList(opportunityAssignedToMap, uniqueOppUserCombination);

            if(opportunityShareList.size() > 0){
                update opportunityShareList;
            }
        }
        catch(exception e){for(Opportunity opp :newOppList) opp.addError('Sorry- '+e.getMessage());}
    }
    } 

Test class

 @isTest(seeAllData=false)
    public Class OpportunityTriggerHandler_Test{



    static testMethod void updateAccessLevelsOnInsert_test()

  {

      Test.startTest();
      init();
      Opportunity opp= [select ID , Assigned_To__c,Name from  Opportunity where property__c =: newproperty.Id limit 1];       
      opp.Name = 'Suresh';
      update opp;      
      newopportunity.add(opp); 

      Map <id,Opportunity> oldOppMap= new map<id,Opportunity>();
      Opportunity OpportunityID= [select ID , Assigned_To__c from  Opportunity where property__c =: newproperty.Id limit 1];     
      oldOppMap.put(newUser.Id,OpportunityID);



      OpportunityTriggerHandler Opportunityobj =new OpportunityTriggerHandler();       
      Opportunityobj.updateAccessLevelsOnUpdate(newopportunity,oldOppMap); 

      Test.stopTest();


     } 
     }
1
  • 1
    People will get a more chance to help you here if you can provide more information such as what is the difficulty you are facing, any errors you receive, applicable code snippets from the trigger, etc.
    – highfive
    Commented Mar 6, 2019 at 11:25

1 Answer 1

4

To make this code enter it's body:

if(opp.Assigned_To__c != oldOppMap.get(opp.Id).Assigned_To__c && opp.Assigned_To__c != null) {
    ...
}

assuming your code is called from a trigger, you will need to modify the Assigned_To__c value of one or more of the Opportunities you create in your code.

Assuming the field isn't defaulted to the current User this should be enough:

  opp.Name = 'Suresh';
  opp.Assigned_To__c = UserInfo.getUserId();
  update opp;

If it is already set to the current User, you will need to create a new User in your test code and use the Id of that; Google for examples.

You must log in to answer this question.