+ Start a Discussion
NSK000NSK000 

help needed to write apex class

Hi guys,
I need some help with the following code. I'm trying to write an apex class but getting errors. Please help me out with this. Thank you in advance.


Purchase_Order__c  po=[select Original_Purchase_Order__c,POCN__c from Purchase_Order__c where id= :ApexPages.currentPage().getParameters().get('id')];
    id myid=po.Original_Purchase_Order__c;
    if(po.POCN__c==0){
        system.debug('This is the original po');
        }
        if(po.POCN__c==1){
        purchase_order__c po1=[select Purchase_Order__c.Name, Purchase_Order__c.status__c, (select Name,id from Expenditures__r) from Purchase_Order__c  where id=:myid];
        List<Expenditures__c> e1 = [select id,Name,Amount__c from Expenditures__c where PurchaseOrder__r.id= :po1.id];
        //return e1;
        system.debug('original po expenditure list '+e1);
        }
        if(po.POCN__c==2){
       purchase_order__c po1=[select Original_Purchase_Order__c,POCN__c from Purchase_Order__c  where id=:myid];
        id myid1=po1.Original_Purchase_Order__c;
        purchase_order__c po2=[select Purchase_Order__c.Name from Purchase_Order__c  where id=:myid1];
        List<Expenditures__c> e2 = [select id,Name,Amount__c from Expenditures__c where PurchaseOrder__r.id= :po2.id];
        system.debug('original po expenditure list '+e2);  
        } 
        if(po.POCN__c==3){
        purchase_order__c po1=[select Original_Purchase_Order__c,POCN__c from Purchase_Order__c  where id=:myid];
        id myid1=po.Original_Purchase_Order__c;
        purchase_order__c po2=[select Original_Purchase_Order__c,POCN__c from Purchase_Order__c  where id=:myid1];
        id myid2=po.Original_Purchase_Order__c;
        purchase_order__c po3=[select Purchase_Order__c.Name, Purchase_Order__c.status__c, (select Name,id from Expenditures__r) from Purchase_Order__c  where id=:myid2];
        List<Expenditures__c> e3 = [select id,Name,Amount__c from Expenditures__c where PurchaseOrder__r.id= :po3.id];
        system.debug('original po expenditure list '+e3);   
        }
        if(po.POCN__c==4){
        purchase_order__c po1=[select Original_Purchase_Order__c,POCN__c from Purchase_Order__c  where id=:myid];
        id myid1=po.Original_Purchase_Order__c;
        purchase_order__c po2=[select Original_Purchase_Order__c,POCN__c from Purchase_Order__c  where id=:myid1];
        id myid2=po.Original_Purchase_Order__c;
        purchase_order__c po3=[select Original_Purchase_Order__c,POCN__c from Purchase_Order__c  where id=:myid2];
        id myid3=po.Original_Purchase_Order__c;
        purchase_order__c po4=[select Purchase_Order__c.Name, Purchase_Order__c.status__c, (select Name,id from Expenditures__r) from Purchase_Order__c  where id=:myid3];    
        List<Expenditures__c> e4 = [select id,Name,Amount__c from Expenditures__c where PurchaseOrder__r.id= :po4.id];
        system.debug('original po expenditure list '+e4);   
        }
        if(po.POCN__c==5){
        purchase_order__c po1=[select Original_Purchase_Order__c,POCN__c from Purchase_Order__c  where id=:myid];
        id myid1=po.Original_Purchase_Order__c;
        purchase_order__c po2=[select Original_Purchase_Order__c,POCN__c from Purchase_Order__c  where id=:myid1];
        id myid2=po.Original_Purchase_Order__c;
        purchase_order__c po3=[select Original_Purchase_Order__c,POCN__c from Purchase_Order__c  where id=:myid2];
        id myid3=po.Original_Purchase_Order__c;
        purchase_order__c po4=[select Original_Purchase_Order__c,POCN__c from Purchase_Order__c  where id=:myid3]; 
        id myid4=po.Original_Purchase_Order__c;
        purchase_order__c po5=[select Purchase_Order__c.Name, Purchase_Order__c.status__c, (select Name,id from Expenditures__r) from Purchase_Order__c  where id=:myid4];     
        List<Expenditures__c> e5 = [select id,Name,Amount__c from Expenditures__c where PurchaseOrder__r.id= :po5.id];
        system.debug('original po expenditure list '+e5);   
        }
jigarshahjigarshah
However, can you also post the exact error that you encounter within your Debug Log and can you share details around the same. I see a couple of things that should change in your class.

1. It is always a recommended that the results set of a Soql query is contained within a List<Sobject> and not a single Sobject. Hence the first line of code to query the retrieve the Purchase Orders shoudl be modified as below.
List<Purchase_Order__c>  poList = [Select Id, Original_Purchase_Order__c, POCN__c 
								   From Purchase_Order__c 
								   Where Id = :ApexPages.currentPage().getParameters().get('id')];
								   
Id myid;							   
if(poList <> null && poList.size() > 0){
	myid = poList[0].Original_Purchase_Order__c;
}

2. Is the datatype for po.POCN__c set to Text or Number? If it is text you will need to modify the If block comparsions as below.
if(poList[0].POCN__c == '1'){
      //Your processing logic goes here
}

3. Also your conditional if blocks to compare the POCN__c value seem incorrect. Based on my understanding, the POCN__c field on Purchase Order for a single record, could have only one of the provided values i.e. either 1, 2, 3, 4, 5. However, the structure of your code requires the value of the POCN__c to be checked everytime irrespective of whether the previous block was execute due to the condition being satisfied. Hence, I would recommend changing the individual if blocks into if, elseif blocks as below. This will ensure only a single block is executed improving your code performance. 
if(poList[0].POCN__c == '1'){

}else if(poList[0].POCN__c == '2'){

}else if(poList[0].POCN__c == '3'){

}else if(poList[0].POCN__c == '4'){

} else {
	//Automatically execute the else block if no previous conditions are satisfied and the POCN__c value is 5
	if(poList[0].POCN__c == '5'){
		
	}
}

4. Moreover, I don't see the need to execute multiple Soql queries. Once you identify the the Original Purchase Order Id value within the first query, all you need to do is fetch all the associated Expenditure records assoicated with that Purchase Order and iterate and process based on the respective condition. If required use Dynamic Soql to generate the Soql query at run time by adding dynamic Where clauses.

Please do not forget to mark this thread as SOLVED and asnwer as the BEST ANSWER if it helps address your issue.
NSK000NSK000
Hi Jigarshah. Thank you for responding. My code works properly. I'm new to coding and i'm having trouble creating a controller and vf page for the above code. I need to display list of expenditures through vf page. 
jigarshahjigarshah
Where and how are you trying to display the respective page? Do you intend to display the list of Expenditure records as a related list on the standard Purchase_Order__c detail page layout or does that need to be displayed as a completely separate page having its own Tab?

Where and how is the page expected to be requested from a custom button, a tab?

Please share more details around the same so that I can help you with the appropriate code.
NSK000NSK000
I'm trying to disply expenditures list in a section. I'm calling vf page by using that section.
Thank you!
NSK000NSK000
Hi Jigarshah,
And also the POCN__C can be 'n' not limited to 5. Please help me out with the code.
Thank you