You need to sign in to do that
Don't have an account?
Creating two objects at once
I have a custom object for Demos with a multi relationship to Contacts meaning that a contact could have multiple demos and a demo has multiple contacts. For this multi relationship to work there is a custom Demo Contact object that simply has two fields - Contact and Demo. Currently the users have to create the Demo Object and then manually tie it back to a contact object. I'd like to make it so that they can easily press a button and behind the scenes it will Create a blank new Demo record, tie it to a new demo contact record for that contact and then direct them to the edit page for the Demo. So to them they would just click on "New Demo" and fill in the information and the Contact would automatically be there in the related list when they hit save. I've tried to do this with custom buttons but I have issues that they won't auto save for me.
Any ideas would be highly appreciated.
Thanks,
Amanda
I would create a Visualforce page that calls an apex page.
This visualforce page would basically be empty and would redirect to where ever you wanted to go afterwords.
The apex page would create your objects and insert them into the database.
There are a couple of gotchas.
1) any fields that you want to transfer data from have to be part of the visualforce page
2) you cannot do DML operations in a constructor.
as far as apex:
Demo__c tutorial1 = new Demo__c (
Name = 'some name'
);
insert(tutorial1);
Contact the_contact = new Contact (
Name = 'John',
//other fields are necessary
);
insert(the_contact);
tutorial1.Contact__c = the_contact.Id;
update(tutorial1);
The button could just be a URL:
https://[the domain]/[Id of your record]
you can do this dynamically too.
You will need to do a PageReference and create a Visualforce controller. All your logic would be in this Visualforce Controller.
All Answers
I would create a Visualforce page that calls an apex page.
This visualforce page would basically be empty and would redirect to where ever you wanted to go afterwords.
The apex page would create your objects and insert them into the database.
There are a couple of gotchas.
1) any fields that you want to transfer data from have to be part of the visualforce page
2) you cannot do DML operations in a constructor.
as far as apex:
Demo__c tutorial1 = new Demo__c (
Name = 'some name'
);
insert(tutorial1);
Contact the_contact = new Contact (
Name = 'John',
//other fields are necessary
);
insert(the_contact);
tutorial1.Contact__c = the_contact.Id;
update(tutorial1);
The button could just be a URL:
https://[the domain]/[Id of your record]
you can do this dynamically too.
You will need to do a PageReference and create a Visualforce controller. All your logic would be in this Visualforce Controller.
I was afraid you would say that but in the end it wasn't as hard as I was fearing. Here's the finished class and the VF page was practically nothing: