+ Start a Discussion
Haseeb Ahmad 9Haseeb Ahmad 9 

ERROR System.QueryException: List has no rows for assignment to SObject

I am getting this ERROR System.QueryException: List has no rows for assignment to SObject on the following lines of code:
 
public static void insertCase(Case caseData) {
        insert caseData;
        Opportunity unitOpportunity = [select Id from Opportunity where Id =: caseData.OppToCase__c limit 1];  
        unitOpportunity.Case__c = caseData.Id;
        update unitOpportunity; 
    }

I tried to use try and catch but then I was not able to link the case back to opportunity, probably I am missing something very small but not able to figure this one out, can someone help, thank you. 
Suraj Tripathi 47Suraj Tripathi 47

Hi Haseeb,

Your code is correct. You check it, you have created an opportunity record related to the case.

Or you can also try this:-
Open your anonymous window:-

opportunity op= new opportunity();
op.name='abc';
op.stageName='closed won';
op..closedDate=date.today();
insert op;

case ca= new case();
ca.suppliedEmail='xyz@gmail.com';
ca.OppToCase__c=op.id;
ca.Origin='phone';
ca.Status='new';

YourClassName.insertCase(ca);
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
Suraj Tripathi 47Suraj Tripathi 47
public static void insertCase(Case caseData) {
        insert caseData;
        Opportunity unitOpportunity = [select Id,Case__c  from Opportunity where Id =: caseData.OppToCase__c && caseData.OppToCase__c!=null limit 1];  
        unitOpportunity.Case__c = caseData.Id;
        update unitOpportunity; 
    }

And also use this code
Haseeb Ahmad 9Haseeb Ahmad 9
Hi Alani and Suraj,

I have tried both of your codes now I am not getting that error but it did not link the case back to opportunity. 

How can I fix that? this is exactly the same behavior when I tried to use try and catch
Haseeb Ahmad 9Haseeb Ahmad 9
Hi Suraj I am not able to use this one 
 
public static void insertCase(Case caseData) {
        insert caseData;
        Opportunity unitOpportunity = [select Id,Case__c  from Opportunity where Id =: caseData.OppToCase__c && caseData.OppToCase__c!=null limit 1];  
        unitOpportunity.Case__c = caseData.Id;
        update unitOpportunity; 
    }

Because I get an error "Logical operator can only be applied to Boolean"
Suraj Tripathi 47Suraj Tripathi 47
Hi haseeb,

Sorry for that. Please replace and in place of the && operator.
Opportunity unitOpportunity = [select Id,Case__c  from Opportunity where Id =: caseData.OppToCase__c and caseData.OppToCase__c!=null limit 1];  
        unitOpportunity.Case__c = caseData.Id;
        update unitOpportunity;

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
 
Haseeb Ahmad 9Haseeb Ahmad 9
Hi Suraj,

By doing that I am getting this now: 

Id =: caseData.OppToCase__c and caseData.OppToCase__c!=null limit
                                ^
ERROR at Row:1:Column:75
Didn't understand relationship 'caseData' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
Suraj Tripathi 47Suraj Tripathi 47
Hi Haseeb,

Okay you have tried this code:-
Opportunity unitOpportunity = [select Id,Case__c  from Opportunity where Id =: caseData.OppToCase__r and caseData.OppToCase__r!=null limit 1];  
        unitOpportunity.Case__c = caseData.Id;
        update unitOpportunity;

 
Haseeb Ahmad 9Haseeb Ahmad 9
Same error:

Id =: caseData.OppToCase__r and caseData.OppToCase__r!=null limit
                                ^
ERROR at Row:1:Column:75
Didn't understand relationship 'caseData' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
Suraj Tripathi 47Suraj Tripathi 47
what is the dataType of OppToCase__c field?
Haseeb Ahmad 9Haseeb Ahmad 9
Its a lookup to opportunity on case object 
Suraj Tripathi 47Suraj Tripathi 47
Hi Haseeb,

Please try this code:-
public class OppAndCase{
    public static void insertCase(Case ca) {
        insert ca;
        if(ca.OppToCase__c!=null){
            Opportunity unitOpportunity = [select Id,case__c  from Opportunity where Id =: ca.OppToCase__c limit 1];  
            unitOpportunity.Case__c = ca.Id;
            update unitOpportunity;
        }
    }
}


And open your anonymous window:-
opportunity op= new opportunity();
op.name='abccc';
op.stageName='closed won';
op.closeDate=date.today();
insert op;

case ca= new case();
ca.suppliedEmail='xyz@gmail.com';
ca.OppToCase__c=op.id;
ca.Origin='phone';
ca.Status='new';

OppAndCase.insertCase(ca);


In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer.