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
anvesh@force.comanvesh@force.com 

please correct the below code ?

 am retreiving  opportunity records  under a contact (Contact Related list opportunities)  and displaying in a page table......i  am retreving all data  and every thing is good.  But i want add a filter condition where  sample Hype = true. then  all opportunity records under contact where sample hypo field is true should need to retrive.  But  after applying  condition i am getting  Referring a Null object exception for Olist.add(Op)  in below code.  I rendered the Olist in VF page pageblock table. I am not getting error in the code/VF.. after  rendered this to a button. After click buttoon it need to pull records and display here i am getting error.


public with sharing class OppRelatedData {

public Id sid{get;set;}
public string sname{get;set;}    


public list<Opportunity> OppRecords {get; set;}
public list<Opportunity> Olist = new list<Opportunity>();
 
contact cn = new contact();
string contactId = ApexPages.currentPage().getParameters().get('rec');
public OppRelatedData(ApexPages.StandardController controller) {

try{
if(ApexPages.currentPage().getParameters().get('rec')!=null){
 OppRecords = [select id,name,Amount,Sample_Hypo__c,Client_Meeting_Date__c,CloseDate,CreatedDate,Last_Activity_Date__c,LastModifiedDate,StageName,Product_Interest__c,Product_Riders__c from Opportunity WHERE Contact_Name__r.id =:ContactId  AND (not StageName LIKE '%close%') ORDER BY CloseDate ASC ];
System.debug('Opprecords'+OppRecords );
 for(Opportunity op:OppRecords){
 if(op.Sample_Hypo__c == true){
 
 Olist.add(op);
 System.debug('Olist'+Olist);
 
 }
 }
 update Olist;

}
else
System.Debug('There is no contact associated with Opportunity');
}

catch(DmlException e){
System.Debug(e.GetMessage());
}
}
}
Sameer Tyagi SFDCSameer Tyagi SFDC
Hi Avnesh, 

1. You do not need to do any DML to retrieve set of data. 
as you have written 
update Olist;
No need to write above line. 

2. Just write this line
OppRecords = [select id,name,Amount,Sample_Hypo__c,Client_Meeting_Date__c,CloseDate,CreatedDate,Last_Activity_Date__c,LastModifiedDate,StageName,Product_Interest__c,Product_Riders__c from Opportunity WHERE Contact_Name__r.id =:ContactId AND Sample_Hypo__c = true AND (not StageName LIKE '%close%') ORDER BY CloseDate ASC ];


In above query you are already fetching filtered records. So you do not need to write any loop
Just use OppRecords directly on vf page. 


Thanks & Regards,
Sameer Tyagi.
http://mirketa.com/
anvesh@force.comanvesh@force.com
yes i did the same first. But  it filtered only current opportunity record only. I want  filter on all opp records under contact.
Sameer Tyagi SFDCSameer Tyagi SFDC
Hi Anvesh, 

It will show you opportunites related to contact as you have mentioned Contact_Name__r.id =:ContactId in your query. 

and second thing -you are doing DML , which is  not required. 
update Olist;// not required

Sameer