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
labaulabau 

Opportunity Contact Role will not save

Hello All:

 

I'm trying to create an Opportunity detail page in which users may edit related data without going to a separate page. In this instance, I want them to be able to edit Contact Roles. I have written a VF page that looks like this:

 

 

<apex:page standardController="Opportunity" tabStyle="Opportunity"> <apex:detail /> <apex:form > <apex:pageBlock title="Change Roles" mode="detail"> <apex:pageMessages /> <apex:pageBlockButtons location="top"> <apex:commandButton value="Save" action="{!save}"/> <apex:commandButton value="Cancel" action="{!cancel}"/> </apex:pageBlockButtons> <apex:pageBlockTable value="{!Opportunity.OpportunityContactRoles}" var="OC"> <apex:column > <apex:inputfield value="{!OC.Contact.Firstname}"/> </apex:column> <apex:column > <apex:inputfield value="{!OC.Contact.Lastname}"/> </apex:column> <apex:column > <apex:inputfield value="{!OC.IsPrimary}"/> </apex:column> <apex:column > <apex:inputfield value="{!OC.Role}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>

 However, when I click "Save," none of the information seems to take. What am I missing?

 

Any help would be appreciated!


Thanks,


Josh

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
There's a new type of controller called a StandardSetController. You can use this controller to manipulate a list of records at once. You can reference the Salesforce Apex Language Reference guide on page 293 for additional information. Perhaps most importantly, it has a single "Save" command, which is used to update or insert all updated records in the current set. Alternatively, you can write out your own controller extension to do this; using the set controller is far easier, however. Either way, you will definitely need to write a controller extension, as standard controllers only operate on the object they're designed for. In your case, for example, you used the Opportunity Standard Controller, meaning you can only modify an opportunity, not any related records to the opportunity (including contact roles).

All Answers

mattdarnoldmattdarnold

Hi Josh,

 

I had to do something like this recently. My understanding is that the standard save function doesn't provide the functionality to save the updates to a related object. For this you'll have to use a controller extension. You could override the save function to add in logic to update / insert the OpportunityContactRole records then leverage controller.save() (assuming you've setup the page with a variable called controller) to save any changed opportunity fields and to get the page reference of the updated opportunity. Take a look at the following code snippet to get you started:

 

 

// Setup the standard controller and class variable for Opportunity
Opportunity opp;
ApexPages.StandardController controller;

public OppEditExtController(ApexPages.StandardController stdController){
this.opp = (Opportunity)stdController.getRecord();
this.controller = stdController;
}

public PageReference save() {

// Write some custom logic here to save the OpportunityContactRole records

PageReference p = controller.save();
// Any other logic you need to add to the page post save

return p;

}

 

 

 

sfdcfoxsfdcfox
There's a new type of controller called a StandardSetController. You can use this controller to manipulate a list of records at once. You can reference the Salesforce Apex Language Reference guide on page 293 for additional information. Perhaps most importantly, it has a single "Save" command, which is used to update or insert all updated records in the current set. Alternatively, you can write out your own controller extension to do this; using the set controller is far easier, however. Either way, you will definitely need to write a controller extension, as standard controllers only operate on the object they're designed for. In your case, for example, you used the Opportunity Standard Controller, meaning you can only modify an opportunity, not any related records to the opportunity (including contact roles).
This was selected as the best answer
labaulabau
Thanks - this is a big help! I'm new to the platform and this makes much more sense.
ChazDadChazDad

Awesome Stuff