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
StaciStaci 

test class coverage for 2 lines

I have a custom object where the record has a button to create an opportunity.  There is a lookup on the custom object that fills in the opportunity name when the opportunity is created through the button.  I have 2 lines of code I can't get covered and I'm not sure how to accomplish, please help!  TIA!

These are the 2 lines I can't get covered
acc.Related_to_Opportunity__c = MapAccCom.get(acc.id);
        oppList.add(acc);

trigger IDM_GSVT_UpdateRelatedOpportunity on EOMPDiscountRequest__c (before insert) {
    //Name - field to be added to Related_to_Opportunity
    //Related_to_Opportunity__c //look up on GSVT record to be updated
    Map<Id, Id> MapAccCom = new Map<Id, Id>();
    for(EOMPDiscountRequest__c comm:trigger.new)
    {
        MapAccCom.put(comm.Name,comm.id);
    }
    List<EOMPDiscountRequest__c> disc = [Select id from EOMPDiscountRequest__c where id IN :MapAccCom.keyset()];
    List<EOMPDiscountRequest__c> oppList = new List<EOMPDiscountRequest__c>();
    for(EOMPDiscountRequest__c acc : disc)
    {
        acc.Related_to_Opportunity__c = MapAccCom.get(acc.id);
        oppList.add(acc);
    }
    update oppList;

}

@isTest
private class IDM_GSVT_UpdateRelatedOpportunityTest {
 private static EOMPDiscountRequest__c createDiscountRequest(string dealerCode, string marketSegment, string segmentType, string discountType, string majorClass) {
        Id OTOretro = Schema.SObjectType.EOMPDiscountRequest__c.getRecordTypeInfosByName().get('OTO Retroactive').getRecordTypeId();
        EOMPDiscountRequest__c discountRequestObj = new EOMPDiscountRequest__c(Dealer_Code__c = dealerCode,
                                                                                Market_Segment_Picklist__c = marketSegment,
                                                                                Major_Class__c = majorClass,
                                                                                Industry_GASD__c = segmentType,
                                                                                Valid_From__c = system.today(),
                                                                                Valid_To__c = system.today(),
                                                                                Discount_Type__c = discountType,
                                                                                recordTypeId = OTOretro,
                                                                                M_Rate__c = 25);
        insert discountRequestObj;
        return discountRequestObj;
    }
private static testMethod void testController(){    
    Map<String,schema.recordtypeinfo> rtMapByName=schema.sobjecttype.account.getRecordtypeInfosByName();
        Id ryById=rtMapByName.get('Customer/Partner').getRecordTypeId();
        Account acc = new Account(Name = 'Account1',RecordtypeId = ryById,Account_Usage__c='GCI');
        insert acc;
        
        Map<String,schema.recordtypeinfo> rtdelMapByName=schema.sobjecttype.account.getRecordtypeInfosByName();
        Id rydelById=rtdelMapByName.get('Dealer').getRecordTypeId();
        Account delacc = new Account(Name = 'delAccount1',RecordtypeId = rydelById,Account_Usage__c='GCI');
        insert delacc;
    
       Id oppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Parts & Service Opportunity').getRecordTypeId();
        Opportunity oppty = new Opportunity();
        oppty.Name = 'test1';
        oppty.RecordTypeId = oppRecordTypeId;
        oppty.AccountId = acc.id;
        oppty.Major_Class__c = 'CSA';
        oppty.Dealer__c = delacc.id;
        oppty.Marketing_Campaign__c = 'GD UC';
        oppty.Amount = 50000.00;
        oppty.LeadSource = 'other';
        oppty.CurrencyIsoCode = 'INR';
        oppty.StageName = 'Closed Won';
        oppty.CloseDate = system.today();
        oppty.Description = 'test';
        oppty.Primary_Lost_Sale_Reason__c = 'Product';
        oppty.Primary_Lost_Sale_Reason_Detail__c = 'Worktool';
        oppty.Winning_Competitor_legacy__c = 'BYG';
        oppty.GASD_Industry__c = 'Construction Industries';
        insert oppty;
        }
        
}

 
saurabh_kumar_Ssaurabh_kumar_S
Hi @staci 
I think this is happening because in trigger line no. 09 you are doing soql on keySet of map which fetch name instead of ID.
This will make sure your test coverage to 100% ,
But i don't think  as per you have describe your requirement  it will update opportunity on your custom object EOMPDiscountRequest__c :Related _to_Opportunity__c field  when a opportunity get created , For that you should  do  write trigger on Opportunity(after insert)   and  update  your custom object  EOMPDiscountRequest__c accordingly 
trigger IDM_GSVT_UpdateRelatedOpportunity on EOMPDiscountRequest__c (before insert) {
    //Name - field to be added to Related_to_Opportunity
    //Related_to_Opportunity__c //look up on GSVT record to be updated
    Map<Id, Id> MapAccCom = new Map<Id, Id>();
    for(EOMPDiscountRequest__c comm:trigger.new)
    {
        MapAccCom.put(comm.Name,comm.id);
    }
   List<EOMPDiscountRequest__c> disc = [Select id from EOMPDiscountRequest__c where id IN :MapAccCom.values()];
    List<EOMPDiscountRequest__c> oppList = new List<EOMPDiscountRequest__c>();
    for(EOMPDiscountRequest__c acc : disc)
    {
        acc.Related_to_Opportunity__c = MapAccCom.get(acc.id);
        oppList.add(acc);
    }
    update oppList;

}


 
Amit Chaudhary 8Amit Chaudhary 8
Hi ,

Please check your query as suggested by saurabh.
List<EOMPDiscountRequest__c> disc = [Select id from EOMPDiscountRequest__c where id IN :MapAccCom.values()];

and performe the Insert for EOMPDiscountRequest__c record like below
@isTest
private class IDM_GSVT_UpdateRelatedOpportunityTest {


		
	static EOMPDiscountRequest__c createDiscountRequest(string dealerCode, string marketSegment, string segmentType, string discountType, string majorClass) {
        Id OTOretro = Schema.SObjectType.EOMPDiscountRequest__c.getRecordTypeInfosByName().get('OTO Retroactive').getRecordTypeId();
        EOMPDiscountRequest__c discountRequestObj = new EOMPDiscountRequest__c(Dealer_Code__c = dealerCode,
                                                                                Market_Segment_Picklist__c = marketSegment,
                                                                                Major_Class__c = majorClass,
                                                                                Industry_GASD__c = segmentType,
                                                                                Valid_From__c = system.today(),
                                                                                Valid_To__c = system.today(),
                                                                                Discount_Type__c = discountType,
                                                                                recordTypeId = OTOretro,
                                                                                M_Rate__c = 25);
        insert discountRequestObj;
        return discountRequestObj;
    }
	
	private static testMethod void testController()
	{    
		Map<String,schema.recordtypeinfo> rtMapByName=schema.sobjecttype.account.getRecordtypeInfosByName();
        Id ryById=rtMapByName.get('Customer/Partner').getRecordTypeId();
    
		Account acc = new Account(Name = 'Account1',RecordtypeId = ryById,Account_Usage__c='GCI');
        insert acc;
        
        Map<String,schema.recordtypeinfo> rtdelMapByName=schema.sobjecttype.account.getRecordtypeInfosByName();
        Id rydelById=rtdelMapByName.get('Dealer').getRecordTypeId();

        Account delacc = new Account(Name = 'delAccount1',RecordtypeId = rydelById,Account_Usage__c='GCI');
        insert delacc;
    
		Id oppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Parts & Service Opportunity').getRecordTypeId();
        
		Opportunity oppty = new Opportunity();
			oppty.Name = 'test1';
			oppty.RecordTypeId = oppRecordTypeId;
			oppty.AccountId = acc.id;
			oppty.Major_Class__c = 'CSA';
			oppty.Dealer__c = delacc.id;
			oppty.Marketing_Campaign__c = 'GD UC';
			oppty.Amount = 50000.00;
			oppty.LeadSource = 'other';
			oppty.CurrencyIsoCode = 'INR';
			oppty.StageName = 'Closed Won';
			oppty.CloseDate = system.today();
			oppty.Description = 'test';
			oppty.Primary_Lost_Sale_Reason__c = 'Product';
			oppty.Primary_Lost_Sale_Reason_Detail__c = 'Worktool';
			oppty.Winning_Competitor_legacy__c = 'BYG';
			oppty.GASD_Industry__c = 'Construction Industries';
        insert oppty;
		
		EOMPDiscountRequest__c dr = createDiscountRequest( 'dealerCode' , 'marketSegment' , 'segmentType' , 'discountType' ,'majorClass' );
		
    }
        
}

Let us know if this will help you