+ Start a Discussion
pranav shah 40pranav shah 40 

Lead Code Explaination

Can anyone explain me below line of codes


public class myWeb2LeadExtension
{
private final Lead weblead;
public myWeb2LeadExtension(ApexPages.StandardController stdController) 
{
weblead = (Lead)stdController.getRecord();

public PageReference saveLead() {
try 
{
insert(weblead);
 }
catch(System.DMLException e) {
 ApexPages.addMessages(e);
return null;
}
PageReference p = Page.ThankYou;
p.setRedirect(true);
       return p;
     }
 
Best Answer chosen by pranav shah 40
RamakantRamakant
Hi Pranav,

Basically this code is extension class for your standard controller Lead. Please find explanation of each line in comment
public class myWeb2LeadExtension // Your class Name
{
	private final Lead weblead; // Created new instance of type Lead
	public myWeb2LeadExtension(ApexPages.StandardController stdController) // Standard Controller extension
	{
		//stdController standard controller variable
		weblead = (Lead)stdController.getRecord(); // To get your lead record
	} 
	public PageReference saveLead() { // Method declaration to save lead record
		try 
		{
			insert(weblead); // Insert Lead record if no error comes
		}
		catch(System.DMLException e) { // Come to Catch block if error comes
			ApexPages.addMessages(e); // add DMLException Message on the visualforce page
			return null;
		}
		PageReference p = Page.ThankYou; // Page reference defenation
		p.setRedirect(true); // to take controller to the ThankYou page in your salesforce org
		return p; //return Pagereference
	}
}
Please let me know if helped.

Regards,
Ramakant
 

All Answers

RamakantRamakant
Hi Pranav,

Basically this code is extension class for your standard controller Lead. Please find explanation of each line in comment
public class myWeb2LeadExtension // Your class Name
{
	private final Lead weblead; // Created new instance of type Lead
	public myWeb2LeadExtension(ApexPages.StandardController stdController) // Standard Controller extension
	{
		//stdController standard controller variable
		weblead = (Lead)stdController.getRecord(); // To get your lead record
	} 
	public PageReference saveLead() { // Method declaration to save lead record
		try 
		{
			insert(weblead); // Insert Lead record if no error comes
		}
		catch(System.DMLException e) { // Come to Catch block if error comes
			ApexPages.addMessages(e); // add DMLException Message on the visualforce page
			return null;
		}
		PageReference p = Page.ThankYou; // Page reference defenation
		p.setRedirect(true); // to take controller to the ThankYou page in your salesforce org
		return p; //return Pagereference
	}
}
Please let me know if helped.

Regards,
Ramakant
 
This was selected as the best answer
pranav shah 40pranav shah 40
hey Ramakant,

will you please explain these.. this code is for inserting opportunity amount and updating the field on Account

public class opportunityhandler
  {
     public void opportunityamount(list<opportunity> newopportunity)
    {
     set<Id> setOppName=new set<Id>();
     List<Account> lstActs =new list<Account>();
     List<Opportunity> opps=new list<Opportunity>();
    for(opportunity opp:newopportunity)
    {
      setOppname.add(opp.Accountid);
    }
    
    list<Account> LstAccs= [select id,Name,Total_Opportunity_Amount__c,(select id, Amount from opportunities) from account where Id IN:setOppname];
    for(Account acc: lstAccs)
    { 
    double TotalAmount=0;
    for(Opportunity opp:acc.opportunities)
    {
     if(opp.Amount!=null)
    {  
     totalAmount=TotalAmount+opp.Amount;
    }
    }  
    acc.Total_Opportunity_Amount__c=totalAmount;
    lstacts.add(acc);
    }
    update lstacts;
    }
  }