Showing posts with label Campaigns. Show all posts
Showing posts with label Campaigns. Show all posts

Friday, January 6, 2012

Adding a default value for Campaign Standard Field

Scenario: Without needing to enter the Campaign Name field on the edit Campaign screen, have a trigger define the Campaign Name field based on a formula like criteria.


Problem:  The Name field is required on the Campaign, and saving the page layout fires a field validation rule before the before update trigger runs.  Furthermore, there is no option to provide a default value for the Name field unlike custom text fields.


Solution: Feed a dummy data value into the Campaign name through a custom Visualforce solution.  The solution involves overriding the New button on the Campaign and directing the user to the Campaign edit page with the Campaign name pre-populated, allowing the campaign to save and fire the trigger.


Step 1) Create a Controller extension to prepopulate the Campaign Name:


Apex controller extension (CampaignExtension.cls):
public with sharing class CampaignExtension {

  public CampaignExtension(ApexPages.StandardController stdController) { 
  }
  
  public PageReference gotoUrl(){
    PageReference pageRef = new PageReference('/701/e');
    Map params = pageRef.getParameters();
    params.put('nooverride','1');
    params.put('RecordType',recordType);
    //See notes below
    params.put('cpn1','Campaign Name');
    return pageRef;
  }
}



** Notes:  
The 'cpn1' parameter is related to whichever field to default.
Viewing the HTML source of the Campaign edit screen reveals that the HTML id of the Name field is 'cpn1', hence the cpn1 parameter for the Campaign Name field.

Step 2) Create a VF page:


<apex:page standardController="Campaign" 
  extensions="CampaignExtension" 
  action="{!gotoUrl}" />

Step 3) Override standard new button for Campaign:
  1. Setup->Customize->Campaigns->Buttons and Links
  2. Click Edit next the standard New button
  3. Select the Visualforce Page radio button and select the Visualforce page from Step 2 above.
  4. Validate that "Skip Record Type Selection Page" is unchecked. 
  5. Save the override properties
Wa-la, you should have a mechanism to set the default value for a required standard field on a standard object.






Thursday, March 3, 2011

Leads, Campaigns, and Workflow

I have a requirement for a client that wishes to create a Web 2 Lead form and assign it to a campaign.
Once that is done, a workflow shall fire off a series of emails depending on what sort of campaign the lead is tied to.  This sounds easy enough to do with Salesforce's out of the box Web2Lead functionality and workflow, but there were some other tweaks we needed, so utilizing our own solution is the only way to go.

- The form -
Utilizing a jQuery modal, the user clicks a link and populates a small web to lead form.  When the user hits submit, sever side validation happens in a VF controller, so we can control the UI (messages and labels if a field is required for instance). 

- Allowing Portal users to use the web 2 lead form -
Once the form is submitted, it stores the lead in a custom lead object.  The custom lead object is needed, since leads cannot be created (owned) by Portal users.  When a portal user creates the mirrored lead object, it fires off a trigger calling an @future method to insert the lead and campaign member.

- Tricky workflows with Leads and Campaigns -
Here's the tricky part of the workflow.  When the lead is inserted, it doesn't have access to its related Campaign member and hence related Campaign (the Campaign member is a junction object to connect Campaigns to leads or contacts).  Our workflow needed to look at the Campaign Name field on the Campaign.  The lead doesn't have this when it is inserted, nor if the Campaign member is updated, does it update the lead.  Therefore, modifying the campaign member or campaign doesn't fire the workflow rule until the lead is updated next.  This is true even if the workflow rule is set to 'Created or did not previously meet the criteria'.

The solution is to create the workflow rule on the Campaign member and not the Lead.  When the Campaign Member junction is created, it should have access to both its Campaign and its Lead objects.  Thus, when the member is created, we can create a rule like 'Campaign Name equals My Name' and fire off a workflow when the Lead is assigned to the particular campaign.

Hope this helps others.