+ Start a Discussion
SMasterSMaster 

How to Create a new record in the related list of an opportunity??

Hi,

 

I want to create a new record in the related list of the Opportunity, using the visualforce button on the Opportunity Page layout.

 

I have an Proposal object as a related list in an opportunity. Whenever a user click a button on the oportunity page layout a new record inside Proposal should get created.... how can i achieve that??

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

At first reading,

We understand like you are creating Prpoposal record on button click,

To create this proposal record you are using some information from current opportunity,

 

steps

Create a detail Page button on Opportunity object, with content source as execute javascript.

you can use sforce.connection.create call from ajax toolkit.

dont miss to link current Opportunity record to newlly created Proposal record.

after successful insert, reload the page,

you can see newlly created  record in related list

 

 

If you face any issue, let me know your Custom object API name and Opportunity -to-Proposal field Mapping

I will help you for script

 

Cheers,

Bala

 

 

 

 

All Answers

b-Forceb-Force

At first reading,

We understand like you are creating Prpoposal record on button click,

To create this proposal record you are using some information from current opportunity,

 

steps

Create a detail Page button on Opportunity object, with content source as execute javascript.

you can use sforce.connection.create call from ajax toolkit.

dont miss to link current Opportunity record to newlly created Proposal record.

after successful insert, reload the page,

you can see newlly created  record in related list

 

 

If you face any issue, let me know your Custom object API name and Opportunity -to-Proposal field Mapping

I will help you for script

 

Cheers,

Bala

 

 

 

 

This was selected as the best answer
SMasterSMaster

Hi Bala,

 

You have understood this absolutely correct...

 

 

My Custom Object API name is opportunity_proposals__c and in this custom object i have a custom field Opportunity__c ( a lookup to an opportunity)

 

i need to create  the proposal record with proposal name as an opportunity name followed by "Proposal".

 

I am really looking forward to it....

 

Thanks

 

SMasterSMaster

Hi Bala,

 

I am using the following code:

 

{!requireScript("/soap/ajax/20.0/connection.js")}
{!requireScript("/soap/ajax/20.0/apex.js")}


var t1= new sforce.SObject("opportunity_proposals__c" );

//t1.Proposal_Name__c = "{!Opportunity.Name}" + "Proposal";
t1.Opportunity__c = "{!Opportunity.Name}";
result = sforce.connection.create([t1]);
alert(result );

 

 

But i am not able to relate an opportunity with newly created record... and the record is not getting created... please let me know what have i missed??

b-Forceb-Force

you was very close to success,

 

You was trying to link newly created record by name, which should be by Id

One more suggestion always do any DML inside try----catch

 

Here is updated code

 

 

{!requireScript("/soap/ajax/20.0/connection.js")}
{!requireScript("/soap/ajax/20.0/apex.js")}

try
{
var t1= new sforce.SObject("opportunity_proposals__c" );
t1.Proposal_Name__c = "{!Opportunity.Name}" + "Proposal";
t1.Opportunity__c = "{!Opportunity.Id}";
var result = sforce.connection.create([t1]);
if (result[0].success=='false')
{
alert(result[0].errors.message);
}
else
{
location.reload(true);
} 
}catch(err)
{
alert(err);
}

 

 

this will work for you, let me know

 

Cheers,

Bala

 

 

 

SMasterSMaster

Hi Bala,

 

Its done sucessfully.... :)

 

Actually my requirement is something like... I need a button on the opportunity page layout.. as i click on that button, I should have two options...

 

<Option1> for Creating a new record in the related list (opportunity_proposals__c) of opportunity and go back to an opportunity page.  [Now this is happening on clicking of a button]

 

<Option2>   for Creating a new record in the related list (opportunity_proposals__c) of opportunity and go back to the new record created page (i.e. Record created in the related list)

 

Please suggest the way of doing this....

 

 

b-Forceb-Force

Give me some time .. we have an idea :)

b-Forceb-Force

Hey this is for you

 

{!requireScript("/soap/ajax/20.0/connection.js")}
{!requireScript("/soap/ajax/20.0/apex.js")}

try
{
var t1= new sforce.SObject("opportunity_proposals__c" );
t1.Proposal_Name__c = "{!Opportunity.Name}" + "Proposal";
t1.Opportunity__c = "{!Opportunity.Id}";
var result = sforce.connection.create([t1]);
if (result[0].success=='false')
{
alert(result[0].errors.message);
}
else
{

var r=confirm("Click on Ok to Navigate new Proposal record.");
if (r==true)
  {
  window.location="/"+result[0].id;
  }
else
  {
	location.reload(true);
  }
} 
}catch(err)
{
alert(err);
}

 

 

just copy and run above code. [you can update message as per your business need]

 

Cheers,

Bala

 

 

SMasterSMaster

Hi Bala,

 

You have just dropped me out of this trouble.... :)

 

Just wanted to know is there any way to change the Button Labels...  I mean in place of Ok.. if i want to write "See the newly created record" or something like it.....   I certainly don't know wheather this can be achieved or not.....

b-Forceb-Force

Oooops Its our Bad :(

 

It is not possible to change button text with confirm window , we need to stop here :(

 

Instead of thinking button labels, You can show more meaningfull  message on Confirm window. This is the only way 

 

 

Cheers,

Bala

SMasterSMaster

Okay Bala,

 

Thanks for confirmation.. even i doubted about it..

 

Thanks

b-Forceb-Force

Great..... :)