You need to sign in to do that
Don't have an account?

Apex Trigger to update a lookup field
Ineed help withthe following Apex Trigger:
trigger ForceSourceCrossRef on Lead (before insert, before update ) {
For (Lead l:Trigger.new) {
if(l.Source__c != null) {
List<CrossRef__c> cr = new List<CrossRef__c>();
cr = [Select Id from CrossRef__c WHERE
CrossRef__c.Name = :l.Source__c ORDER BY Id LIMIT 1];
if(cr.size()>0){
cr.Source_Cross_Reference__c = cr[0].Id;
}
}
}
}
Attempting to update a custom lookup field on the Lead object called Source_Cross_Reference__c with the value that's imported in a Lead field called Source__c. (Source__c is a picklist field.) There is a custom object called CrossRef__c that uses the Source__c picklist value in the CrossRef__c.Name field. I want to populate the Source_Cross_Reference__c field with the ID value from the CrossRef__c object. The Devleoper Console is displaying the following Problem: "Initial term of field expression must be a concrete SObject: List<CrossRef__c>".. What is neededto correct this problem? Will this trigger perform the update described earlier?
trigger ForceSourceCrossRef on Lead (before insert, before update ) {
For (Lead l:Trigger.new) {
if(l.Source__c != null) {
List<CrossRef__c> cr = new List<CrossRef__c>();
cr = [Select Id from CrossRef__c WHERE
CrossRef__c.Name = :l.Source__c ORDER BY Id LIMIT 1];
if(cr.size()>0){
cr.Source_Cross_Reference__c = cr[0].Id;
}
}
}
}
Attempting to update a custom lookup field on the Lead object called Source_Cross_Reference__c with the value that's imported in a Lead field called Source__c. (Source__c is a picklist field.) There is a custom object called CrossRef__c that uses the Source__c picklist value in the CrossRef__c.Name field. I want to populate the Source_Cross_Reference__c field with the ID value from the CrossRef__c object. The Devleoper Console is displaying the following Problem: "Initial term of field expression must be a concrete SObject: List<CrossRef__c>".. What is neededto correct this problem? Will this trigger perform the update described earlier?
I think I misunderstood your field and objects. Here you go with new code revision: Hope this works as per your expectations
All Answers
Hope this helps! if yes, then mark it as solution.
Thanks,
Sumit
Will this trigger perform the update described earlier ?
A.You have to update the field on lead object not the CrossRef__c object .This is how you should do .trigger ForceSourceCrossRef on Lead (before insert, before update ) {
For (Lead l:Trigger.new) {
if(l.LeadSource != null) {
List<CrossRef__c> cr = new List<CrossRef__c>();
cr = [Select Id from CrossRef__c WHERE
CrossRef__c.Name = :l.LeadSource ORDER BY Id LIMIT 1];
if(cr.size()>0){
l.Source_Cross_Reference__c = cr[0].Id;
}
}
}
}
This code needs ot be bulkified .
Thanks,
Sailaja
I think I misunderstood your field and objects. Here you go with new code revision: Hope this works as per your expectations