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
MohaMoha 

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

 

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

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

sushant sussushant sus
replace this
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];
Venkat PolisettiVenkat Polisetti

Try this:

 

List<Contact> contactname = [SELECT Name, Id
			       FROM Contact
			      WHERE Id =: user.contactId];

Related_Events_and_reports__c 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];

 

Venkat

MohaMoha

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

souvik9086souvik9086

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

This was selected as the best answer
MohaMoha

Thanks for the help it worked very well :)

souvik9086souvik9086

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.

MohaMoha

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 !!

souvik9086souvik9086

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

 

MohaMoha
thanks for the reply it's seems close to what i want but i will explain more,
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 ?
souvik9086souvik9086

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. 

 

 

MohaMoha

this is it, well Just give me a help if u have an idea of how to do that it would be great 

MohaMoha
ah sorry, we have to calculate the minimum between Today Date and Start Date of an Event and the min value let us fetch the nearest Event ,
souvik9086souvik9086

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

MohaMoha

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

Venkat PolisettiVenkat Polisetti

You may want to split your query into two. See the full code listing (not tested):

 

List<Related_Events_and_reports__c> related;

List<Contact> contacts = [SELECT Name, Id
			       FROM Contact
			      WHERE Id =: user.contactId];

if (contacts.size() > 0) {
	/* pick the first event in the order of start date */
	Event__c[] events =
		 [SELECT Id
		    FROM Event__c
		   WHERE Id IN (SELECT Event__c
FROM Related_Events_and_Reports__c
WHERE Related_Contact__c = :contacts[0].Id) ORDER BY Start_Date, End_Date LIMIT 1]; if (events.size() > 0) { related = [SELECT Id, Event__c, Related_Contact__c, Yes__c, No__c FROM Related_Events_and_reports__c WHERE Related_Contact__c =:contacts[0].Id] AND Event__c = events[0].Id; } }

 Hope this helps.

MohaMoha
Thanks (y)
MohaMoha

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

" Id not specified in an update call"
this is the methode for the New : 

public void NewRecord(){
isEdit=true;
related = new Related_Events_and_Reports__c();
}

and this is the methode for Save :


     public void save() {
        if (related == null ) {
              insert related;
      }
      try {

        else if (related != null ) {
      update related;
}

isEdit=false;

}catch(DmlException e){
ApexPages.addMessages(e);
}

 

now if i change in the if(related != null) TO  if(related.id != null){ update related; } it doesn't give me the Error but it doesn't insert in the Table

 

 

Any Help Please !!

MohaMoha
Hello Venkat, well the query worked great but there is a problem, emm if i have 3 events and the contact is subscribed in 3 of them and one of them Start date is = 04/04/2013 and the other one starts on the 06/06/2013 the result of the query choose the First One even if the event is Over
Thanks for the Help @Venkat
Venkat PolisettiVenkat Polisetti

 

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