You need to sign in to do that
Don't have an account?
System.QueryException: List has no rows for assignment to SObject
Hello,
I have a custom objet with no more than 1 record per year per account. but I can have account with no record for the previous year.
For 1 specific field I want to get the value and set this value in the field for this year record.
If I have a 2009 record all work fine but if I don't have I get an error. I assume because I don't have result for req2.
I am not a expert and did not find how to set 0.0 as value to req2 if I have no result
Thanks for your help
Pat
Double req2 = [select Aesthetic_patients_treated__c from Yearly_Account_Plan__c where start_date__c = last_year and Account__c = :System.currentPageReference().getParameters().get('id')].Aesthetic_patients_treated__c; Yearly_account_plan__c Req; Public Yearly_account_plan__c getReq(){ for (Yearly_account_plan__c rqs : [select id ,year__c, account__c,contact__c,Express_Potential__c, Aesthetic_patients_treated__c, name, CFY_Volume_Plan__c, Open_days__c, Q4_Volume_plan__c, Q3_Volume_plan__c ,Q2_Volume_plan__c, Q1_Volume_plan__c,Actual_vs_Target_CFY__c, Actual_vs_Target_CQTR__c,Actual_vs_Target_PQTR__c, Maxi_Anterior_Cost__c,Maxi_Full_Cost__c,Maxi_Teen_Cost__c,Mini_Anterior_Cost__c, Mini_Full_Cost__c, Mini_Teen_Cost__c,Q1_Objective_Actions__c,Q2_Objective_Actions__c,Q3_Objective_Actions__c, Q4_Objective_Actions__c,Teen_Target_CFY__c, Express_Target_CFY__c, Anterior_Target_CFY__c, Full_Target_CFY__c from Yearly_Account_Plan__c where start_date__c = this_year and Account__c = :System.currentPageReference().getParameters().get('id')]){ req = rqs; return req; } if (req == null) Req = new yearly_account_plan__c(); req.Account__c = System.currentPageReference().getParameters().get('id'); req.contact__c = con.id; req.start_date__c = d; req.Aesthetic_patients_treated__c = req2; return req; }
You can wrap your query inside a try and catch clause, simiar to JAVA, and inside the catch clause set the value of req2 to 0.0.
Ex:
Double req2; try { req2 = [select Aesthetic_patients_treated__c from Yearly_Account_Plan__c where start_date__c = last_year and Account__c = :System.currentPageReference().getParameters().get('id')].Aesthetic_patients_treated__c; } catch (System.QueryException e) { req2 = 0.0; }
All Answers
You can wrap your query inside a try and catch clause, simiar to JAVA, and inside the catch clause set the value of req2 to 0.0.
Ex:
Double req2; try { req2 = [select Aesthetic_patients_treated__c from Yearly_Account_Plan__c where start_date__c = last_year and Account__c = :System.currentPageReference().getParameters().get('id')].Aesthetic_patients_treated__c; } catch (System.QueryException e) { req2 = 0.0; }
Hello,
Thanks a lot it works.
Pat