You need to sign in to do that
Don't have an account?
Nick Valeriote
Sample code with AddButton element
Hi SF Community,
I'm not a developer, but have dabled in some code in the past.
Wondering if anyone has some sample code I can modify/use to achieve the following (see screen shot below)? I don't need those specific field headings, as I would substitute them with other field headings. And I'm hoping to have that Add Row button, whereby when a user clicks it, it adds a new row. I want to add this as a new section on the page layour of our opportunity page.
Does anyone have some code I can use for such a thing?
Thanks much!
Nick
Sorry for the late reply. Please use below code to save records to associate Opportunity. I created a lookup relationship field in my custom sObject with API name Opportunity__c to relate custom object with opportunity.
Custom sObject: Student__c
Lookup in Student__c related to Opportunity: Opportunity__c
After that, you can create a related list of your object in Opportunity layout so that related records will be visible on Opportunity detail page.
I hope it helps you.
Kindly mark this as solved if the information was helpful.
Regards,
Khan Anas
All Answers
I trust you are doing very well.
Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Visualforce:
Controller:
Screenshot:
Instead of commandLink you can use commandButton if you want button on your page.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.
Thanks and Regards,
Khan Anas
Question - is 'Student__c' referencing a custom object in your example? Can I replace this with Opportunity?
Yes, you can replace Student__c with Opportunity :)
Sorry, I forgot to mention, Student__c is a custom object.
Name, Course__c (Picklist Field), Mobile_Number__c are fields of Student__c object.
Another quick question for you, Khan...
What is function of this part of the code:
"public PageReference groupInsert(){
try{
for(Opportunity s2 : OpportunityAddList){
if(s2.course__c == 'Salesforce'){
stu.add(s2);
}
}
insert stu;
}
public PageReference groupInsert(){
try{
for(Student__c s2 : studentAddList){
stu.add(s2);
}
insert stu;
}
catch (Exception e) {
ApexPages.addMessages (e);
}
PageReference pr = new PageReference(System.currentPageReference().getURL());
pr.setRedirect(true);
return pr;
}
Don't forget to mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.
Regards,
Khan Anas
This is what I wanted, looks good, thanks!!!
However, do you know how I can add this as a section to the Opportunity page layout? I don't see it there as an option...
To embed a Visualforce page into page layout, the Visualforce page should be using the standard controller tag referencing the same entity that will be used in the page layout.
I hope it helps you.
Regards,
Khan Anas
Current error on Vforce page: Error: Unknown property 'OpportunityStandardController.OpportunityAddList'
Vforce page code:
<apex:page standardController="Opportunity" extensions="ApexClass">
<apex:form >
<apex:pageBlock >
<apex:pageblockSection >
<apex:pageBlockTable value="{!OpportunityAddList}" var="std">
<apex:column headerValue="SOW Item">
<apex:inputField value="{!std.SOW_Item__c}"/>
</apex:column>
<apex:column headerValue="SOW Hours">
<apex:inputField value="{!std.SOW_Hours__c}"/>
</apex:column>
<apex:column headerValue="SOW Item Cost">
<apex:inputField value="{!std.SOW_Item_Cost__c}"/>
</apex:column>
</apex:pageBlockTable>
<br/><apex:commandLink value="Add Row" action="{!addRow}"/>
<br/><apex:commandLink value="Remove Row" action="{!delRow}"/>
</apex:pageblockSection>
<apex:pageblockButtons >
<apex:commandButton value="Save" action="{!groupInsert}" />
</apex:pageblockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Class:
public class GroupInsertController{
public List<Opportunity> OpportunityAddList {get;set;}
public List<Opportunity> opp {get; set;}
public GroupInsertController()
{
Opportunity s = new Opportunity();
OpportunityAddList = new List<Opportunity>();
opp = new List<Opportunity>();
OpportunityAddList.add(s);
}
public void addRow()
{
Opportunity s1 = new Opportunity();
OpportunityAddList.add(s1);
}
public void delRow()
{
Integer i=OpportunityAddList.size();
OpportunityAddList.remove(i-1);
}
public PageReference groupInsert(){
try{
for(Opportunity s2 : OpportunityAddList){
opp.add(s2);
}
insert opp;
}
catch (Exception e) {
ApexPages.addMessages (e);
}
PageReference pr = new PageReference(System.currentPageReference().getURL());
pr.setRedirect(true);
return pr;
}
}
Please refer to below code:
Visualforce:
Controller:
Kindly mark this as solved if the information was helpful.
Regards,
Khan Anas
Looking good on the Opportunity page. However, when I enter values in these fields and click the Save button, the values disappear. How can I save inputted values and have them associated to the opportunity record I'm on?
Sorry for the late reply. Please use below code to save records to associate Opportunity. I created a lookup relationship field in my custom sObject with API name Opportunity__c to relate custom object with opportunity.
Custom sObject: Student__c
Lookup in Student__c related to Opportunity: Opportunity__c
After that, you can create a related list of your object in Opportunity layout so that related records will be visible on Opportunity detail page.
I hope it helps you.
Kindly mark this as solved if the information was helpful.
Regards,
Khan Anas
It's my pleasure. I’m glad I was able to help!
Below is my test class with 100% code coverage.
Controller:
Test Class:
Regards,
Khan Anas
I updated the test class so it fits with my items (not your custom Students stuff). However, when I validate this test class on the incominging Change Set in production, I get this error:
"System.NullPointerException: Argument 1 cannot be null
Stack Trace: Class.SOWItemsOpp2.groupInsert: line 41, column 1 Class.SOWItemsOpp2Test.myUnitTest: line 24, column 1".
Here's my updated test class code:
Any idea what the issue might be?
Thanks.
Nick
Please update controller code, you need to remove 2 lines from groupInsert method.
PageReference pr = new PageReference(System.currentPageReference().getURL()); pr.setRedirect(true);
Remove this and you are good to go :)
Regards,
Khan Anas