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
phyerlightphyerlight 

Edit SObject/Opportunity Before Insert

Hi All,

 

Hopefully someone can help me out with this one. I'm trying to create a new opportunity, in apex with some fields set in code and then pass that object to the default salesforce opportunity new/edit page.

 

A bit more information: I'm trying to create a set of specialized (certain subset of fields) cloning processes. I have a custom button on the opp view page that links to a custom VF page that allows the user to select the type of clone they want and then carry out the cloning. After cloning, the customer would like to be directed to the edit page. If I insert the clone into the database, and create a standardcontroller and use the edit method:

insert newOpp;
return (new ApexPages.StandardController(newOpp)).edit();

This works.

 

This is where I kind of start to run into trouble. There are a number of validation rules that exist on the opportunity object and so sometimes validation fails. This means that in order to be able to clone that opportunity, the user either needs to change the original opp (highly undesirable) or I some how edit the opportunity that was cloned before inserting it into the database. When I try to get an edit page for the new opp like I did above, all I get is the edit page for the original opportunity, not the cloned one. I'm assuming this is because the edit page requires an ID to function properly. So does anyone know how to use the new opportunity page for editing an opportunity in this kind of situation?

 

Thank you all for your help,

 

Kevin

Ispita_NavatarIspita_Navatar

I would suggest you return a simple PageReference variable after insertion of the clone opportunity.


Try the following :-


PageReference pageRef = new PageReference('<partialURL>');

Set partialURL to '/' + <recordID> +'/' + '/e?retURL='+<recordID>
Here <recordID>= newOpp.id;

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.


phyerlightphyerlight

Hi Ispita_Navatar, that only works if the object was successfully inserted into the database. The problem is that sometimes it is not due to validation errors. In this situation, the object's ID is null and so the approach you suggest does not work.