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
ForceRookieForceRookie 

Insert multiple objects’s data in one controller

I created a visualforce page in which it has
form for create new contact,
form for create new account,
and form for create new opportunity..
inside <apex:form>
(Fields are dynamic)

Task:
- create variable in controller for Contact’s LastName and variable for Account’s Name and call it in InputText in VF.
- if I created a new contact, and new opportunity, the Contact will be the Primary Contact of the Opportunity created.

Help me to improve my Controller.

Thanks in advance, I’ll appreciate the help..
public class myController {

public Contact con {get;set;}
public Account acc {get;set;}
public Opportunity opp {get;set;}

public Contact getcon() {
return con;
}

public Account getacc() {
return acc;
}

public Opportunity getopp() {
return opp;
}

public PageReference save() {
insert con;
insert acc;
insert opp;
return null;
}

}


 
Best Answer chosen by ForceRookie
Gaurish Gopal GoelGaurish Gopal Goel
Okay then try this code
public class myController 
{
	public Contact con {get;set;}
	public Account acc {get;set;}
	public Opportunity opp {get;set;}

	public myController ()
	{
		con = new COntact();
		acc = new Account();
		opp = new Opportunity();
	}
	public PageReference save() 
	{
		if(String.isNotBlank(con.LastName))
		{
			insert con;
			opp.Primary_Contact__c = con.Id;
		}
		insert acc;
		opp.Name = acc.Name;
		insert opp;
		return new PageReference('/'+opp.Id).setRedirect(true);
	}
}

All Answers

Gaurish Gopal GoelGaurish Gopal Goel
Hi,

CONTROLLER CODE:
public class myController 
{
	public Contact con {get;set;}
	public Account acc {get;set;}
	public Opportunity opp {get;set;}

	public myController ()
	{
		con = new COntact();
		acc = new Account();
		opp = new Opportunity();
	}
	public PageReference save() 
	{
		insert con;
		insert acc;
		opp.Name = acc.Name;
		insert opp;
		
		OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId=opp.Id,ContactId=con.Id,IsPrimary=True);
        insert ocr;
		return new PageReference('/'+opp.Id).setRedirect(true);
	}
}
VF PAGE CODE:
<apex:page controller="myController">
	<apex:form>
		Contact LastName - <apex:inputField value="{!con.LastName}"/>
		Account Name - <apex:inputField value="{!acc.Name}"/>
		Opportunity Close Date - <apex:inputField value="{!opp.CloseDate}"/>
		Opportunity Stage Name - <apex:inputField value="{!opp.StageName}"/>
		<apex:commandButton value="Save" action="{!save}"/>
	</apex:form>
</apex:page>

If this answer solves your problem then mark it as the solution to help others. Thanks.
ForceRookieForceRookie
Hi, Gaurish.. I tried that code, but it doesn’t save anything on Contact, Account, and Opportunity.. my page just refreshes when I clicked the save button. And also, the Primary Contact is an Lookup field in the Opportunity.
Gaurish Gopal GoelGaurish Gopal Goel
Then there must be some required field missing in any of these 3 objects. You need to put all the required fields before inserting any record.

For example: Suppose Email field on Contact object is required then you need to put an extra field on your VF Page like this:
<apex:inputField value="{!con.Email}"/>

So you need to find out all the required field for all three objects and put on your VF page.

Update your controller code to this for PrimaryContact field, I am assuming that the field API name is Primary_Contact__c. 
public class myController 
{
	public Contact con {get;set;}
	public Account acc {get;set;}
	public Opportunity opp {get;set;}

	public myController ()
	{
		con = new COntact();
		acc = new Account();
		opp = new Opportunity();
	}
	public PageReference save() 
	{
		insert con;
		insert acc;
		opp.Name = acc.Name;
                opp.Primary_Contact__c = con.Id;
		insert opp;
		return new PageReference('/'+opp.Id).setRedirect(true);
	}
}
ForceRookieForceRookie
Thanks for the response. I will try it. But I have to declare variable of Contact’s LastName and Account’s Name first in controller then call it in inputText in VF. So that I will not get an error for example in Account required field if I only fill up the Contact form and Opportunity form. Will you help me how to do it? :)
Gaurish Gopal GoelGaurish Gopal Goel
You can add PageMessage tag in your VF page to see the error messages:
<apex:page controller="Test">
    <apex:form>
        <apex:pageMessages></apex:pageMessages>
        <apex:inputField value="{!con.LastName}"/>
        <apex:inputField value="{!acc.Name}"/>
        <apex:inputField value="{!opp.CloseDate}"/>
        <apex:inputField value="{!opp.StageName}"/>
        <apex:commandButton value="Save" action="{!save}"/>
    </apex:form>
</apex:page>
ForceRookieForceRookie
Your codes works if I fill up the Contact form, Account form, and Opportunity form at once, thanks for that. :) But If I try to create an account and opportunity only, it wont save, it errors because the Contact.LastName is required. So it must be save as well even if I don’t fill the Contact form. Same approach if I don’t fill the Account form.
Gaurish Gopal GoelGaurish Gopal Goel
Okay then try this code
public class myController 
{
	public Contact con {get;set;}
	public Account acc {get;set;}
	public Opportunity opp {get;set;}

	public myController ()
	{
		con = new COntact();
		acc = new Account();
		opp = new Opportunity();
	}
	public PageReference save() 
	{
		if(String.isNotBlank(con.LastName))
		{
			insert con;
			opp.Primary_Contact__c = con.Id;
		}
		insert acc;
		opp.Name = acc.Name;
		insert opp;
		return new PageReference('/'+opp.Id).setRedirect(true);
	}
}
This was selected as the best answer
ForceRookieForceRookie
Thanks Gaurish! But how to make a condition of it, that it will not be save if both Contact and Account is not filled, only the Opportunity. It should notify “Please enter Account or Contact”.
ForceRookieForceRookie
Hi Gaurish, How to make that ISNOTBLANK to Opportunity CloseDate?
Gaurish Gopal GoelGaurish Gopal Goel
Try this
public PageReference save() 
{
	if(String.isNotBlank(con.LastName))
	{
		insert con;
		opp.Primary_Contact__c = con.Id;
	}
	insert acc;
	if(opp.CloseDate != null)
	{
		opp.Name = acc.Name;
		insert opp;
		return new PageReference('/'+opp.Id).setRedirect(true);
	}
	else
	{
		return new PageReference('/'+acc.Id).setRedirect(true);
	}
}
ForceRookieForceRookie
Hi Gaurish, thanks for the reply. But what I mean is how to bypass the Opportunity required fields if I only filled up the Account or Contact? It should be save even without filling the Opportunity form.. I did my best but it always shows an error REQUIRED_FIELD_MISSING [Opportunity Name, StageName, CloseDate]
Gaurish Gopal GoelGaurish Gopal Goel
Change the Command button code:
<apex:commandButton value="Save" action="{!save}" immediate="true"/>
ForceRookieForceRookie
Hi Gaurish, I marked your answers as best answer. Thanks man!

I have one favor, will you help me with its Test Class? ‘Coz I can’t find anything when I searched. Hoping for your reply again :)
Gaurish Gopal GoelGaurish Gopal Goel
Hi, Please send the test class code. I will check what's the problem.