You need to sign in to do that
Don't have an account?
Jacek 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...
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
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.
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?
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.
Sounds good. Can you point me into the direction? I've never built a SOQL query...
I appreciate it!
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?
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?
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?
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?
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?
Thanks a lot, by the way! This is extremely useful!
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
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
Problem solved. :)
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