• Detirtus
  • 0 Points
  • Member since 2012

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
  • 1

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();
        return opportunityPage;
    public PageReference save() {

        insert contact;

        role.contactId = contact.id;
        insert role;
        PageReference opptyPage = new ApexPages.StandardController(contact).view();
        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?

Struggling with understanding how to create testmethods so I can package my code to move from development to production.   While there is  information in the manuals, blogs, and discussion boards,  creating testmethods is still confusing.   From the discussion boards, I see that I'm not alone.  I've searched, but haven't been able to find an example testmethod for Lists.....
Could someone describe:
- How they determine what needs to be tested,  and
- How they would go about testing the following sample code  (from cookbook)
and provide testmethod code for this sample List code.
VF Page:
<apex:page controller="sampleCon">
<apex:form >
   <apex:selectCheckboxes value="{!countries}">
   <apex:selectOptions value="{!items}"/>
    <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
<apex:outputPanel id="out">
<apex:actionstatus id="status" startText="testing...">
   <apex:facet name="stop">
    <apex:outputPanel >
      <p>You have selected:</p>
      <apex:dataList value="{!countries}" var="c">{!c}</apex:dataList>

public class sampleCon {
String[] countries = new String[]{};
public PageReference test() {
return null;
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('US','US'));
options.add(new SelectOption('CANADA','Canada'));
options.add(new SelectOption('MEXICO','Mexico'));
return options;
public String[] getCountries() {
return countries;
public void setCountries(String[] countries) {
this.countries = countries;

Thanks in advance....