You need to sign in to do that
Don't have an account?
Denise Crosby
visualforce event to create dynamic SOSL creating event using custom controller
Hello. I am creating a custom controller to insert an event. I understand it has to be a custom controller so it can be fired from a global action on Salesforce1. I need Visualforce to fire an event when either the Contact (whoid) or Account (whatid) is selected on the page so that I can dynamically create the SOSL to restrict the Contact or Account to those that are related to each other. Users should not be able to select a Contact unrelated to an Account. How can I do this? Thanks so much.
VisualForce:
<apex:page controller="NewEvent">
<apex:form >
<apex:pageBlock title="Event Details">
<apex:pageMessages />
<apex:pageBlockSection title="Event Information">
<apex:inputfield value="{!ev.subject}"/>
<apex:inputfield value="{!ev.description}"/>
<apex:inputfield value="{!ev.startdatetime}"/>
<apex:inputfield value="{!ev.enddatetime}"/>
<apex:inputfield value="{!ev.whoid}"/>
<apex:inputfield value="{!ev.whatid}"/>
</apex:pageBlockSection>
<apex:pageBlockbuttons >
<apex:commandbutton value="Save" action="{!save}"/>
</apex:pageBlockbuttons>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex:
public class NewEvent {
public event ev { get; set; }
public newevent (){
ev = new event();
ev.CurrencyIsoCode = UserInfo.getDefaultCurrency();
ev.OwnerId = UserInfo.getUserId();
ev.StartDateTime = Datetime.now().addMinutes(-Datetime.now().minute());
ev.EndDateTime = Datetime.now().addMinutes(60-Datetime.now().minute());
}
public PageReference save() {
return new ApexPages.StandardController(ev).save();
}
}
VisualForce:
<apex:page controller="NewEvent">
<apex:form >
<apex:pageBlock title="Event Details">
<apex:pageMessages />
<apex:pageBlockSection title="Event Information">
<apex:inputfield value="{!ev.subject}"/>
<apex:inputfield value="{!ev.description}"/>
<apex:inputfield value="{!ev.startdatetime}"/>
<apex:inputfield value="{!ev.enddatetime}"/>
<apex:inputfield value="{!ev.whoid}"/>
<apex:inputfield value="{!ev.whatid}"/>
</apex:pageBlockSection>
<apex:pageBlockbuttons >
<apex:commandbutton value="Save" action="{!save}"/>
</apex:pageBlockbuttons>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex:
public class NewEvent {
public event ev { get; set; }
public newevent (){
ev = new event();
ev.CurrencyIsoCode = UserInfo.getDefaultCurrency();
ev.OwnerId = UserInfo.getUserId();
ev.StartDateTime = Datetime.now().addMinutes(-Datetime.now().minute());
ev.EndDateTime = Datetime.now().addMinutes(60-Datetime.now().minute());
}
public PageReference save() {
return new ApexPages.StandardController(ev).save();
}
}
<pre>
<apex:page standardController="Event" extensions="NewEventController">
<apex:form >
<apex:pageBlock title="Event Details">
<apex:pageMessages />
<apex:pageBlockSection title="Event Information" id="eventInformation">
<apex:inputfield value="{!Event.Subject}"/>
<apex:inputfield value="{!Event.Description}"/>
<apex:inputfield value="{!Event.StartDateTime}"/>
<apex:inputfield value="{!Event.EndDateTime}"/>
<apex:inputfield label="{!accountLabel}" value="{!dummyContact.AccountId}">
<apex:actionSupport action="{!changeAccount}" event="onchange" rerender="eventInformation" />
</apex:inputField>
<apex:selectList label="{!contactLabel}" value="{!contactId}" size="1" rendered="{!showContactOptions}">
<apex:selectOptions value="{!contactOptions}" />
<apex:actionSupport action="{!changeContact}" event="onchange" rerender="eventInformation" />
</apex:selectList>
<apex:pageBlockSectionItem rendered="{!!showContactOptions}"/>
</apex:pageBlockSection>
<apex:pageBlockbuttons >
<apex:commandbutton value="Save" action="{!save}"/>
<apex:commandbutton value="Cancel" action="{!cancel}"/>
</apex:pageBlockbuttons>
</apex:pageBlock>
</apex:form>
</apex:page>
public class NewEventController
{
private Event event;
public String accountLabel
{
get { return Account.sObjectType.getDescribe().getLabel(); }
}
public String contactLabel
{
get { return Contact.sObjectType.getDescribe().getLabel(); }
}
public Contact dummyContact
{
get
{
if ( dummyContact == null ) dummyContact = new Contact();
return dummyContact;
}
private set;
}
public List<SelectOption> contactOptions
{
get
{
if ( contactOptions == null && dummyContact.AccountId != null )
{
contactOptions = new List<SelectOption>();
contactOptions.add( new SelectOption( 'null', '--None--' ) );
for ( Contact contact :
[ SELECT Id, Name
FROM Contact
WHERE AccountId = :dummyContact.AccountId
]
)
{
contactOptions.add( new SelectOption( contact.Id, contact.Name ) );
}
}
return contactOptions;
}
private set;
}
public Boolean showContactOptions
{
get { return contactOptions != null && contactOptions.size() > 1; }
}
public String contactId { get; set; }
public NewEventController( ApexPages.StandardController controller )
{
event = (Event) controller.getRecord();
event.CurrencyIsoCode = UserInfo.getDefaultCurrency();
event.OwnerId = UserInfo.getUserId();
DateTime now = Datetime.now();
event.StartDateTime = now.addMinutes( -now.minute() ).addSeconds( -now.second() );
event.EndDateTime = event.StartDateTime.addMinutes( 60 );
if ( event.WhatId != null && event.WhatId.getSObjectType() == Account.sObjectType )
{
dummyContact.AccountId = event.WhatId;
}
}
public void changeAccount()
{
event.WhatId = dummyContact.AccountId;
event.WhoId = null;
contactOptions = null;
}
public void changeContact()
{
event.WhoId =
( ( contactId != null && contactId != 'null' )
? Id.valueOf( contactId )
: null
);
}
}
</pre>
All Answers
The idea is to use an actionSupport tag to call an action method in your controller when the What value changes (onchange). It would rerender that part of the page to reveal the "Who" field, which is actually a picklist of the Contacts related to the selected Account. On save, the controller would have to populate the actual WhoId field based on the selected Contact. It sounds complicated, but isn't really. Let me know if this might work for you and I'll see if I have time to write some code for you.
Thanks for this information. Yes, that should work fine. If you could share some code snippets or examples, that would be very helpful to me. Thank you so much!
Denise
<pre>
<apex:page standardController="Event" extensions="NewEventController">
<apex:form >
<apex:pageBlock title="Event Details">
<apex:pageMessages />
<apex:pageBlockSection title="Event Information" id="eventInformation">
<apex:inputfield value="{!Event.Subject}"/>
<apex:inputfield value="{!Event.Description}"/>
<apex:inputfield value="{!Event.StartDateTime}"/>
<apex:inputfield value="{!Event.EndDateTime}"/>
<apex:inputfield label="{!accountLabel}" value="{!dummyContact.AccountId}">
<apex:actionSupport action="{!changeAccount}" event="onchange" rerender="eventInformation" />
</apex:inputField>
<apex:selectList label="{!contactLabel}" value="{!contactId}" size="1" rendered="{!showContactOptions}">
<apex:selectOptions value="{!contactOptions}" />
<apex:actionSupport action="{!changeContact}" event="onchange" rerender="eventInformation" />
</apex:selectList>
<apex:pageBlockSectionItem rendered="{!!showContactOptions}"/>
</apex:pageBlockSection>
<apex:pageBlockbuttons >
<apex:commandbutton value="Save" action="{!save}"/>
<apex:commandbutton value="Cancel" action="{!cancel}"/>
</apex:pageBlockbuttons>
</apex:pageBlock>
</apex:form>
</apex:page>
public class NewEventController
{
private Event event;
public String accountLabel
{
get { return Account.sObjectType.getDescribe().getLabel(); }
}
public String contactLabel
{
get { return Contact.sObjectType.getDescribe().getLabel(); }
}
public Contact dummyContact
{
get
{
if ( dummyContact == null ) dummyContact = new Contact();
return dummyContact;
}
private set;
}
public List<SelectOption> contactOptions
{
get
{
if ( contactOptions == null && dummyContact.AccountId != null )
{
contactOptions = new List<SelectOption>();
contactOptions.add( new SelectOption( 'null', '--None--' ) );
for ( Contact contact :
[ SELECT Id, Name
FROM Contact
WHERE AccountId = :dummyContact.AccountId
]
)
{
contactOptions.add( new SelectOption( contact.Id, contact.Name ) );
}
}
return contactOptions;
}
private set;
}
public Boolean showContactOptions
{
get { return contactOptions != null && contactOptions.size() > 1; }
}
public String contactId { get; set; }
public NewEventController( ApexPages.StandardController controller )
{
event = (Event) controller.getRecord();
event.CurrencyIsoCode = UserInfo.getDefaultCurrency();
event.OwnerId = UserInfo.getUserId();
DateTime now = Datetime.now();
event.StartDateTime = now.addMinutes( -now.minute() ).addSeconds( -now.second() );
event.EndDateTime = event.StartDateTime.addMinutes( 60 );
if ( event.WhatId != null && event.WhatId.getSObjectType() == Account.sObjectType )
{
dummyContact.AccountId = event.WhatId;
}
}
public void changeAccount()
{
event.WhatId = dummyContact.AccountId;
event.WhoId = null;
contactOptions = null;
}
public void changeContact()
{
event.WhoId =
( ( contactId != null && contactId != 'null' )
? Id.valueOf( contactId )
: null
);
}
}
</pre>
On the page, replace 'Event.xxx' with '{!event.xxx}'; and in the apex:page tag, replace the 'standardController' and 'extensions' attributes with 'controller="NewEventController"', similar to what you had originally.
I think I'm out of my league with this one. I am trying to add my constructor to your code, so I can use it as either a custom controller or a controller extension. In this case, I would need two pages? One for overriding the NewEvent action, and another for the mobile global action? I tried following your instructions, but it does not compile. There's probably something simple that I'm missing. Can you help me again? I really appreciate it.
(Also, sorry for the delay but I just got my power back after the Harvey storm).
I was able to get this working as a custom controller. Thank you!
I was hoping you might be available to offer advice with my code. I have a lot of your code that I'm using and I've made a few modifications. The page is already in production and I'm training our executives on it next week. The page has some strange behavior in SF1 though and I'm not sure how to solve them. Would you have any availablility to assist? I would greatly appreciate it.