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
Collin EdlCollin Edl 

Apex Code to create custom object on Opportunity

Hi,
I'm trying to see why my test code is failing for when I try to create a custom object off our Opportunities. Running this will only give me 4%. Any help would be greatly appreciated!

Here's the class:
public with sharing class triggerLogic_Opportunity { 

        public static void createProject(List<Opportunity> newOpportunities){
        Set<ID> opportunityIDs = new Set<ID>();
            
        // Check for qualifying opportunities.
        for(Opportunity o : newOpportunities){
            if(o.StageName == 'Closed Won' && o.Type == 'New Business' && o.Setup_Opportunity_Created__c == false){
                opportunityIDs.add(o.id);
                           
            }
        }
        try{
            if(opportunityIDs.size() > 0){
            List<Opportunity> createProjectsFor = opportunityQuery(opportunityIDs);
            List<Milestone1_Project__c> projectsCreated = new List<Milestone1_Project__c>();
            List<Milestone1_Milestone__c> mainMilestones = new List<Milestone1_Milestone__c>();
            Group pUserForOwnerID = [SELECT Id FROM GROUP WHERE Name = 'Sales Operations Queue' AND TYPE = 'QUEUE'];
            
            
           
            //supportLogic_Dates sld = new supportLogic_Dates();
            
            for(Opportunity o : createProjectsFor){
                // Create projects
               
                o.Setup_Opportunity_Created__c = true;
                Milestone1_Project__c project = new Milestone1_Project__c();
                project.Name = o.Name + ' New Account Setup';
                //project.Status__c = 'Agreement Signed';
                //project.Project_Type__c = 'New Account Setup';
                project.Opportunity__c = o.Id;
                project.Kickoff__c = Date.today();
                project.Date_Time_Opened__c = system.now();
                project.Deadline__c = supportLogic_Dates.addBusinessDays(Date.today(), 5);
                project.OwnerId = pUserForOwnerID.Id;
                projectsCreated.add(project);
            }                   
                    
                
            insert(projectsCreated);
            update(createProjectsFor);  
            // Add Main Milestones to each project.
            for(Milestone1_Project__c p : projectsCreated){
                // Create Setup Milestone
                Milestone1_Milestone__c mainMilestone1 = new Milestone1_Milestone__c();
                mainMilestone1.Name = 'Implementation Call';
                mainMilestone1.Project__c = p.id;
                mainMilestone1.Date_Time_Opened__c = system.now();
                mainMilestone1.Kickoff__c = Date.today();
                mainMilestone1.Stage__c = 'Stage 1';
                mainMilestone1.Deadline__c = supportLogic_Dates.addBusinessDays(Date.today(), 4);
                mainMilestones.add(mainMilestone1);
 
                Milestone1_Milestone__c mainMilestone2 = new Milestone1_Milestone__c();
                mainMilestone2.Name = 'Packages';
                mainMilestone2.Project__c = p.id;
                mainMilestone2.Date_Time_Opened__c = system.now();
                mainMilestone2.Kickoff__c = Date.today();
                mainMilestone2.Stage__c = 'Stage 2';
                mainMilestone2.Deadline__c = supportLogic_Dates.addBusinessDays(Date.today(), 4);
                mainMilestones.add(mainMilestone2);
                
                Milestone1_Milestone__c mainMilestone3 = new Milestone1_Milestone__c();
                mainMilestone3.Name = 'Users';
                mainMilestone3.Project__c = p.id;
                mainMilestone3.Date_Time_Opened__c = system.now();
                mainMilestone3.Kickoff__c = Date.today();
                mainMilestone3.Stage__c = 'Stage 3';
                mainMilestone3.Deadline__c = supportLogic_Dates.addBusinessDays(Date.today(), 4);
                mainMilestones.add(mainMilestone3);
                
  
            }   
            
            insert(mainMilestones);
            
        }
        }Catch(Exception ex){
            new Opportunity().addError('Issue in creating associated project.');
            new Milestone1_Project__c().addError('Issue in creating associated project.');
        }
        
        
            
    }
    
    public static List<Opportunity> opportunityQuery(Set<ID> queryThese){
        String pqString = supportLogic_SOQL.getAllObjectFields('Opportunity', 'ID IN :queryThese');
        List<Opportunity> theseCases = (List<Opportunity>)database.query(pqString);
        return(theseCases);
    } 
}

And here's the test code:
 
@isTest
private class test_triggerLogic_Opportunity { 
   static testmethod void testCreateProject(){
        // Create a new opportunity
        Opportunity testOpportunity = new Opportunity();
        Opportunity testOpportunityClone = new Opportunity();
        final ID opportunitiesStandardRTID =  supportLogic_RT.getRTId('Standard', 'Opportunity');
        System.debug('STANDARD RECORD TYPE ID: >>>> + opportunitiesStandardRTID');
        
        testOpportunity.Name = 'Project Test Opportunity';
        testOpportunity.RecordTypeID = opportunitiesStandardRTID;
        testOpportunity.type = 'New Business';
        testOpportunity.StageName = 'Qualification';
        testOpportunity.LeadSource = 'Web';
        testOpportunity.CloseDate = date.parse('09/25/1990');
        testOpportunity.Probability = 10;
        testOpportunity.NextStep = 'Nothing';
        insert(testOpportunity);
        
        // This is to throw an exception for 100% code coverage.
        testOpportunityClone.Name = 'Duplicate Test Opportunity';
        testOpportunityClone.RecordTypeID = opportunitiesStandardRTID;
        testOpportunityClone.type = 'New Business';
        testOpportunityClone.StageName = 'Qualification';
        testOpportunityClone.LeadSource = 'Web';
        testOpportunityClone.CloseDate = date.parse('09/25/1990');
        testOpportunityClone.Probability = 10;
        testOpportunityClone.NextStep = 'Nothing';
        insert(testOpportunityClone);
           
        } 
}

 
Best Answer chosen by Collin Edl
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
@isTest
private class test_triggerLogic_Opportunity 
{ 
	static testmethod void testCreateProject()
	{
		
        Opportunity testOpportunity = new Opportunity();

        final ID opportunitiesStandardRTID =  supportLogic_RT.getRTId('Standard', 'Opportunity');
        System.debug('STANDARD RECORD TYPE ID: >>>> + opportunitiesStandardRTID');
        testOpportunity.Name = 'Project Test Opportunity';
        testOpportunity.RecordTypeID = opportunitiesStandardRTID;
        testOpportunity.type = 'New Business';
        testOpportunity.StageName = 'Closed Won';
        testOpportunity.LeadSource = 'Web';
        testOpportunity.CloseDate = date.parse('09/25/1990');
        testOpportunity.Probability = 10;
        testOpportunity.NextStep = 'Nothing';
		testOpportunity.Setup_Opportunity_Created__c  = false;
        
		try
		{
			insert(testOpportunity);
		}
		Catch(Exception ee)
		{
			
		}
		
    } 
}

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Try to update your test class like below
@isTest
private class test_triggerLogic_Opportunity 
{ 
	static testmethod void testCreateProject()
	{
		
        Opportunity testOpportunity = new Opportunity();

        final ID opportunitiesStandardRTID =  supportLogic_RT.getRTId('Standard', 'Opportunity');
        System.debug('STANDARD RECORD TYPE ID: >>>> + opportunitiesStandardRTID');
        testOpportunity.Name = 'Project Test Opportunity';
        testOpportunity.RecordTypeID = opportunitiesStandardRTID;
        testOpportunity.type = 'New Business';
        testOpportunity.StageName = 'Qualification';
        testOpportunity.LeadSource = 'Web';
        testOpportunity.CloseDate = date.parse('09/25/1990');
        testOpportunity.Probability = 10;
        testOpportunity.NextStep = 'Nothing';
		testOpportunity.Setup_Opportunity_Created__c  = false;
        
		try
		{
			insert(testOpportunity);
		}
		Catch(Exception ee)
		{
			
		}
		
    } 
}

 
Collin EdlCollin Edl
Still getting 4% coverage. Here's a screenshot of what's not being caught:

User-added image
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
@isTest
private class test_triggerLogic_Opportunity 
{ 
	static testmethod void testCreateProject()
	{
		
        Opportunity testOpportunity = new Opportunity();

        final ID opportunitiesStandardRTID =  supportLogic_RT.getRTId('Standard', 'Opportunity');
        System.debug('STANDARD RECORD TYPE ID: >>>> + opportunitiesStandardRTID');
        testOpportunity.Name = 'Project Test Opportunity';
        testOpportunity.RecordTypeID = opportunitiesStandardRTID;
        testOpportunity.type = 'New Business';
        testOpportunity.StageName = 'Closed Won';
        testOpportunity.LeadSource = 'Web';
        testOpportunity.CloseDate = date.parse('09/25/1990');
        testOpportunity.Probability = 10;
        testOpportunity.NextStep = 'Nothing';
		testOpportunity.Setup_Opportunity_Created__c  = false;
        
		try
		{
			insert(testOpportunity);
		}
		Catch(Exception ee)
		{
			
		}
		
    } 
}

 
This was selected as the best answer
Collin EdlCollin Edl
That did it!! Thank you so much for the help