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
cbrocbro 

Problem with SOQL query (Error: Invalid Data...List has no rows for assignment)

I am trying to populate Contract_Header_LOOKUP__c (a lookup field) with the id from Contract_Header__c (a text field)... hence, the SOQL query.

 

It compiles, but when I try to get it to work, I am getting this error.

 

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger ContractLine_ContractHeaderLookupUpdate caused an unexpected exception, contact your administrator: ContractLine_ContractHeaderLookupUpdate: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.ContractLine_ContractHeaderLookupUpdate: line 7, column 1

 

trigger ContractLine_ContractHeaderLookupUpdate on Contract_Line__c (before update, before insert){

    for (Contract_Line__c cl: Trigger.new)
    {
        if(cl.Contract_Header_LOOKUP__c == Null)
        {
            cl.Contract_Header_LOOKUP__c = [SELECT id FROM Contract_Line__c WHERE Name =: cl.Contract_Header__c].id ;
        }
    }
}

 

This helped me (http://boards.developerforce.com/t5/Apex-Code-Development/Can-t-get-this-trigger-to-work-any-ideas-Populate-Lookup-field/m-p/341141#M60433), but I am stuck at this point...

 

Any help is greatly appreciated!

Best Answer chosen by Admin (Salesforce Developers) 
cbrocbro

I needed to change the following:

 

[SELECT id FROM Contract_Line__c WHERE Name =: cl.Contract_Header__c].id ;

 

to

 

[SELECT id FROM Contract_Header__c WHERE Name =: cl.Contract_Header__c].id ;

 

trigger ContractLine_ContractHeaderLookupUpdate on Contract_Line__c (before insert, before update){

    for (Contract_Line__c cl: Trigger.new)
    {
        if(cl.Contract_Header_LOOKUP__c == Null)
        {
            cl.Contract_Header_LOOKUP__c = [SELECT id FROM Contract_Header__c WHERE Name =: cl.Contract_Header__c].id ;
        }
    }
}

 

All Answers

cbrocbro

I needed to change the following:

 

[SELECT id FROM Contract_Line__c WHERE Name =: cl.Contract_Header__c].id ;

 

to

 

[SELECT id FROM Contract_Header__c WHERE Name =: cl.Contract_Header__c].id ;

 

trigger ContractLine_ContractHeaderLookupUpdate on Contract_Line__c (before insert, before update){

    for (Contract_Line__c cl: Trigger.new)
    {
        if(cl.Contract_Header_LOOKUP__c == Null)
        {
            cl.Contract_Header_LOOKUP__c = [SELECT id FROM Contract_Header__c WHERE Name =: cl.Contract_Header__c].id ;
        }
    }
}

 

This was selected as the best answer
cbrocbro

How do I put an if catch in this?

 

Thanks,
Chris

jbroquistjbroquist
What do you mean by an "if catch"?
cbrocbro

like this.

 

if(contractHeaders.size()>0)
    {
    //System.debug('Chris has values to insert = '+ ContractHeaderInsert.size());//
     try{
        insert contractHeaders;
        }catch (System.Dmlexception e)  
             {
             system.debug (e); 
             }

 

but I don't know what to put in the if/insert statements with this code?  is it necessary?

I was also told I should not put a SOQL query in a for loop...

cbrocbro

how do i close the loop?

 

Thanks,

Chris