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
Terry411Terry411 

Redirecting Opportunity Save to Opportunity Contact Role

I feel like I'm very close to getting this to work.  What I'm trying to do it take the user to enter a Opportunity Contact Role page once they save the Opportunity.  To do so, I'm overriding the Opportunity View button.

 

I'm good with the redirct to the Opportunity Contact Role page but the standard Opportunity page is not coming up.  I get the following error:   

 

java.lang.IllegalArgumentException: Illegal view ID OpportunityRedirect?id=006M0000004ahc8. The ID must begin with /

 

Here's my code:

public with sharing class OpportunityRedirect {
	String oppId;

	public OpportunityRedirect(ApexPages.StandardController stdController) {
		//c = stdController;
		oppId = stdController.getId();
	}
        
	public PageReference redirect() {
		PageReference pageDetail = new PageReference ('/p/opp/ContactRoleEditUi/e?oppid=' + oppId + '&retURL=' + oppId);
		pageDetail.setRedirect(true);
		
		OpportunityContactRole[] ocr = [select ContactId, Role, isPrimary from OpportunityContactRole where OpportunityId = :oppId];
		system.debug('--------------------- ocr: ' + ocr);
		if (ocr.size() == 0) return pageDetail;
		
		//PageReference pageOpportunity = new PageReference('OpportunityRedirect/?id=' + oppId);
		PageReference pageOpportunity = new PageReference('OpportunityRedirect');
		pageOpportunity.getParameters().put('id',oppId);
		pageOpportunity.setRedirect(true);
		return pageOpportunity;
	}
}

 

<apex:page standardController="Opportunity" 
		extensions="OpportunityRedirect" action="{!redirect}">
	<apex:detail />
</apex:page>

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Terry411Terry411

Thank you...  that apparently fixed one issue but then I ended up with an infinite loop where the page never actually loaded.  To fix that, I created a second VF Page without the Action tag and send the user to that new page.  So below is the final working code.   If anyone has a smarter way to accomplish this, I'd love to hear from you.

 

 

Redirect page that the Opportunity View button redirects to:

<apex:page standardController="Opportunity" 
		extensions="OpportunityRedirect" action="{!redirect}">
	<apex:outputfield value"{!Opportunity.OwnerId/>
</apex:page>

 

New view page for the Opportunity which still leverages all the page layout functionality:

<apex:page standardController="Opportunity">
	<apex:detail />
</apex:page>

 

Here is the controller to bring it all together:

public with sharing class OpportunityRedirect {
	String oppId;
String ownerId;
private final Opportunity Oppty; public OpportunityRedirect(ApexPages.StandardController stdController) { oppId = stdController.getId();
Oppty = (Opportunity) stdController.getRecord();
ownerId = oppty.OwnerId; } public PageReference redirect() { PageReference pageDetail = new PageReference ('/p/opp/ContactRoleEditUi/e?oppid=' + oppId + '&retURL=' + oppId); pageDetail.setRedirect(true); OpportunityContactRole[] ocr = [select ContactId, Role, isPrimary from OpportunityContactRole where OpportunityId = :oppId]; if (ocr.size() == 0 && UserInfo.getUserId() == ownerId) return pageDetail; PageReference pageOpportunity = new PageReference('/apex/OpportunityView'); pageOpportunity.getParameters().put('id',oppId); pageOpportunity.setRedirect(true); return pageOpportunity; } }

 

(modified to add logic the redirect only the Opportunity Owner to the Contact Role page)

All Answers

Ritesh AswaneyRitesh Aswaney

Visualforce Pages are referenced as /apex/PageName

 

So the PageReference you're returning should read

 

PageReference pageOpportunity = new PageReference('/apex/OpportunityRedirect');
Terry411Terry411

Thank you...  that apparently fixed one issue but then I ended up with an infinite loop where the page never actually loaded.  To fix that, I created a second VF Page without the Action tag and send the user to that new page.  So below is the final working code.   If anyone has a smarter way to accomplish this, I'd love to hear from you.

 

 

Redirect page that the Opportunity View button redirects to:

<apex:page standardController="Opportunity" 
		extensions="OpportunityRedirect" action="{!redirect}">
	<apex:outputfield value"{!Opportunity.OwnerId/>
</apex:page>

 

New view page for the Opportunity which still leverages all the page layout functionality:

<apex:page standardController="Opportunity">
	<apex:detail />
</apex:page>

 

Here is the controller to bring it all together:

public with sharing class OpportunityRedirect {
	String oppId;
String ownerId;
private final Opportunity Oppty; public OpportunityRedirect(ApexPages.StandardController stdController) { oppId = stdController.getId();
Oppty = (Opportunity) stdController.getRecord();
ownerId = oppty.OwnerId; } public PageReference redirect() { PageReference pageDetail = new PageReference ('/p/opp/ContactRoleEditUi/e?oppid=' + oppId + '&retURL=' + oppId); pageDetail.setRedirect(true); OpportunityContactRole[] ocr = [select ContactId, Role, isPrimary from OpportunityContactRole where OpportunityId = :oppId]; if (ocr.size() == 0 && UserInfo.getUserId() == ownerId) return pageDetail; PageReference pageOpportunity = new PageReference('/apex/OpportunityView'); pageOpportunity.getParameters().put('id',oppId); pageOpportunity.setRedirect(true); return pageOpportunity; } }

 

(modified to add logic the redirect only the Opportunity Owner to the Contact Role page)

This was selected as the best answer
Terry411Terry411
@shoba shoba, here is a link to my blog post that I wrote about how to do this... http://hometeamconsulting.com/creative-way-capture-opportunity-contact-role/