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
DetirtusDetirtus 

test method for OpportunityContactRole

I'm trying to make a custom controller, but can't get the testMethod to work. When running test I get the following error:

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [OpportunityId]: [OpportunityId]

 I've tried different things with no luck. 

My controller code  with testMethod is:

public class newContactCont {
    public static testMethod void testNewContactCont() {
 
        newContactCont ctrl = new newContactCont();
        
        Account account = ctrl.getAccount();
        Contact contact = ctrl.getContact();
        Opportunity opp = ctrl.getOpportunity();
        OpportunityContactRole role = ctrl.getRole();

        contact.lastname = 'TestLastName';
        role = new OpportunityContactRole(OpportunityId=opp.Id);
        PageReference save = ctrl.save();

    }
    
    Account account;
    Contact contact;
    Opportunity opportunity;
    OpportunityContactRole role;
    
    public Account getAccount() {
        if(account == null) account = new Account();
        return account;
    } 
    
    public Contact getContact() {
        if(contact == null) contact = new Contact();
        return contact;
    }
    
    public Opportunity getOpportunity() {
        if(opportunity == null) opportunity = new Opportunity();
        return opportunity;
    }
    
    public OpportunityContactRole getRole() {
        if(role == null) role = new OpportunityContactRole();
        return role;
    }
    
    public PageReference cancel() {
        PageReference opportunityPage = new ApexPages.StandardController(opportunity).view();
        opportunityPage.setRedirect(true);
        return opportunityPage;
    }
    
    public PageReference save() {

        insert contact;

        role.contactId = contact.id;
        insert role;
        
        PageReference opptyPage = new ApexPages.StandardController(contact).view();
        opptyPage.setRedirect(true);
        
        return opptyPage;
    }

}

 I got the same error for LastName in Contact the first time I tried it and solved it by adding: 

 contact.lastname = 'TestLastName';

I can't however find a solution for the OpportunityId in Role.

 

Anyone got any suggestions?

Best Answer chosen by Admin (Salesforce Developers) 
NasipuriNasipuri

You are doing it wrong way.

 

You need to write test method with the same steps you do for manual testing.

 

Need to have code for

 

Create Account , Create Contact for the Account, Insert Opportunity ..

 

Then using the Id of the inserted Opportunity and Contact insert the ContactRole.

 

In the code you are trying to get  Opportunity opp = ctrl.getOpportunity()

 

But how the ctrl will return it ?

 

Thanks and Regards,

Dinesh Nasipuri

Dinesh.Nasipuri@gmail.com

All Answers

NasipuriNasipuri

You are doing it wrong way.

 

You need to write test method with the same steps you do for manual testing.

 

Need to have code for

 

Create Account , Create Contact for the Account, Insert Opportunity ..

 

Then using the Id of the inserted Opportunity and Contact insert the ContactRole.

 

In the code you are trying to get  Opportunity opp = ctrl.getOpportunity()

 

But how the ctrl will return it ?

 

Thanks and Regards,

Dinesh Nasipuri

Dinesh.Nasipuri@gmail.com

This was selected as the best answer
Rajesh SriramuluRajesh Sriramulu

Hi

 

As per ur class insert some required fields like

for eg:for Account  Account Name is mandatory

and Contact Last Name mandatory

like this only for oppurtunities Oppurtunitiesid is mandatory

and also insert the values for each individually and call the function.