0

I'm struggling to test a line of code within my test class below:

 public static void cloneRenewalOpportunity(Map<Id, Opportunity> mapNewOpps, Map<Id,Opportunity> mapOldOpps){
    try{
        List<Opportunity> lstOpp = new List<Opportunity>();
        Set<String> sAccountId = new Set<String>();
        Id ArchiveRT = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Archive').getRecordTypeId();
        system.debug('NEW MAP '+ mapNewOpps);
        system.debug('OLD MAP '+ mapOldOpps);
        for(Opportunity opp : mapNewOpps.values()){
            //if the opportunity is an Archive RT Opportunity then Clone 
            if(mapOldOpps.containsKey(opp.Id) && opp.RecordTypeId == ArchiveRT){
                Opportunity oldOpp = mapOldOpps.get(opp.Id);
                system.debug('new'+opp.name+''+opp.stagename);
                system.debug('old'+oldOpp.name+''+oldOpp.stagename);
                if(opp.StageName != oldOpp.StageName && opp.StageName == 'Closed Won'){
                    sAccountId.add(opp.AccountId);
                    lstOpp.add(opp);
                }

Mainly this line here:

if(opp.StageName != oldOpp.StageName && opp.StageName == 'Closed Won'){

in my test class I have created 4 test Opportunities with different names and stages however when i add them run the test class it only ever picks up the first Opportunity so will never hit that line in the apex class

RecordType rt = [SELECT Id, Name, DeveloperName FROM RecordType WHERE SObjectType = 'Opportunity' AND Name = 'Archive'];

 Opportunity oppr = new Opportunity(); 
    oppr.RecordTypeId = rt.Id;   
    oppr.Name = 'OPP 1';       
    oppr.StageName = 'Closed Won';
    oppr.Payment_Terms__c = '30 Days';
    insert oppr;

    Opportunity oppr2 = new Opportunity(); 
    oppr2.RecordTypeId = rt.Id;   
    oppr2.Name = 'Opp 2';   
    oppr2.Probability=20;      
    oppr2.StageName = 'Closed Won';
    oppr2.Payment_Terms__c = '30 Days';
    insert oppr2;

    Opportunity oppr3 = new Opportunity(); 
    oppr3.RecordTypeId = rt.Id;   
    oppr3.Name = 'Opp 3';   
    oppr3.Probability=20;      
    oppr3.StageName = 'Initial Discussion';
    oppr3.Payment_Terms__c = '30 Days';
    insert oppr3;

    Opportunity oppr4 = new Opportunity(); 
    oppr4.RecordTypeId = rt.Id;   
    oppr4.Name = 'Opp 4';   
    oppr4.StageName = 'Initial Discussion';
    oppr4.Payment_Terms__c = '30 Days';

    insert oppr4;

    Map<Id, Opportunity> mapOldOpps = new Map<Id, Opportunity>();
    Map<Id, Opportunity> mapNewOpps = new Map<Id, Opportunity>();

    mapNewOpps.put(Oppr3.id, Oppr3);
    mapNewOpps.put(Oppr4.id, Oppr4);
    mapOldOpps.put(Oppr3.Id, Oppr3);
    mapOldOpps.put(Oppr.Id, Oppr);
    mapOldOpps.put(Oppr2.id, Oppr2);

    // RenewalOpportunity ro = new RenewalOpportunity();
    List<Opportunity> LstOpp = new List<Opportunity>();

    RenewalOpportunity.cloneRenewalOpportunity(mapNewOpps, mapOldOpps);

Any help why it only ever picks the opp "Opp 1" would be massive help, Thanks

  • what is rt.id in oppr.RecordTypeId = rt.Id;. Make sure the record type of opportunity in test class is Archive. – Samir Apr 27 '17 at 9:11
  • 2
    you are passing uneven maps i.e. pass old map with old data and then add a new version of opportunities to the new map, I don't see you adding an updated opportunity ? – d_k Apr 27 '17 at 9:12
  • @Samir I have added the rt.id query above forgot to include it on this – ClevelandDan Apr 27 '17 at 9:13
3

Try this:

Please update stageName accordingly

 Opportunity oppr = new Opportunity();
    oppr.RecordTypeId = rt.Id;
    oppr.Name = 'OPP 1';
    oppr.StageName = 'Closed Won';
    oppr.Payment_Terms__c = '30 Days';
    insert oppr;

    Opportunity oppr2 = new Opportunity();
    oppr2.RecordTypeId = rt.Id;
    oppr2.Name = 'Opp 2';
    oppr2.Probability=20;
    oppr2.StageName = 'Closed Won';
    oppr2.Payment_Terms__c = '30 Days';
    insert oppr2;

    Opportunity oppr3 = new Opportunity();
    oppr3.RecordTypeId = rt.Id;
    oppr3.Name = 'Opp 3';
    oppr3.Probability=20;
    oppr3.StageName = 'Initial Discussion';
    oppr3.Payment_Terms__c = '30 Days';
    insert oppr3;

    Opportunity oppr4 = new Opportunity();
    oppr4.RecordTypeId = rt.Id;
    oppr4.Name = 'Opp 4';
    oppr4.StageName = 'Initial Discussion';
    oppr4.Payment_Terms__c = '30 Days';

    insert oppr4;

    Map<Id, Opportunity> mapOldOpps = new Map<Id, Opportunity>();
    Map<Id, Opportunity> mapNewOpps = new Map<Id, Opportunity>();

    mapOldOpps.put(Oppr3.Id, Oppr3);
    mapOldOpps.put(Oppr.Id, Oppr);
    mapOldOpps.put(Oppr2.id, Oppr2);
    mapOldOpps.put(Oppr1.id, Oppr1);

    //edit opportunities
    Opportunity updatedoppr = new Opportunity();
    updatedoppr.Id = oppr.Id;
    oppr.RecordTypeId = rt.Id;
    oppr.Name = 'OPP 1';
    oppr.StageName = 'changed stage name';
    oppr.Payment_Terms__c = '30 Days';


    Opportunity updatedoppr2 = new Opportunity();
    updatedoppr2.Id = oppr2.Id;
    oppr2.RecordTypeId = rt.Id;
    oppr2.Name = 'Opp 2';
    oppr2.Probability=20;
    oppr2.StageName = 'changed stage name';
    oppr2.Payment_Terms__c = '30 Days';

    Opportunity updatedoppr3 = new Opportunity();
    updatedoppr3.Id = oppr3.Id;
    oppr3.RecordTypeId = rt.Id;
    oppr3.Name = 'Opp 3';
    oppr3.Probability=20;
    oppr3.StageName = 'Ichanged stage name';
    oppr3.Payment_Terms__c = '30 Days';

    Opportunity updatedoppr4 = new Opportunity();
    updatedoppr4.Id = oppr4.Id;
    oppr4.RecordTypeId = rt.Id;
    oppr4.Name = 'Opp 4';
    oppr4.StageName = 'changed stage name';
    oppr4.Payment_Terms__c = '30 Days';

    mapNewOpps.put(updatedoppr.Id, updatedoppr);
    mapNewOpps.put(updatedoppr2.Id, updatedoppr2);
    mapNewOpps.put(updatedoppr3.id, updatedoppr3);
    mapNewOpps.put(updatedoppr3.id, updatedoppr4);

    // RenewalOpportunity ro = new RenewalOpportunity();
    List<Opportunity> LstOpp = new List<Opportunity>();

    RenewalOpportunity.cloneRenewalOpportunity(mapNewOpps, mapOldOpps);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.