I currently have an Apex class that pulls the data from a record of one custom object 'property__c' and uses it to create a record of another object 'proposal__c' and then redirects the user to this new record's edit view.
However, there's a problem with this. On the edit page, if the user hits "cancel," the record is still inserted. This is because in order to direct the user to the edit view of the new record, we have to first insert that record so that we can pull its id.
Outside of making a custom edit view for this object, is it possible to send the user to the edit view without inserting the record first? This would ideally behave the same as if the user simply hit the "new" button and then "cancel." The only difference is, we want some of the fields of the record to be pre-populated.
Here is the code. You can see where it redirects the user at the bottom of the convert method.
public class ControllerCreateProposalView {
public Id propertyId;
public ControllerCreateProposalView(ApexPages.StandardController stdController){
propertyId = ApexPages.CurrentPage().getParameters().get('id');
}
public PageReference convert(){
PageReference pref;
Property__c property = [
select
Id,
Name,
OwnerId,
Primary_Contact__c,
from Property__c
where Id = :propertyId limit 1
];
Proposal__c proposal = new Proposal__c(
Name = property.Name,
OwnerId = property.ownerid,
Property__c = property.Id,
Client__c = property.Primary_Contact__c,
);
insert proposal;
String sServerName = ApexPages.currentPage().getHeaders().get('Host');
sServerName = 'https://'+sServerName+'/';
String editName='/e?retURL=%2F'+proposal.Id;
pref = new PageReference(sServerName + proposal.Id+editName);
pref.setRedirect(true);
return pref;
}
public PageReference back(){
PageReference pref = new PageReference('/' + propertyId);
pref.setRedirect(true);
return pref;
}
}
EDIT:
I can send the user to the default edit page, which is /xxxx.salesforce.com/a0r/e?retURL=%2Fa0r%2Fo
and I can even pass in values for standard fields, such as "name"
/xxxx.salesforce.com/a0r/e?name=TEST&retURL=%2Fa0r%2Fo
But I cannot pass custom fields. If I could pass custom fields in using this method, I believe I could achieve what I want to do.