You need to sign in to do that
Don't have an account?
System.QueryException: List has no rows for assignment to SObject
hello in the code bellow :
//the first statement return the Right Contact using the user.contactId i want to use this contact in another query to select a record which is associated to this contact, but the problem is that i have message error " no rows for assignement SObject
List<Contact> contactname = [SELECT name, id from Contact where id =: user.contactId];
Related_Events_and_reports__c related;
related = [Select id, Event__c, Related_Contact__c, Yes__c, No__c FROM Related_Events_and_reports__c
where Related_Contact__c =:contactname];
But if i use the Id directly in the Second query i get the result as example :
related = [Select id, Event__c, Related_Contact__c, Yes__c, No__c FROM Related_Events_and_reports__c
where Related_Contact__c =:'xxxxxxxxxxxxxxx'];
so i don't know how to put the right id directly in the query
any Help would be great thanks
Hi,
try this
List<Contact> contactname = [SELECT name, id from Contact where id =: user.contactId];
Contact conObj;
if(contactname != NULL && contactname.size() > 0){
conObj = contactname.get(0);
}
Related_Events_and_reports__c related;
if(conObj != NULL){
related = [Select id, Event__c, Related_Contact__c, Yes__c, No__c FROM Related_Events_and_reports__c
where Related_Contact__c =:conObj.id];
}
If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.
Thanks
All Answers
related = [Select id, Event__c, Related_Contact__c, Yes__c, No__c FROM Related_Events_and_reports__c
where Related_Contact__c =:contactname];
to:
related = [Select id, Event__c, Related_Contact__c, Yes__c, No__c FROM Related_Events_and_reports__c
where Related_Contact__c =:contactname[0].id];
Try this:
Venkat
thanks for the quick answer i tried :
related = [Select id, Event__c, Related_Contact__c, Yes__c, No__c FROM Related_Events_and_reports__c
where Related_Contact__c =:contactname[0].id];
but it still giving me exception this time it's : System.ListException: List index out of bounds: 0
Hi,
try this
List<Contact> contactname = [SELECT name, id from Contact where id =: user.contactId];
Contact conObj;
if(contactname != NULL && contactname.size() > 0){
conObj = contactname.get(0);
}
Related_Events_and_reports__c related;
if(conObj != NULL){
related = [Select id, Event__c, Related_Contact__c, Yes__c, No__c FROM Related_Events_and_reports__c
where Related_Contact__c =:conObj.id];
}
If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.
Thanks
Thanks for the help it worked very well :)
Yes, generally the problem came when checking with a list variable in (=) in soql. So we have toassign them at first to a record by get(0) and then use it is soql.
If the posts are helpful please throw kudos to them.
i have another question, emmmm in that query :
related = [Select id, Event__c, Related_Contact__c, Yes__c, No__c FROM Related_Events_and_reports__c
where Related_Contact__c =:contactname[0].id];
i need to add another Where on Event__c , to choose the closest Event
to explain more, i have Event__c Object it has a start date and End Date, and ihave many Events, and a Contact can be related to one or many Events, so i want to retrieve the Participation Record associated to the closest Event
anyhelp please !!
Can you describe more about closest event? Means in what basis it should be fetched? Startdate or EndDate?
Anyways I'm writing a sample
related = [Select id, Event__c, Related_Contact__c, Yes__c, No__c FROM Related_Events_and_reports__c
where Related_Contact__c =:contactname[0].id and Event__r.EndDate__c = //fetching criteria];
If this post is helpful please throw Kudos.
Thanks
now i have Two Events both of them have Stard Date and end Date
the First Event Start 28/05/2013 , End Date = 30/05/2013
the Second Event Start is 05/06/2013 , End Date = 10/06/2013
and i have a contact that is subscribed in both of them and i want to show him the closest subscirbtion, do you get me now i hope ?
Yes got it. that means we have to differentiate between the startdate and enddate and fetch which event has the minimum difference and get that event. But unfortunately those minus functions might be not there in soql. It needs more research.
this is it, well Just give me a help if u have an idea of how to do that it would be great
Well, you can create a formula field by the formula,
Date.Today() - StartDate__c
and use that field to check the minimum value.
if this post is helpful please throw kudos.
thanks
thanks for the reply, i've created the formula field that calculate the minimum betwen Today and Start DAte but i have a problem in the query
related = [Select id, Event__c, Related_Contact__c, Yes__c, No__c FROM Related_Events_and_reports__c
where Related_Contact__c =:contactname[0].id and Event__r.Min_Date__c = :// what should do here ];
thanks
You may want to split your query into two. See the full code listing (not tested):
Hope this helps.
Hello Again Guys, well i have the other requests working Fine now i'll put in other context, if the Contact doesn't have any participation in an Event__c and he would like to participate i've added a New Button the create a new instence of Related_Event__c , but when i want to save the participation it gives me this Error
Thanks for the Help @Venkat
Change the order by clause on the Event__c SOQL to your liking. You know your data better than me.
Read about SOQL here: http://www.salesforce.com/us/developer/docs/soql_sosl/
Thanks,
Venkat