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
matt.bylermatt.byler 

Visualforce Page does not pull in the value of a field on opportunities

I have a VF page that creates work orders when you click a custom button on the opportunities page. I want to transfer opportunity name and account to the work order through the page but it is throwing an error. Any ideas how to retain information from a previous page????

Best Answer chosen by Admin (Salesforce Developers) 
Alok_NagarroAlok_Nagarro

Hi,

 

Try the following code -

 

// ---------------controller-------------------

 

public with sharing class MyExtension

{
public Work_Order__c wo{get; set;}
opportunity opp;


 public MyExtension(ApexPages.StandardController controller) {
opp=[select id,accountId from opportunity where id=:controller.getId()];
 wo = new Work_Order__c();
 wo.Opportunity__c=opp.id;
 if(opp.AccountId!=null)
 wo.Account__c=opp.AccountId;
 }



public PageReference save()
{
 if(wo != null)
 insert wo;
 return null;
}

public PageReference cancel()
{
  return null;
}
}

All Answers

Alok_NagarroAlok_Nagarro

Hi Matt,

 

In visualforce page you can access the opportunity Id from url, since when you click on custom button on the standard details page of any object's record (in your case it is Opportunity) and if you gonna opening visualforce page then that record id is appended in url by default. So in the page you can get this id as -

 

String Id=ApexPages.CurrentPage.getParameters().get('id');

 

now using this id you can query to opportunity to get required data.

 

Note - it's only true in case of you selected "Visualforce Page" as Content Source at the time of creating custom detail button.

If you selected URL and Given the URL manualy then you need to pass opprtunity Id with this url manually too.

 

 

 

 

matt.bylermatt.byler

I was able to manually pull in the Id of the Opportunity and the Opportunity account but it will not let me prepopulate the fields with these. Can you send me some example code to prepopulate the fields? 

 

Here is my code so far:

 

<apex:page standardController="Work_Order__c">
<apex:form >
<apex:pageBlock title="Create Work Order for {!$User.FirstName}">
<apex:pageMessages showDetail="true" />
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:inputField value="{!Work_Order__c.Account__c}" required="true"/>
<apex:inputField value="{!Work_Order__c.Opportunity__c}" required="true"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

And the two fields I need filled in are the Account and the Opportunity name on the custom object called work order.

 

Here is the URL I get from clicking the custom button:

https://c.cs3.visual.force.com/apex/Test1?OpportunityId=006Q0000008tiHL&AccountId=001Q000000R8OMQ

 

Any help is appreciated

 

Alok_NagarroAlok_Nagarro

Hi Matt,

 

Try the following code -

 

//------------Visualforce Page------------

 

<apex:page standardController="Opportunity" extensions="MyExtension" tabStyle="Work_Order__c">
<apex:form >
<apex:pageBlock title="Create Work Order for {!$User.FirstName}">
<apex:pageMessages showDetail="true" />
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:inputField value="{!wo.Account__c}" required="true"/>
<apex:inputField value="{!wo.Opportunity__c}" required="true"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

//----------controller------------

public class MyExtension
{
public Work_Order__c wo{get; set;}
opportunity opp;

public MyExtension(ApexPages.standardController std)
{
 opp=(Opportunity)std.getRecord();
 wo = new Work_Order__c();
 wo.Opportunity__c=opp.id;
 if(opp.Account__c!=null)
   wo.Account__c=app.Account__c;
}

public PageReference save()
{
 if(wo != null)
 insert wo;
 return null;
}

public PageReference cancel()
{
  return null;
}
}

matt.bylermatt.byler

This code did work to pull in the opportunity name but will not populate the account from the opportunity. Any idea on how to pull in this field?

Alok_NagarroAlok_Nagarro

Hi,

 

Plz make sure there is an account associated with opporunity. It might be possible that no any account is associated with that opportunity.

matt.bylermatt.byler

This is the error message I receive:

 

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Opportunity.Account

 

 

Please let me know if I need to add anything to the code.

Alok_NagarroAlok_Nagarro

Hi Matt,

 

Can u plz post your code(controller & page) so that i can figure out where is the problem ?

matt.bylermatt.byler

--------------------------------page---------------------------

<apex:page standardController="Opportunity" extensions="MyExtension" tabStyle="Work_Order__c">
<apex:form >
<apex:pageBlock title="Create Work Order for {!$User.FirstName}">
<apex:pageMessages showDetail="true" />
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:inputField value="{!wo.Account__c}" required="false"/>
<apex:inputField value="{!wo.Opportunity__c}" required="true"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

----------------------Controller-------------------------

public class MyExtension
{
public Work_Order__c wo{get; set;}
opportunity opp;

public MyExtension(ApexPages.standardController std)
{
 opp=(Opportunity)std.getRecord();
 wo = new Work_Order__c();
 wo.Opportunity__c=opp.id;
 if(opp.Account__c!=null)
   wo.Account__c=app.Account__c;
}

public PageReference save()
{
 if(wo != null)
 insert wo;
 return null;
}

public PageReference cancel()
{
  return null;
}
}

Alok_NagarroAlok_Nagarro

Hi,

 

Try the following code -

 

// ---------------controller-------------------

 

public with sharing class MyExtension

{
public Work_Order__c wo{get; set;}
opportunity opp;


 public MyExtension(ApexPages.StandardController controller) {
opp=[select id,accountId from opportunity where id=:controller.getId()];
 wo = new Work_Order__c();
 wo.Opportunity__c=opp.id;
 if(opp.AccountId!=null)
 wo.Account__c=opp.AccountId;
 }



public PageReference save()
{
 if(wo != null)
 insert wo;
 return null;
}

public PageReference cancel()
{
  return null;
}
}

This was selected as the best answer
matt.bylermatt.byler

Thanks this works perfect.