+ Start a Discussion
Jacek DJacek D 

Can't get this trigger to work - any ideas? (Populate Lookup field with content from text field)

trigger Update_Lookup__c_on_Text_ID__c on Lead (before update, before insert) {
        for (Lead l : Trigger.new){
        if(l.Lookup__c !=l.Text_ID__c){
        l.Lookup__c=l.Text_ID__c;
        }
        }
}

 I'm trying to run above trigger when importing new leads. I need the content from field "Text_ID__c" to populate the custom lookup field "Lookup__c". any idea what I'm doing wrong?

 

I'm pretty new to this...

Best Answer chosen by Admin (Salesforce Developers) 
spraetzspraetz

That looks fine, what error is it throwing?

 

is the data in Text__c a record id?

 

Lookups in Apex require record IDs, not names.

All Answers

spraetzspraetz

That looks fine, what error is it throwing?

 

is the data in Text__c a record id?

 

Lookups in Apex require record IDs, not names.

This was selected as the best answer
Jacek DJacek D

So that might be the case: the value is not a record ID. I was under the impression I could force content from the field into the lookup. Is there no way to do that?

spraetzspraetz

You'll have to use a SOQL query to retrieve the record ID of the record you are referncing in Text_ID__c and populate the lookup__c field with that ID.

Jacek DJacek D

Sounds good. Can you point me into the direction? I've never built a SOQL query...

 

I appreciate it!

spraetzspraetz

Googling Apex SOQL will give you some good results to get started.

 

SOQL is essentially a query language similar to SQL.

 

What you'll want to do is something like

 

l.Lookup__c = [SELECT id FROM Lead WHERE some_field = :l.Text_ID__c].id

 

That will find the Lead you're looking for and put the id in the Lookup__c field.

 

What kind of data are you storing in the Text_ID__c field?

Jacek DJacek D

It's zip codes I want to store there. The Lookup__c points to the zip code database.

 

I would need to build a query that looks for the record ID in the zip database that equals Text_ID__c and populates that into the lookup__c, correct?

Jacek DJacek D
trigger Update_Market_ID_on_Market_Update on Lead (before update, before insert) {
        for (Lead l : Trigger.new){
        l.Market__c = [SELECT id FROM Market__c WHERE Name =:l.Market_Record_ID__c].id;
        }
}

 Thanks so far, I think I got the idea. Above code produces the following error:  "list has no row for assignment". What does that mean?

spraetzspraetz

It means that query is returning zero rows. 

 

Are you sure there is a Market__c record with a NAME equal to l.Market_Record_ID__c?

Jacek DJacek D

Ah, it worked! I used the data loader to insert the leads and named the "Market_Record_ID__c" column in the import file differently. I was under the impression that would make no difference thanks to the mapping?

 

 

Jacek DJacek D

Thanks a lot, by the way! This is extremely useful!

cbrocbro

This has been helpful.  I am experiencing the same issue, however!  

 

 

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 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_Line__c WHERE Name =: cl.Contract_Header__c].id ;
        }
    }
}

 Any help would be appreciated!

 

I have also posted here: http://boards.developerforce.com/t5/Apex-Code-Development/Problem-with-SOQL-query-Error-Invalid-Data-List-has-no-rows-for/td-p/580637

 

 

Thanks,

Chris