You need to sign in to do that
Don't have an account?
How to solve teh System.queryException in trigger
Hi Friends,
I want to write the trigger on Address Object which is used to concatinate the values of city name, state name and country name into single field called "location__c". So, i wrote the trigger on Address Object for the actions of Before insert, Before update. But, while new record is inserting or updating the existing record, it shows following exception.
Exception:-
"execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.updateLocationDetails: line 5, column 1"
Trigger code:-
trigger updateLocationDetails on SWD_Address__c (before insert,before update)
{
for(SWD_Address__c addressDetails : Trigger.New)
{
SWD_City__c cityDetails = [select Id,name,State_Id__r.Name,State_Id__r.Country_Code__r.Name from SWD_City__c where name =: addressDetails.City__r.name];
String cityName = cityDetails.name; system.debug('%%%%valued of city name in trigger%%%%%'+cityName);
if(cityName!= null && cityName != ' ')
{
String stateName = cityDetails.State_Id__r.Name;
String countryName = cityDetails.State_Id__r.Country_Code__r.Name;
addressDetails.Location__c = cityName+'/'+stateName+'/'+countryName; }
}
}
Pls help me to solve this.
I agree, but taking the result in list will help you to avoid this exception as you can continue with your code scripts if the list.size() > 0
Does that make sense?
Thanks
Ankit Arora
Blog | Facebook | Blog Page
All Answers
As a best practice you should not write a query within loop.So, I would suggest you to use collections.
http://salesforcedeveloperblog.blogspot.com/2011/05/best-practices-of-triggers.html
http://wiki.developerforce.com/page/Apex_Code_Best_Practices
You are getting this exception because query is retuning null.
Hi,
Please make these changes in your code. You are getting this error because city Details return on any records , so just check that whether it returns any value or not then assign that value inside String cityName like below.
trigger updateLocationDetails on SWD_Address__c (before insert,before update)
{
for(SWD_Address__c addressDetails : Trigger.New)
{
String cityName =null;
SWD_City__c cityDetails = [select Id,name,State_Id__r.Name,State_Id__r.Country_Code__r.Name from SWD_City__c where name =: addressDetails.City__r.name];
If(cityDetails !=null)
{
cityName = cityDetails.name; system.debug('%%%%valued of city name in trigger%%%%%'+cityName);
}
if(cityName!= null && cityName != ' ')
{
String stateName = cityDetails.State_Id__r.Name;
String countryName = cityDetails.State_Id__r.Country_Code__r.Name;
addressDetails.Location__c = cityName+'/'+stateName+'/'+countryName; }
}
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Try replacing your code :
With this :
Change rest of the code because we are using list now.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Hi Jain,
Thanks for reply. Even, i changed the code as you mentioned. But, still same issue is happening.
Regards,
Phanikumar
Hi Ankit,
Thanks for reply. But, we are using where condition in query. Hence, there is no chance to get more than one record. Because, city, state and country details are stored seperately for normalization purpose. Also, we are querying from city object. so, there is no chance to get more than one record with the same name.
Pls correct me, if i am wrong.
Regards,
Phanikumar
I agree, but taking the result in list will help you to avoid this exception as you can continue with your code scripts if the list.size() > 0
Does that make sense?
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Hi Ankit,
Thanks for your answer.
Regards,
Phanikumar