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
Janis Davis 22Janis Davis 22 

Customize the Contact Roles related list fields on Opportunities with VF - Need to add Delete

I recently posted a question about the VF code for customizing the Opportunity Contact Roles to show additional Contact fields. Karanrajs very graciously fixed my code to show what I needed (thread like below)

https://developer.salesforce.com/forums/?id=906F0000000kB3NIAU

I now need to add the Del function to my VF Contact Roles page. I tried playing around with code a bit and I got the Del link to show up, however it gives the URL not found error. 

User-added image

Any help or advice will be most appreciated!

Thanks!
RbnRbn
Hi Janis,

Please find the below Vf Page & the associated Controller class:

Vf Page:

<apex:page standardController="Opportunity" extensions="DeletePage" id="opfrm" >
    <apex:form id="form">
   
        <apex:pageBlock title="Contact Roles">
            <apex:pageBlockTable value="{!opt}" var="ContactRole" id="pgblktab">
                <apex:column >
                    <apex:facet name="header">
                        Action
                    </apex:facet>
                    <a href="/p/opp/ContactRoleEditUi/e?oppid={!Opportunity.Id}&retURL=%2F{!Opportunity.Id}" target="_top">Edit</a>  |
                 <a href="javascript:if (window.confirm('Are you sure?')) Deleteopt('{!ContactRole.Id}');" style="font-weight:bold">Del</a>
                   </apex:column>
                <apex:column >
                    <apex:facet name="header"> Contact Name </apex:facet>
                    <a href="/{!ContactRole.Contact.Id}" target="_top">{!ContactRole.Contact.Name} </a>
                </apex:column>
               <!-- <apex:column value="{!ContactRole.Contact.Account.Name}" headerValue="Account Name"/>
                <apex:column value="{!ContactRole.Contact.Email}" headerValue="Email"/>
                <apex:column value="{!ContactRole.Contact.Phone}" headerValue="Phone"/>
                <apex:column value="{!ContactRole.Role}" headerValue="Role"/>
                <apex:column value="{!ContactRole.Contact.Account.BillingCity},{!ContactRole.Contact.Account.BillingState},{!ContactRole.Contact.Account.BillingCountry}" headerValue="Bill To"/>
                <apex:column value="{!ContactRole.Contact.MailingCity},{!ContactRole.Contact.MailingState},{!ContactRole.Contact.MailingCountry}" headerValue="Ship To"/>                -->
                <apex:column value="{!ContactRole.IsPrimary}" headerValue="Is Primary"/>
            </apex:pageBlockTable>   
            <apex:pageBlockButtons location="top">
                <input type="button" class="btn" value="New" onclick="javascript:window.open('/p/opp/ContactRoleEditUi/e?oppid={!Opportunity.Id}&retURL=%2F{!Opportunity.Id}');"/>
            </apex:pageBlockButtons>
           
        </apex:pageBlock>   
          <apex:actionFunction action="{!Deleteopt}" name="Deleteopt" reRender="form">
         <apex:param name="OpportunityContactRoles" value="" assignTo="{!SelectedoptId}"/>
        </apex:actionFunction> 
       
    </apex:form>
</apex:page>


Controller:

public with sharing class DeletePage {

   public List<OpportunityContactRole> opt{ get; set; }
  
 
   public string SelectedoptId { get; set; }
   public DeletePage(ApexPages.StandardController controller) {
    LoadData();
    }

   private void LoadData() {
       opt= [Select id,ContactId,IsPrimary,Contact.Name from OpportunityContactRole limit 20];
          }
  
   public pagereference Deleteopt()
   {
      // if for any reason we are missing the reference 
      if (SelectedoptId == null) {
      
         return null;
      }
     
      OpportunityContactRole  tobeDeleted = null;
      for(OpportunityContactRole a : opt)
       if (a.Id == SelectedoptId ) {
          tobeDeleted = a;
          System.debug('#############'+ a);
          break;
       }
      
      if (tobeDeleted != null) {
      Delete tobeDeleted;
      

      }
      LoadData();
      return null;
   } 
 
}

Let me know if it resolves your query.

Rgrds,
Rabi
Janis Davis 22Janis Davis 22
Hi Rabi, 

Thanks so much for your help! I created the Controller using you exact code and modified the code in my VF Contact Roles page.The fields we're pulling though in the VF Contact Roles related list changed a bit from the original version. We basically removed the Account Billing and Contact Shipping fields and added the Mobile Phone. I only modified that part of the VF code you gave me, but I could have easily messed something up in your VF code. Pasted below is your VF code I modified for the new related list field views, as well as the error messages I'm getting when I try to save the updated VF page. Can you please take a look to see if you can identify what I'm doing wrong? 

Thanks!

Janis

VF Page:
<apex:page standardController="Opportunity" extensions="DeletePage" id="opfrm" >
    <apex:form id="form">
   
        <apex:pageBlock title="Contact Roles">
            <apex:pageBlockTable value="{!opt}" var="ContactRole" id="pgblktab">
                <apex:column >
                    <apex:facet name="header">
                        Action
                    </apex:facet>
                    <a href="/p/opp/ContactRoleEditUi/e?oppid={!Opportunity.Id}&retURL=%2F{!Opportunity.Id}" target="_top">Edit</a>  |
                 <a href="javascript:if (window.confirm('Are you sure?')) Deleteopt('{!ContactRole.Id}');" style="font-weight:bold">Del</a>
                   </apex:column>
                <apex:column >
                    <apex:facet name="header"> Contact Name </apex:facet>
                    <a href="/{!ContactRole.Contact.Id (http://contactrole.contact.id/" target="_blank)}" target="_top">{!ContactRole.Contact.Name (http://contactrole.contact.name/" target="_blank)} </a>
                </apex:column>
               <!-- <apex:column value="{!ContactRole.Contact.Account.Name (http://contactrole.contact.account.name/" target="_blank)}" headerValue="Account Name"/>
                <apex:column value="{!ContactRole.Contact.Email}" headerValue="Email"/>
                <apex:column value="{!ContactRole.Contact.Phone}" headerValue="Phone"/>
<apex:column value="{!ContactRole.Contact.MobilePhone}" headerValue="Mobile"/>
                <apex:column value="{!ContactRole.Role}" headerValue="Role"/>
             <apex:column value="{!ContactRole.IsPrimary}" headerValue="Is Primary"/>
            </apex:pageBlockTable>   
            <apex:pageBlockButtons location="top">
                <input type="button" class="btn" value="New" onclick="javascript:window.open('/p/opp/ContactRoleEditUi/e?oppid={!Opportunity.Id}&retURL=%2F{!Opportunity.Id}');"/>
            </apex:pageBlockButtons>      
        </apex:pageBlock>   
          <apex:actionFunction action="{!Deleteopt}" name="Deleteopt" reRender="form">
         <apex:param name="OpportunityContactRoles" value="" assignTo="{!SelectedoptId}"/>
        </apex:actionFunction>        
    </apex:form>
</apex:page>

VF Error Page Message
User-added image
RbnRbn

Hi janis,

Sorry for replying a bit late.Struck with work.:-)

The reason why you are getting this error is you have commented the below line of code in the vf page.

<!-- <apex:column value="{!ContactRole.Contact.Account.Name (http://contactrole.contact.account.name/" target="_blank)}" headerValue="Account Name"/>

but there is no end tag .specified.Please add --> at the end of the line.and it would resolve the issue.


Let me know if it helps u.
 
Janis Davis 22Janis Davis 22
No problem whatsoever Rabi. I'm happy to n.get help from developer gurus like you whenever I possibly can. 

OK, I did what you recommended and I'm getting the following syntax error, but it doen't tell me where the error is in the code (screenshot below). I'm also pasting the VFcode I modified with your suggestion in case I did something wrong. 
User-added image

Modified VF Code
<apex:page standardController="Opportunity" extensions="DeletePage" id="opfrm" >
    <apex:form id="form">   
        <apex:pageBlock title="Contact Roles">
            <apex:pageBlockTable value="{!opt}" var="ContactRole" id="pgblktab">
                <apex:column >
                    <apex:facet name="header">
                        Action
                    </apex:facet>
                    <a href="/p/opp/ContactRoleEditUi/e?oppid={!Opportunity.Id}&retURL=%2F{!Opportunity.Id}" target="_top">Edit</a>  |
                 <a href="javascript:if (window.confirm('Are you sure?')) Deleteopt('{!ContactRole.Id}');" style="font-weight:bold">Del</a>
                   </apex:column>
                <apex:column >
                    <apex:facet name="header"> Contact Name </apex:facet>
                    <a href="/{!ContactRole.Contact.Id (http://contactrole.contact.id/" target="_blank)}" target="_top">{!ContactRole.Contact.Name (http://contactrole.contact.name/" target="_blank)} </a>
                </apex:column>
               <!-- <apex:column value="{!ContactRole.Contact.Account.Name (http://contactrole.contact.account.name/" target="_blank)}" headerValue="Account Name"/>
                <apex:column value="{!ContactRole.Contact.Email}" headerValue="Email"/>
                <apex:column value="{!ContactRole.Contact.Phone}" headerValue="Phone"/>
                <apex:column value="{!ContactRole.Contact.MobilePhone}" headerValue="Mobile"/>
                <apex:column value="{!ContactRole.Role}" headerValue="Role"/>       -->
             <apex:column value="{!ContactRole.IsPrimary}" headerValue="Is Primary"/>
            </apex:pageBlockTable>   
            <apex:pageBlockButtons location="top">
                <input type="button" class="btn" value="New" onclick="javascript:window.open('/p/opp/ContactRoleEditUi/e?oppid={!Opportunity.Id}&retURL=%2F{!Opportunity.Id}');"/>
            </apex:pageBlockButtons>      
        </apex:pageBlock>   
          <apex:actionFunction action="{!Deleteopt}" name="Deleteopt" reRender="form">
         <apex:param name="OpportunityContactRoles" value="" assignTo="{!SelectedoptId}"/>
        </apex:actionFunction>        
    </apex:form>
</apex:page>

I also tried testing the original VF code you posted before I change the fields to be visible. The page saved and the Del link showed up and worked, however the fields that were to be visible didn't show up (screenshot below).



Janis Davis 22Janis Davis 22
User-added image
RbnRbn
Hi Janis,

Just Copy paste the below code.if you want to add any additional column, just let me know.or else i have modified the code and its error free.
 
<apex:page standardController="Opportunity" extensions="DeletePage" id="opfrm" >
    <apex:form id="form">
   
        <apex:pageBlock title="Contact Roles">
            <apex:pageBlockTable value="{!opt}" var="ContactRole" id="pgblktab">
                <apex:column >
                    <apex:facet name="header">
                        Action
                    </apex:facet>
                    <a href="/p/opp/ContactRoleEditUi/e?oppid={!Opportunity.Id}&retURL=%2F{!Opportunity.Id}" target="_top">Edit</a>  |
                 <a href="javascript:if (window.confirm('Are you sure?')) Deleteopt('{!ContactRole.Id}');" style="font-weight:bold">Del</a>
                   </apex:column>
                <apex:column >
                    <apex:facet name="header"> Contact Name </apex:facet>
                    <a href="/{!ContactRole.Contact.Id}" target="_top">{!ContactRole.Contact.Name} </a>
                </apex:column>
                <apex:column value="{!ContactRole.Contact.Account.Name}" headerValue="Account Name"/>
                <apex:column value="{!ContactRole.Contact.Email}" headerValue="Email"/>
                <apex:column value="{!ContactRole.Contact.Phone}" headerValue="Phone"/>
               <!-- <apex:column value="{!ContactRole.Role}" headerValue="Role"/>
                <apex:column value="{!ContactRole.Contact.Account.BillingCity},{!ContactRole.Contact.Account.BillingState},{!ContactRole.Contact.Account.BillingCountry}" headerValue="Bill To"/>
                <apex:column value="{!ContactRole.Contact.MailingCity},{!ContactRole.Contact.MailingState},{!ContactRole.Contact.MailingCountry}" headerValue="Ship To"/>                -->
                <apex:column value="{!ContactRole.IsPrimary}" headerValue="Is Primary"/>
            </apex:pageBlockTable>   
            <apex:pageBlockButtons location="top">
                <input type="button" class="btn" value="New" onclick="javascript:window.open('/p/opp/ContactRoleEditUi/e?oppid={!Opportunity.Id}&retURL=%2F{!Opportunity.Id}');"/>
            </apex:pageBlockButtons>
           
        </apex:pageBlock>   
          <apex:actionFunction action="{!Deleteopt}" name="Deleteopt" reRender="form">
         <apex:param name="OpportunityContactRoles" value="" assignTo="{!SelectedoptId}"/>
        </apex:actionFunction> 
       
    </apex:form>
</apex:page>

 
Janis Davis 22Janis Davis 22
Hi Rabi,

Now I really feel like a pest. I entered the code in the VF page exactly as you gave it to me here and now I seem to be having a problem with the SOQL query on the Contact.Account field that shows up then I go to view the VF Contact Roles on the Opportunity (screenshot below). I did a little bit of search on the error message and it seems to have something to do with the Controller code, but that's as far as I could get with my very limited knowledge of VF and Apex. 

If you get a chance, please let me know what I need to do to correct this. 

User-added image
Thanks!!

Janis 
RbnRbn
Hi Janis,

Sorry for the late reply. As usual stuck up with work :-)

Please update the below line of code in controller for private void LoadData() method:

opt= [Select id,ContactId,Contact.Account.Name,Contact.Phone,IsPrimary,Contact.Name,Contact.Email from OpportunityContactRole limit 20]; // Line # 8 in controller

This will resolve your above shown issue.

Regards,
Rabi