i have overridden new button of lead object.
My requirement is to add lead object to the related list of the custom object "journey".
I have created a lookup field called Journey id on lead.
I have created a vf page to override new button and create the commandbutton save. after clicking on the save button it will 1st call to the javascript function which will capture the location and after that save method. the javascript function capture the location and creates a object for journey.
in action part of commandbutton i have called the save method.
VisualforcePage:
<apex:sectionheader title="Lead Edit" subtitle="{!if(Lead.Id==null,'New Lead',Lead.Name)}"></apex:sectionheader>
<apex:pageblock mode="edit" id="leadPB" title="Lead Edit">
<apex:pageblockbuttons >
<apex:commandbutton onclick="getRecordLocations()" action="{!save}" value="Save" >
<apex:actionFunction name="addRecordLocation" action="{!addRecordLocation}" reRender="">
<apex:param id="lat1" name="lat1" value="" assignTo="{!valueLat}"/>
<apex:param id="long1" name="long1" value="" assignTo="{!valueLong}"/>
<apex:param id="addr1" name="addrr" value="" assignTo="{!existingAddresses1}"/>
</apex:actionFunction>
</apex:commandbutton>
<apex:commandbutton action="{!cancel}" value="Cancel"></apex:commandbutton>
</apex:pageblockbuttons>
<apex:pagemessages ></apex:pagemessages>
<apex:pageblocksection id="LeadInformationPBS" title="Lead Information">
<apex:inputfield value="{!Lead.OwnerId}"></apex:inputfield>
<apex:inputfield value="{!Lead.Phone}"></apex:inputfield>
<apex:pageblocksectionitem >
<apex:outputlabel value="{!$ObjectType.Lead.Fields.FirstName.label}"></apex:outputlabel>
<apex:outputpanel >
<apex:inputfield value="{!Lead.Salutation}"></apex:inputfield>
<apex:inputfield value="{!Lead.FirstName}"></apex:inputfield>
</apex:outputpanel>
</apex:pageblocksectionitem>
<apex:inputfield value="{!Lead.MobilePhone}"></apex:inputfield>
<apex:inputfield value="{!Lead.LastName}"></apex:inputfield>
<apex:inputfield value="{!Lead.Fax}"></apex:inputfield>
<apex:inputField value="{!Lead.Company}" />
<apex:inputfield value="{!Lead.Email}" required="true"></apex:inputfield>
<apex:inputfield value="{!Lead.Title}"></apex:inputfield>
<apex:inputfield value="{!Lead.Website}"></apex:inputfield>
<apex:inputfield value="{!Lead.Leadsource}"></apex:inputfield>
<apex:inputfield value="{!Lead.Status}"></apex:inputfield>
<apex:inputfield value="{!Lead.Rating}"></apex:inputfield>
<apex:inputfield value="{!Lead.Industry}"></apex:inputfield>
<apex:inputfield value="{!Lead.AnnualRevenue}"></apex:inputfield>
<apex:inputfield value="{!Lead.NumberOfEmployees}"></apex:inputfield>
</apex:pageblocksection>
<apex:pageblocksection id="AddressInformationPBS" title="Address Information">
<apex:inputfield value="{!Lead.Street}"></apex:inputfield>
<apex:outputtext value=""></apex:outputtext>
<apex:inputfield value="{!Lead.City}"></apex:inputfield>
<apex:outputtext value=""></apex:outputtext>
<apex:inputfield value="{!Lead.State}"></apex:inputfield>
<apex:outputtext value=""></apex:outputtext>
<apex:inputfield value="{!Lead.PostalCode}"></apex:inputfield>
<apex:outputtext value=""></apex:outputtext>
<apex:inputfield value="{!Lead.Country}"></apex:inputfield>
<apex:outputtext value=""></apex:outputtext>
</apex:pageblocksection>
<apex:pageblocksection id="AdditionalInformationPBS" title="Additional Information">
<apex:inputfield value="{!Lead.ProductInterest__c}"></apex:inputfield>
<apex:inputfield value="{!Lead.CurrentGenerators__c}"></apex:inputfield>
<apex:inputfield value="{!Lead.SICCode__c}"></apex:inputfield>
<apex:inputfield value="{!Lead.Primary__c}"></apex:inputfield>
<apex:inputfield value="{!Lead.NumberofLocations__c}"></apex:inputfield>
<apex:inputfield value="{!Lead.Journey__c }"></apex:inputfield>
</apex:pageblocksection>
<apex:pageblocksection id="DescriptionInformationPBS" title="DescriptionInformation">
<apex:inputfield value="{!Lead.Description}"></apex:inputfield>
</apex:pageblocksection>
<apex:pageblocksection id="OptionPBS" title="Lead Information">
</apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>
Controller:
global with sharing class LeadObjectLocationController {
public Lead led{ get; set; }
public ApexPages.StandardController sc{get; set;}
public String existingAddresses {get;set;}
public String existingAddresses1 {get;set;}
public String valueLong { get; set; }
public String valueLat { get; set; }
public String valueLong1 { get; set; }
public String valueLat1 { get; set; }
public String valueAddr { get; set; }
public List<Journey__c> journeys {get;set;}
public Location__c LatLong;
public Journey__c jr;
public List<Lead> leadData{get;set;}
public Id LeadId{get;set;}
public String j{get;set;}
global String getIdOfJ{get;set;}
public LeadObjectLocationController(ApexPages.StandardController controller)
{
this.sc=controller;
this.led = (Lead)controller.getRecord();
}
//RemoreAction method to get accessed in the javascript
@RemoteAction
global static List<Location__c> findAll() {
return [SELECT Id,Name,User__c ,Location_Name__Latitude__s ,Location_Name__Longitude__s,Journey__c,Address__c FROM Location__c];
}
public void addRecordLocation()
{
if(test.isRunningTest()){
valueLat = '18.51870548';
valueLong = '73.93681982';
existingAddresses = 'Amanora Town Centre, Amanora Town Centre, Amanora Park Town, Hadapsar, Pune, Maharashtra 411028, India';
}
Double lat = Double.valueOf(valueLat);
Double lng = Double.valueOf(valueLong);
journeys = [SELECT Id, Name, Journey_Start_Location__c, Date_Time__c, User__c,Address__c,IsActive__c, CreatedDate FROM Journey__c WHERE CreatedDate =TODAY AND IsActive__c=true AND User__c=: userInfo.getUserId() ];
if(!journeys.isEmpty()){
Journey__c jr;
Location__c LatLong;
LatLong = new Location__c(Journey__c = journeys[0].id,Location_Name__Latitude__s = lat,Location_Name__Longitude__s = lng,Date_Time__c = System.now(),Address__c = existingAddresses1,User__c=userInfo.getUserId(),Location_Type__c='Lead Creation');
insert LatLong;
getIdOfJ=journeys[0].id;
system.debug('Journey id in getRecord method:'+getIdOfJ);
}else{
jr = new Journey__c(IsActive__c=false,Date_Time__c = System.now(),Address__c = existingAddresses1, User__c=userInfo.getUserId(),Journey_Start_Location__Latitude__s = lat, Journey_Start_Location__Longitude__s = lng,Journey_Stop_Address__c=existingAddresses1,Journey_Stop_Lat_Long__Latitude__s=lat,Journey_Stop_Lat_Long__Longitude__s=lng,Journey_Stop_Time__c =System.now().Time());
DateTime dt =System.now();
String formattedDt = dt.format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\'');
jr.DateTimeStamp__c =formattedDt;
insert jr;
LatLong = new Location__c(Journey__c = jr.id,Location_Name__Latitude__s = lat,Location_Name__Longitude__s = lng,Date_Time__c = System.now(),Address__c = existingAddresses1,User__c=userInfo.getUserId(),Location_Type__c='Lead Creation');
insert LatLong;
getIdOfJ=jr.id;
system.debug('Journey id in getRecord method:'+getIdOfJ);
}
system.debug('Journey id in getRecord method outside part:'+getIdOfJ);
}
public PageReference save() {
PageReference detailPage = sc.save();
j=sc.getRecord().id;
system.debug('lead id in save method:'+j);
system.debug('Journey id in save method:'+getIdOfJ);
List<Lead> leadCreateNow =[SELECT Journey__c, Id FROM Lead where Id=:j];
List<Lead> updatedLeadList = new List<Lead>();
for(Lead l:leadCreateNow)
{
l.Journey__c= getIdOfJ;
system.debug('after lead ID of j:'+ l.Journey__c);
updatedLeadList.add(l);
}
upsert updatedLeadList;
return detailPage;
}}