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
KeerthigeeKeerthigee 

How to access the Campaign field from lead object to visualforce

Hi All,

I want to access campaign field from lead object  to visualforce using custom controller. For this,is the right way to write trigger? Suppose If I write trigger then how to call trigger from visualforce?

Kindly support and suggest.

Thank You.
Best Answer chosen by Keerthigee
VempallyVempally
Hi keerthigee...

create a handler in the constructor..

write this in constructor...
campmem = new CampaignMember();

All Answers

ShashForceShashForce
You cannot call a trigger from a visualforce custom controller. You can write a SOQL query in your custom controller (or preferably a controller extension) to get the fields on lead object.
KeerthigeeKeerthigee
Shashank,

Thank you for your immediate suggestion, which is useful.

Thanks.
KeerthigeeKeerthigee
Shashank,

Can you explain how to write soql query to acess campaign field from lead object using extensions.

Awaiting for your response.

Thanks.
VempallyVempally
Hi Keerthigee...

Here Campaign and Lead are not directly related...

we need to use the junction object CampaignMembers to get the field displayed....

Even when you save it check that you save as the code given below...


VF:

<apex:inputField id="if_lead_firstname" value="{!mylead.firstname}" label="First Name"/>
<apex:inputField id="if_lead_campaign" value="{!campmem.campaignID}" label="Campaign"/>

public Lead mylead { get; set; }
public CampaignMember campmem {get; set;}

public PageReference save() {
        insert mylead;
       campmem.leadid = mylead.id;
        insert campmem;
        mylead.clear();
       
        return null;
    }

KeerthigeeKeerthigee
Hi,

Thanks for your response.

 For junction object,We can't create a master-detail relationship with lead object.

Then,how to create this relationship?

Kindly support and suggest.
VempallyVempally
Hi Keerthigee

you need not create any master detail relationship...it already exixts...

1 Campaign ----- n Leads --- n leads can arise from 1 campaign...
n Campaigns ---- 1 Lead --- n campaigns may give rise to 1 lead...

here exixts a many to many relationship...which is achieved through a junction object called " Campaign Members "...

Use the above given code to get the field
KeerthigeeKeerthigee
Hi ,

Thanks for your response.

I got  this point yesterday

With the help of your points,I displayed campaign label but not  textfield.


This is my visualforce page.check campaign field in it once?

what I want to do next.

Kindly support and suggest.





VempallyVempally
Plz paste the related code of both vf and controller...
KeerthigeeKeerthigee
Hi,

Thanks for your response.

Vf:

<apex:page standardcontroller="Lead" tabstyle="lead" extensions="campaignacess">
<b>my name is "{!$User.FirstName}"</b>
<apex:form >
<apex:pageBlock title="Lead Edit" tabstyle="contact">
<apex:pageblockbuttons >
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!Cancel}" value="cancel"/>
</apex:pageblockbuttons>
<apex:pageBlockSection title="Lead Information" columns="2">
<apex:inputfield value="{!lead.firstname}"/>
<apex:inputfield value="{!lead.lastname}"/>
<apex:inputfield value="{!lead.company}"/>
<apex:inputfield value="{!lead.leadsource}"/>
<apex:inputfield value="{!lead.title}"/>
<apex:inputfield value="{!lead.status}"/>
<apex:inputfield value="{!lead.industry}"/>
<apex:inputfield value="{!lead.Approval_status__c}"/>
</apex:pageBlockSection>
<apex:pageblocksection title="Address Information">
<apex:inputfield value="{!lead.street}"/>
<apex:inputfield value="{!lead.city}"/>
<apex:inputfield value="{!lead.state}"/>
<apex:inputfield value="{!lead.postalcode}"/>
<apex:inputfield value="{!lead.country}"/>
<apex:inputfield value="{!campmem.campaignID}"/>


</apex:pageblocksection>

<apex:pageBlocksection title="Additional Information">
</apex:pageBlocksection>
</apex:pageBlock>
</apex:form> 
</apex:page>

controller:
-------------
public class campaignacess {

    public campaignacess(ApexPages.StandardController controller) {

    }
    public Lead mylead { get; set; }
   public CampaignMember campmem {get; set;}

  public PageReference save() {
        insert mylead;
       campmem.leadid = mylead.id;
        insert campmem;
        mylead.clear();
      
        return null;
    }

}

Kindly support and suggest.
VempallyVempally
Hi keerthigee...

create a handler in the constructor..

write this in constructor...
campmem = new CampaignMember();
This was selected as the best answer
KeerthigeeKeerthigee
Hi,

I got it what exactly i want.

Thanks for helping me with wonderful suggestion.
KeerthigeeKeerthigee
Hi,

Can you help me on one more thing.i.e Writing articles

Kindly support and suggest.
VempallyVempally
what articles...?
KeerthigeeKeerthigee
Hi,

Thanks for your response.

I want to write articles related to reports,triggers,workflows.

Kindly support and suggest.
KeerthigeeKeerthigee
Hi Vempally,

Thanks for your Response.

If I enter some data in campaign field in VF and then save it.

Before saving,I got an error as


User-added image

Kindly support and suggest.


VempallyVempally
Hi 

plz refer to the save method which i have posted earlier...

thanks
KeerthigeeKeerthigee
Hi,

I tried it,but not saving data.

Thanks.
VempallyVempally
add this in constructor

mylead = new Lead();
KeerthigeeKeerthigee
Hi,

I added this constructor already,but not saving the data.

Thanks.
VempallyVempally
I have implemented your code...

its working good...

able to save the data...
KeerthigeeKeerthigee
Hi,

Thank you for your response.

I am getting error till..

User-added image


Kindly support and suggest.


VempallyVempally
Error itself gives you the answer

you missed out to give entries to required fields
KeerthigeeKeerthigee
Hi,

I had given that also.

still,not working.

Thanks.
VempallyVempally
VF code

<apex:page standardcontroller="Lead" tabstyle="lead" extensions="campaignacess5">
<b>my name is "{!$User.FirstName}"</b>
<apex:form >
<apex:pageBlock title="Lead Edit" tabstyle="contact">
<apex:pageblockbuttons >
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!Cancel}" value="cancel"/>
</apex:pageblockbuttons>
<apex:pageBlockSection title="Lead Information" columns="2">
<apex:inputfield value="{!lead.firstname}"/>
<apex:inputfield value="{!lead.lastname}"/>
<apex:inputfield value="{!lead.company}"/>
<apex:inputfield value="{!lead.leadsource}"/>
<apex:inputfield value="{!lead.title}"/>
<apex:inputfield value="{!lead.status}"/>
<apex:inputfield value="{!lead.industry}"/>

</apex:pageBlockSection>
<apex:pageblocksection title="Address Information">
<apex:inputfield value="{!lead.street}"/>
<apex:inputfield value="{!lead.city}"/>
<apex:inputfield value="{!lead.state}"/>
<apex:inputfield value="{!lead.postalcode}"/>
<apex:inputfield value="{!lead.country}"/>
<apex:inputfield value="{!campmem.campaignID}"/>


</apex:pageblocksection>

<apex:pageBlocksection title="Additional Information">
</apex:pageBlocksection>
</apex:pageBlock>
</apex:form>
</apex:page>

Controller

public with sharing class campaignacess5 {

    public campaignacess5(ApexPages.StandardController controller) {
campmem = new CampaignMember();

    }
    public CampaignMember campmem { get; set; }
   
      
     
}


Check it out and say what changes that occur in  related lists of the detail page of the respective record
KeerthigeeKeerthigee
Hi,

Campaigns  have not been updated  into related list campaign history.

Thanks.
VempallyVempally
As expected...

Better to go with Custom Controller instead of standard controller...
KeerthigeeKeerthigee
Hi, Thanks for your response. Ok.I will try with custom controller. Kindly support and suggest.
Abdul RazzaqAbdul Razzaq

Hi,

Thanks. Problem with Campaign field solved... Need help for owner field on the same page... 

Regards,

Razzzaq.

VempallyVempally
Hi Abdul...

Create a handler of User sObject and make use of user id...
Abdul RazzaqAbdul Razzaq

hi vempally,

Is this how you were referring to...

If  yes, I want that 'id' to binded directly... 

Code as follows :-

<apex:page StandardController="Lead" extensions="leadext">
<apex:form >
 <apex:pageBlock mode="Edit" title="Lead Edit" tabstyle="Lead">
 <apex:pageblockButtons >
 <apex:commandButton value="Save" action="{!Save}"/>
 <apex:commandButton Value="Cancel" action="{!Cancel}"/>
 </apex:pageblockButtons>
 <apex:pageBlockSection columns="2" title="Lead Information">
 
<!---- Here it goes ------->
<apex:pageBlockSectionItem >
<apex:outputLabel value="Lead owner"></apex:outputLabel>
<apex:outputpanel >
<apex:outputfield value="{!userame.name}"/>
</apex:outputpanel>
</apex:pageBlockSectionItem>
 
 <!------- -------->
 
 <apex:inputField value="{!mylead.Phone}" /> 
 <apex:pageBlockSectionItem >
 <apex:outputLabel value="First Name"></apex:outputLabel>
 <apex:outputPanel >
 
 <apex:inputfield value="{!mylead.Salutation}"/>
<apex:inputfield value="{!mylead.FirstName}"/></apex:outputPanel></apex:pageBlockSectionItem>
 <apex:inputField value="{!mylead.MobilePhone}" /> 
<apex:inputfield value="{!mylead.LastName}"/>
<apex:inputfield value="{!mylead.Fax}"/>
<apex:inputfield value="{!mylead.Company}"/>
<apex:inputfield value="{!mylead.Email}"/>
<apex:inputfield value="{!mylead.title}"/>
<apex:inputfield value="{!mylead.website}"/>

 
 <apex:inputfield value="{!mylead.leadsource}"/>
 <apex:inputfield value="{!mylead.Status}"/>
 <apex:inputfield value="{!campmem.Campaignid}"/>
 <apex:inputfield value="{!mylead.rating}"/>
 <apex:inputfield value="{!mylead.industry}"/>
 <apex:inputfield value="{!mylead.NumberOfEmployees}"/>
 <apex:inputfield value="{!mylead.AnnualRevenue}"/>
 <apex:inputfield value="{!mylead.state}"/>
 </apex:pageBlockSection>
 <apex:pageBlockSection columns="1" title="Address Information">
 <apex:inputfield value="{!mylead.street}"/>
 <apex:inputfield value="{!mylead.city}"/>
 
  <apex:inputField value="{!mylead.postalcode}"/>
 <apex:inputfield value="{!lead.country}"/>
 </apex:pageBlockSection>
 <apex:pageblocksection columns="2" title="Additional Information">
<apex:inputfield value="{!mylead.ProductInterest__c}"/>
<apex:inputfield value="{!mylead.CurrentGenerators__c}"/>
<apex:inputfield value="{!mylead.SICCode__c}"/>
<apex:inputfield value="{!mylead.Primary__c}"/>
<apex:inputfield value="{!mylead.NumberofLocations__c}"/>
 </apex:pageblocksection>
 <apex:pageblocksection title="Description Information">
 <apex:inputfield value="{!mylead.Description}"/>
 </apex:pageblocksection>
 <apex:pageblocksection title="Optional">
 <apex:pageblockSectionItem >
 <apex:outputPanel >
 <apex:inputCheckbox value="{!mylead.HasOptedOutOfEmail}"/>
  </apex:outputPanel></apex:pageblockSectionItem></apex:pageblocksection>
 </apex:pageBlock>
 </apex:form>
</apex:page>



==============================================================================



public class leadext {

public leadext(ApexPages.StandardController controller) {
campmem = new CampaignMember();
mylead = new lead();
ls = new user();
    }
    
    public Lead mylead { get; set; }
   public CampaignMember campmem {get; set;}
   public user ls{get;set;}
   Pagereference leadpage;
   user userame;
   
   public user getuserame(){
   ls = [ Select Name from User where Id = :'00590000003Mfae'];
   return ls;
      }

  public PageReference save() {
  insert mylead;
campmem.leadid = mylead.id;
insert campmem;
mylead.clear();
campmem.clear();
return null;    
    }

}