1

My visualforce page:

<apex:page standardController="CampaignMember">
<apex:form >
<apex:outputPanel id="CmPage">
<apex:pageMessages />
<apex:messages />
<apex:pageBlock mode="edit">
<apex:pageBlockSection title="" columns="1">
<apex:inputfield value="{!Campaignmember.campaignid}" label="Campaign Name" id="cmName"/>
<apex:inputfield value="{!campaignmember.Notes__c}" label="{!$ObjectType.campaignmember.fields.Notes__c.label}" id="cmNotes__C"/>
<apex:inputField value="{!campaignmember.Contactid}" label="Contact"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!quickSave}" onclick="window.top.close()"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:outputPanel> 
</apex:form>
</apex:page>

Right now, this page is attached to a button on the Contact object:

/apex/CreateNewcm?save_new=1

Clicking this button takes you to the visualforce page, where entering a Campaign Name and a Contact Name and clicking "Save" creates a new Campaign Member.

But I shouldn´t need to enter the Contact Name; the name of the Contact whose page I´m on should be the name that appears on the visualforce page. The field should already be populated, so that it can be shown as a apex:outputfield value, or a pageBlockSection title.

My question is: is there a way to achieve this without the use of an extension? Can I add anything to the button´s code, that will allow it to retrieve the Contact ID?

1

You can use the merge value {!$CurrentPage.parameters.SomeParameter} to get a parameter from the url for the current page. Adding the contact id and name to the button is the first step:

/apex/CreateNewcm?save_new=1&ContactId={!Contact.Id}&ContactName={!URLENCODE(SUBSTITUTE(Contact.Name, ' ', '%20'))}

The easiest way to set this in by checking the value provided in the url in the constructor of your apex class, but with a little javascript we should be able to avoid using a controller directly.


Now, we the value in the page. We do this by first adding an id to the inputField.

<apex:inputField id="CampaignMemeberContactId" value="{!campaignmember.Contactid}" label="Contact"/>

We can't pass the value along directly, so we need to get creative. Adding a javascript snippet to set the value of the inputField we added an id to (with the _lkid addition to make sure we set the id input and not the name input) with the value of the {!CurrentPage.parameters.ContactId} merge field should set the value of the lookup field.

There seems to be a total of four fields which change whenever you modify a lookup: _lkid, _lkold, _mod, and the id'd component itself. The snippet below sets all four of these fields with the right value, so the user sees a populated lookup field.

This script needs to be placed after the field, or the {!Component.Id} will show as null.

<script>
        document.getElementById('{!$Component.CampaignMemberContactId}' + '_lkid').value = '{!$CurrentPage.parameters.ContactId}';
        document.getElementById('{!$Component.CampaignMemberContactId}' + '_lkold').value = '{!$CurrentPage.parameters.ContactName}';
        document.getElementById('{!$Component.CampaignMemberContactId}' + '_mod').value = 1;
        document.getElementById('{!$Component.CampaignMemberContactId}').value = '{!$CurrentPage.parameters.ContactName}';
    </script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy