+ Start a Discussion
ShadowlessKickShadowlessKick 

Salesforce Classic to Lightning

Trying to duplicate a  process that was developed in Salesforce Classic.  What should happen is a method in a controller should be launched on page load of  a visual force page, the constructor should do some work and return  a parm to the visualforce page.  Do not want to click more buttons other than the initial "action button" that calls a visualforce page.  Could someone please explain what is in error here?  Is this the correct path to take?  
<apex:page standardController="Opportunity" extensions="opportunityMockUp" showQuickActionVfHeader="false"> 
<script>  
         window.onload = new function() { buildURL(); };
            function buildURL()
            {
			//Just a container to launch the action button
            };   
  </script>
  <script>          
            function navigateToMockUp(){            
               if ( (typeof sforce != 'undefined') && (sforce != null) )  {s
                   alert('{!theBuiltURL}');
                    sforce.one.navigateToSObject('{!theBuiltURL}');
                } else {
                    window.location='/' + '{!theBuiltURL}';
               }        
            }
</script>
   <apex:form >
       <apex:actionFunction name="buildURL" action="{!autoRun}" rerender="none" oncomplete="navigateToMockUp();">
       </apex:actionFunction>
</apex:form>
</apex:page>
 
public with sharing class opportunityMockUp{
//Define the Project Object
    Opportunity theOpportunity = new Opportunity(); 
    String thePageOpportunityId;  
    String theOpportunityID; 
    String theOppName;
    String theAccountID;
    String theAmount;
    String theOppNumber;
    String theQuote;
    String theSalesRep;
    String theDistrictName;
    String theRegion;
    String theProjectCoordinator;
    String theMockUpName;
    String theTarget;
    String theParameters;
    String theValues;   
    String theURL;
    String theAccountName;
    public String theBuiltURL {get;set;}
       
    // Constructor - this only really matters if the autoRun function doesn't work right     
    public opportunityMockUp(ApexPages.StandardController stdController) {        
        this.theOpportunity = (Opportunity)stdController.getRecord();   
     } 
    
    // Code invoked on page load.      
    public PageReference autoRun()
    {           
        thePageOpportunityId = ApexPages.currentPage().getParameters().get('id'); 
        if (thePageOpportunityId == null) 
        {             
            // Display the Visualforce page's error message if no Id is passed over             
            return null;         
        }       
      
    for (Opportunity theOpportunity:[select Id, Name, Accountid, Account.Name, Amount, Opportunity_Number__c, KI_Quote__c, Sales_Representative__c,District_Name__c,Region__c, 	Project_Coordinator__c, KCO_Competition_Current_Suppliers__c from Opportunity where id =:thePageOpportunityId Limit 1]) 
    {               
        theTarget = '&00N50000002pCh2'; 
        theParameters = '';
        theValues = theOpportunity.KCO_Competition_Current_Suppliers__c; 
        if (theValues != null && theValues  != '' && theValues != '0'){
        List<String> theValueArray = theValues.split(';'); 
        for (integer i=0; i < theValueArray.size(); i++) { 
          theParameters = theParameters + theTarget + '=' + theValueArray[i] ; 
        system.debug('TheParms: ' + theParameters);
        }
        }  
        else
        {
          theParameters = '';
        }
        
        theMockUpName = 'Mock Up: ' + theOpportunity.Name;
        theAmount = String.ValueOf(theOpportunity.Amount);
        if (theAmount != null && theAmount != '') {
        theAmount = theAmount.Substring(0,3);
        }
        else
        {
          theAmount = '0';
        }
         
        theOpportunityID = theOpportunity.Id; 
        theAccountID = theOpportunity.Accountid; 
        theAccountName = theOpportunity.Account.Name;
        theOppName = theOpportunity.Name;
        theOppNumber = theOpportunity.Opportunity_Number__c;
        theQuote = theOpportunity.KI_Quote__c;  
        if (theOpportunity.KI_Quote__c != null && theOpportunity.KI_Quote__c != '') {
        theQuote = theOpportunity.KI_Quote__c;  
        }
        else
        {
          theQuote = '';
        }
        theSalesRep = theOpportunity.Sales_Representative__c;
        theDistrictName = theOpportunity.District_Name__c;
        theRegion = theOpportunity.Region__c;
        theProjectCoordinator = theOpportunity.Project_Coordinator__c;
        theOpportunityID = theOpportunity.id; 
        TheURL = '/a0G/e?CF00N500000026uRE=' + theOppName + '&CF00N500000026uR5=' + theAccountName + '&00N50000002pCi0=' + theOppNumber + '&00N50000002pCuV=' + theQuote + 	'&00N500000026uRM=' +  theSalesRep + '&00N50000002pCmW=' + theDistrictName + '&00N50000002pCmb=' + theRegion + theParameters + '&00N500000026uRK=' + theAmount + 	'&00N500000026uRK=' + theProjectCoordinator + '&Name=' + theMockUpName + '&retURL=/' + theOpportunityID + ' target="blank"';  
          system.debug('TheURL 1: = ' + TheURL);
        //TheURL = EncodingUtil.URLENCODE(TheURL,'UTF-8');
            system.debug('TheURL 2: = ' + TheURL);
        theBuiltURL = TheURL;    
        }  
     return null;
    }

 
Best Answer chosen by ShadowlessKick
ShadowlessKickShadowlessKick
This is what finally worked.  It cannot be this difficult to replace URL hacks.  I must be missing something.
<apex:page standardController="Account" extensions="createOpportunityFromAccount"  showQuickActionVfHeader="false"> 
<!-- The actionFunction is activated by the buildURL java script call on page load. 
When the actionFunction is activated, the method "autorun" in controller createOpportunityFromAccount is  
fired.  The return value of null is needed to return to the page. When the actionFunction re-render
is executed the page is redirected to the opportunity the controller just created. -->
<script>
         	onload = function() { buildURL(); };
</script>
<apex:pageBlock >
<apex:pageBlockSection title="Create Opportunity From Account" columns="1" >
<apex:outputPanel id="jspanel">
<script type="text/javascript">
 			   var theURL = '{!theBuiltURL}';
               if ( (typeof sforce != 'undefined') && sforce && (!!sforce.one) )   {
                    sforce.one.navigateToSObject(theURL);
               } else {
                    alert('Something is not right1');
               }               
</script>
</apex:outputPanel>  
</apex:pageBlockSection>
</apex:pageBlock>
<apex:form >
	<apex:actionFunction name="buildURL" action="{!autoRun}" rerender="jspanel" >
    </apex:actionFunction>
</apex:form>
</apex:page>

And the controller.
public with sharing class createOpportunityfromAccount {

Account theAccount = new Account();
Opportunity theOpportunity = new Opportunity();   
String theOpportunityID = ''; 
String theAccountID = ''; 
List<Opportunity> OpportunityInsert  = new List<Opportunity>();
public String theBuiltURL {get;set;}

// Constructor - This only matters if the autoRun function doesn't work right     
public createOpportunityfromAccount(ApexPages.StandardController stdController) {        
this.theAccount = (Account)stdController.getRecord();     
}
 
// Code invoked on page load.      
public PageReference autoRun()
{           
String theAccountID = ApexPages.currentPage().getParameters().get('id'); 
{             
// Display the Visualforce page's error message if no Id is passed over             
return null;         
} 

List<Account> TheAccountList = [select Id, Name, Market_Code__c, Sales_Rep__c, Field_Sales_Rep_Number__c, KI_Customer__c, Region__c, District__c, District_Name__c, District_Type__c from Account where id =:theAccountID  Limit 1];

for(Account theAccount: TheAccountList)
{ 
theOpportunity.AccountId = theAccount.Id;    
theOpportunity.StageName = 'FO';
theOpportunity.Name = theAccount.Name;
Date theCloseDate = system.today();
theOpportunity.CloseDate =  theCloseDate.addDays(90); 
 
if (theAccount.Market_Code__c != null ) {
theOpportunity.Market_Code__c = theAccount.Market_Code__c;  
}

if (theAccount.Sales_Rep__c != null ) {
theOpportunity.Sales_Representative__c = theAccount.Sales_Rep__c;  
}

if (theAccount.Field_Sales_Rep_Number__c != null ) {
theOpportunity.Field_Sales_Rep_Number__c = theAccount.Field_Sales_Rep_Number__c;  
}

if (theAccount.KI_Customer__c != null ) {
theOpportunity.KI_Customer__c = theAccount.KI_Customer__c;  
}

if (theAccount.Region__c != null ) {
theOpportunity.Region__c = theAccount.Region__c;  
}

if (theAccount.District__c != null ) {
theOpportunity.District__c = theAccount.District__c;  
}

if (theAccount.District_Name__c != null ) {
theOpportunity.District_Name__c = theAccount.District_Name__c;  
}

if (theAccount.District_Name__c != null ) {
theOpportunity.District_Name__c = theAccount.District_Name__c;  
}

if (theAccount.District_Type__c != null ) {
theOpportunity.District_Type__c = theAccount.District_Type__c;  
}
    insert theOpportunity;
    theBuiltURL =   theOpportunity.id;
  
}

return null; 
      
} 

}

       
</apex:page>

   
 

All Answers

Andy BoettcherAndy Boettcher
What exactly are you trying to do?  
ShadowlessKickShadowlessKick
The top statement of this post says what I am trying to do.
SattibabuSattibabu
I am not getting you fully. I can suggest you one thing based on my understanding is that build a lightning component which will call the controller method at the time of component loading . You can do this with 'init' method and afer that use that component in visualforce page. Please check the below links for furhter reference. 

https://developer.salesforce.com/blogs/developer-relations/2015/03/lightning-components.html

Including Lightening component into Visualforce page and passing parameter through VF page to Lightening component.

https://satti-salesforce.blogspot.com/b/post-preview?token=1MeO6VEBAAA.N4qll1Gul7EYGKTAwFpTJf4y9hU2D8wOr6y-FxfYRpADq6ediiEKho0SB_ZOZGlneVM3ajxVg0ODRTsjlkX9yg.cPjI3BjVvPjuzqYRRSLnRw&postId=7458652044440064044&type=POST
ShadowlessKickShadowlessKick
Sattibabu, I have been trying to access your link. It is invalid.  Could you post again? Thanks.
ShadowlessKickShadowlessKick
This is what finally worked.  It cannot be this difficult to replace URL hacks.  I must be missing something.
<apex:page standardController="Account" extensions="createOpportunityFromAccount"  showQuickActionVfHeader="false"> 
<!-- The actionFunction is activated by the buildURL java script call on page load. 
When the actionFunction is activated, the method "autorun" in controller createOpportunityFromAccount is  
fired.  The return value of null is needed to return to the page. When the actionFunction re-render
is executed the page is redirected to the opportunity the controller just created. -->
<script>
         	onload = function() { buildURL(); };
</script>
<apex:pageBlock >
<apex:pageBlockSection title="Create Opportunity From Account" columns="1" >
<apex:outputPanel id="jspanel">
<script type="text/javascript">
 			   var theURL = '{!theBuiltURL}';
               if ( (typeof sforce != 'undefined') && sforce && (!!sforce.one) )   {
                    sforce.one.navigateToSObject(theURL);
               } else {
                    alert('Something is not right1');
               }               
</script>
</apex:outputPanel>  
</apex:pageBlockSection>
</apex:pageBlock>
<apex:form >
	<apex:actionFunction name="buildURL" action="{!autoRun}" rerender="jspanel" >
    </apex:actionFunction>
</apex:form>
</apex:page>

And the controller.
public with sharing class createOpportunityfromAccount {

Account theAccount = new Account();
Opportunity theOpportunity = new Opportunity();   
String theOpportunityID = ''; 
String theAccountID = ''; 
List<Opportunity> OpportunityInsert  = new List<Opportunity>();
public String theBuiltURL {get;set;}

// Constructor - This only matters if the autoRun function doesn't work right     
public createOpportunityfromAccount(ApexPages.StandardController stdController) {        
this.theAccount = (Account)stdController.getRecord();     
}
 
// Code invoked on page load.      
public PageReference autoRun()
{           
String theAccountID = ApexPages.currentPage().getParameters().get('id'); 
{             
// Display the Visualforce page's error message if no Id is passed over             
return null;         
} 

List<Account> TheAccountList = [select Id, Name, Market_Code__c, Sales_Rep__c, Field_Sales_Rep_Number__c, KI_Customer__c, Region__c, District__c, District_Name__c, District_Type__c from Account where id =:theAccountID  Limit 1];

for(Account theAccount: TheAccountList)
{ 
theOpportunity.AccountId = theAccount.Id;    
theOpportunity.StageName = 'FO';
theOpportunity.Name = theAccount.Name;
Date theCloseDate = system.today();
theOpportunity.CloseDate =  theCloseDate.addDays(90); 
 
if (theAccount.Market_Code__c != null ) {
theOpportunity.Market_Code__c = theAccount.Market_Code__c;  
}

if (theAccount.Sales_Rep__c != null ) {
theOpportunity.Sales_Representative__c = theAccount.Sales_Rep__c;  
}

if (theAccount.Field_Sales_Rep_Number__c != null ) {
theOpportunity.Field_Sales_Rep_Number__c = theAccount.Field_Sales_Rep_Number__c;  
}

if (theAccount.KI_Customer__c != null ) {
theOpportunity.KI_Customer__c = theAccount.KI_Customer__c;  
}

if (theAccount.Region__c != null ) {
theOpportunity.Region__c = theAccount.Region__c;  
}

if (theAccount.District__c != null ) {
theOpportunity.District__c = theAccount.District__c;  
}

if (theAccount.District_Name__c != null ) {
theOpportunity.District_Name__c = theAccount.District_Name__c;  
}

if (theAccount.District_Name__c != null ) {
theOpportunity.District_Name__c = theAccount.District_Name__c;  
}

if (theAccount.District_Type__c != null ) {
theOpportunity.District_Type__c = theAccount.District_Type__c;  
}
    insert theOpportunity;
    theBuiltURL =   theOpportunity.id;
  
}

return null; 
      
} 

}

       
</apex:page>

   
 
This was selected as the best answer