List has no rows for assignment - salesforce

Can any one help me in solving this error:List has no rows for assignment to SObject
Class at line :
        
qName = [select Row_Id__c, Name from Ccon__c where Id = :ApexPages.currentPage().getParameters().get('id')].Row_Id__c;
Test Class line :
Controller  controller1  = new Controller(sc1);

This mean your query is returning no result (So the list is empty and then when casting into an Sobject there is not item to take).
qName = [select Row_Id__c, Name from Ccon__c where Id = :ApexPages.currentPage().getParameters().get('id')].Row_Id__c;
I would do it this way (to make sure in any case I will get value from my query if my ID is valid):
Id tempID = null;
if(ApexPages.currentPage().getParameters().get('id') != null)
{
tempId = ApexPages.currentPage().getParameters().get('id');
}
else{ //Error management
}
List<Ccon__c> recordsToProcess = new List<Ccon__c>();
if(tempID != null)
{
recordsToProcess = [select Row_Id__c, Name from Ccon__c where Id = :tempID];
}
else{ //Error management
}
//qName Declaration
if(!recordsToProcess.isEmpty())
{
qName = recordsToProcess[0].Row_Id__c;
}
else{ //Error management
}
if(qName == null)
{
//Error management
}
If you have more questions please feel free to ask. But as a best practice you might always ensure that your list is not empty before getting the record returned.
Thanks
HqSeO

Related

Attempt to de reference a null object error

On line acct.AmountX__c += amtX this and acct.AmountY__c += amtY this it is giving me an error. The trigger is on after insert and after update. When i am inserting any record in contact then it is giving me an error i don't know why. Why should 'acct' be initialized if i am not wrong this error comes when you have not initialized something.
public class ContactAccountRollUpAmount {
public static void onAfterInsertOrUpdate(List<Contact> cont){
contAccRollUpAmount(cont);
}
public static void contAccRollUpAmount(List<Contact> contactList){
List<Contact> cont = new List<Contact>([Select AccountId, Amount_Type__c, Amount__c
from Contact
where id in: contactList]);
List<Account> accList = new List<Account>([Select id, AmountX__c, AmountY__c
from Account Limit 50000]);
List<Account> accListToUpdate = new List<Account>();
for(Account acct: accList){
Double amtX = 0;
Double amtY = 0;
system.debug('#########'+acct);
for(Contact con: cont){
if(con.AccountId == acct.Id){
if(con.Amount__c != NULL){
if(con.Amount_Type__c == 'AmountX'){
amtX = amtX + con.Amount__c;
} else if(con.Amount_Type__c == 'AmountY'){
amtY = amtY + con.Amount__c;
}
}
}
}
if(amtX != NULL){
acct.AmountX__c += amtX;
}
if(amtY != NULL){
acct.AmountY__c += amtY;
}
accListToUpdate.add(acct);
}
if(accListToUpdate.size()>0){
update accListToUpdate;
}
}
}
The problem is not that the 'acct' is not initialized, its the field 'AmountX__c' and 'AmountY__c'.
As I can see you have queried max of 50000 records, out of these records some of them have 'Null' values for the above two fields.
I suggest please check whether the above two fields have 'Null' values for some records or not.

Maximum view state size limit (135KB) exceeded

This code has been working fine for years.
public PageReference chkinst() {
i=0;
integer j=0;
Set<String> aSearchSet = new Set<String>();
List<Lead> lList = ui;
for (Lead l : lList) {
aSearchSet.add(l.company);
}
Account[] accountToCreate = new Account[]{};
Map<String,Account> companyToAccountMap = new Map<String,Account> ();
for (Account a: [select id, Name from Account where name IN :aSearchSet])
companyToAccountMap.put(a.name,a);
for (Lead l : lList) {
if (!companyToAccountMap.containsKey(l.company)){
Account act = new Account(Name = l.Company , Country__c = l.Country__c );
accountToCreate.add(act);
j = j +1;
}
}
insert accountToCreate;
if(j == 0) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'All leads have correct institution names'));
} else{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,j + ' Institutions created'));
}
return null;
}
Today, it gave me the following error:
Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 135.078KB
Does anyone know why?
Thanks,
It's hard to tell from this code snippet. But if I had to guess. It looks like your companyToAccountMap is a class level variable, and it's probably returning more records now then it used to. If you don't need that map anywhere else in the controller, I would move it to a method level or make it transient.

'Empty tree' function causing program to crash

I have a empty_tree() function that basically returns a true value if the tree is empty and returns false if it is not.
bool empty_tree(AVL self)
{
if (self == NULL)
{
return true;
}
else
{
return false;
}
}
It exists so that my program is easier to understand without comments. Now, it works fine everywhere else in my program. However when I try and use it in my insert function in place of the working:
if(self == NULL)
my program will crash. This isn't really a big deal and using the 'if (self==NULL) will suffice, but I was just wondering if anyone had any ideas as to why this would cause the program to crash. As I said, it's not extremely important so I won't bother including more code, but if anyone had any guesses, I'd appreciate hearing your thoughts.
EDIT: Upon request, here's where the section that works:
if (self == NULL)
{
self = (AVL)malloc(sizeof(struct avlNode));
self->student_id = id;
self->left = self->right = NULL;
}
And here's the code that will, when changed to this, crash the program:
if (!empty_tree(self))
{
self = (AVL)malloc(sizeof(struct avlNode));
self->student_id = id;
self->left = self->right = NULL;
}

Add more than one patient C Pointers

I'm having trouble with my pointers. I'm trying to add multiple patients to my list. I know how to do it, it's just that the code gives me seg faults.
Here is the code in question:
void addPatient(int patientID) {
Chartptr patients_chart;
patients_chart = getChart(patientID);
// if patient wasn't found, add new patient
if (patients_chart == NULL) {
Chartptr new_chart;
// allocate and initialize new patient
new_chart = (Chartptr)malloc(sizeof(Chart));
new_chart->id = patientID;
new_chart->buffer = NULL;
// insert new patient into list
new_chart->next = patientList;
patientList = new_chart;
// test print patient data
printf("%d %d\n", new_chart->id, patientList->id);
}
}
/*
* getChart: given a patientID, return a pointer to their Chart
*/
Chartptr getChart(int patientID) {
Chartptr foundChart = NULL;
// find the patient chart with id
foundChart = patientList;
if (foundChart != NULL) {
while(foundChart->id != patientID) {
foundChart = foundChart->next;
}
}
return foundChart;
}
Here are the structs:
/*
* Patient's health chart: ID + linked list of health type readings
*/
typedef struct chartEntry* Chartptr; /* pointer to a Chart */
typedef struct chartEntry{
int id; /* patient ID */
CBuffptr buffer; /* pointer to first health type buffer */
Chartptr next; /* pointer to next patient */
}Chart;
extern Chartptr patientList; /* global declaration for start of the patient chart linked list */
I'm sending to the add patient, id, which I get from main, that much I know works perfectly.
But for some reason when the patientList is not NULL and it enters the while loop it seg faults or its after the while loop in the rest of addPatient. I don't know which. Thanks for your help.
I think here is your error:
while(foundChart->id != patientID) {
foundChart = foundChart->next;
You are updating foundChart but never checking in the while loop if it had become NULL, in case no patientID matches.
getChart() has no condition stop it running off the end of the list if no match is found.
Please update code as given below it will work :D
Chartptr getChart(int patientID) {
Chartptr foundChart = NULL;
// find the patient chart with id
foundChart = patientList;
while(foundChart!= NULL) {
if(foundChart->id == patientID) {
return foundChart;
}
foundChart = foundChart->next;
}
return NULL;
}

Deleting a contact from a database using SQLite3. Delete Method is skipping the if statement and displaying the else

I am unable to delete the record. The code looks good and I am not getting any errors on the Xcode side but when I run the application and attempt to delete the record, it skips the if and goes straight to the else displaying my error code:
Error: Failed to delete contact.
I've followed all instructions and all other functions work well. I have to use SQLite3 for the delete. What is wrong with the code?
- (IBAction)deleteData:(id)sender {
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
NSString *deleteSQL = [NSString stringWithFormat:#"DELETE FROM CONTACTS (name, address, phone)VALUES (\"%#\", \"%#\", \"%#\")",
self.name.text, self.address.text, self.phone.text];
const char *delete_stmt = [deleteSQL UTF8String];
sqlite3_prepare_v2(_contactDB, delete_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
self.status.text = #"Contact Deleted";
self.name.text = #"";
self.address.text = #"";
self.phone.text = #"";
} else {
self.status.text = #"Error! Failed to delete contact";
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
}
}
Your DELETE syntax is wrong.
Please read the documentation.
You probaly want to delete a record that is identified by its ID value:
DELETE FROM Contacts WHERE id = ?

Resources