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
Cloud AtlasCloud Atlas 

Apex List

Hi All,

Developed below trigger on Lead...initially I forgot to add Record type under LIst..But even after that correction , I am getting the following error..

Error: Compile Error: Initial term of field expression must be a concrete SObject: List<RecordType> at line 31 column 30 

trigger AH_Lead_Address_Campaign on Lead(before insert, before update){   
    List<RecordType> rt = [SELECT id, name, developername FROM recordtype WHERE developername = 'MAH'];    
    Campaign c = [SELECT id FROM Campaign WHERE name = 'LS2' limit 1]; 
     for(Lead l: Trigger.New) 
    {     
        if(l.address__c == null || l.address__c == '')        
        l.Address__c = l.street;    
        if(l.city__c == null || l.city__c == '')        
        l.City__c = l.city;   
        if(l.state__c == null || l.state__c == '')        
        l.State__c = l.state;            
        if(l.Zip__c == null || l.Zip__c == '')        
        l.Zip__c = l.postalcode;       
        if(l.RecordTypeId == rt.Id)      ......Error Line
        l.Campaign__c = c.Id;    
    }
}        
        
can some one please point what am I doing wrong??
Any help is appreciated.
Thanks!

Ashish
Best Answer chosen by Cloud Atlas
Suraj GharatSuraj Gharat
"rt" variable is of type List. Access it using index as below:
 
if(l.RecordTypeId == rt[0].Id)

 

All Answers

Suraj GharatSuraj Gharat
"rt" variable is of type List. Access it using index as below:
 
if(l.RecordTypeId == rt[0].Id)

 
This was selected as the best answer
Cloud AtlasCloud Atlas
Thank Suraj.
The error got eliminated. But test class wouldnot pass...

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AH_Lead_Address_Campaign: execution of BeforeInsert
caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.AH_Lead_Address_Campaign: line 15, column 1: []... below line...
List<Campaign> c = [SELECT id FROM Campaign WHERE name = 'LS2' limit 1]; 

So I made the below correction...
f(l.RecordTypeId == rt[0].Id)      
        l.Campaign__c = c[0].Id; .... this line

The test class passed and all functionalities are working except this capaign field is not getting populated.

Can you take a look please. The campaign field is a custom look up field which should get populated with a specific value "LS2" when RT is "MAH". 
William TranWilliam Tran
Change your declaration 

from 
Campaign c = [SELECT id FROM Campaign WHERE name = 'LS2' limit 1]; 

to
List<Campaign> c = [SELECT id FROM Campaign WHERE name = 'LS2' limit 1]; 

thx
Suraj GharatSuraj Gharat
Hi Ashish,

If your code runs without errors and test class passes too, then only reason that I can see for "Campaign__c" field not populating is lead's record type is not "MAH". 

This is just a guess. You need to cross check once on lead's record type and also check the record type SOQL returns expected results or not. Moreover there may be case that after you set "Campaign__c", it gets changed to blanks because of some other code or rule.
Cloud AtlasCloud Atlas
I fixed the code.
Thank you both for taking time to reply.

@William... I had already done what you were recommending.. ( see my previous response) ... Still there was error.

@Suraj... Record type is available. I had to put in an if condition to check if "c" has some value.Error got removed and test passed with 82% coverage.

trigger AH_Lead_Address_Campaign on Lead(before insert, before update){   
    List<RecordType> rt = [SELECT id, name, developername FROM recordtype WHERE developername = 'MAH'];    
    List<Campaign> c = [SELECT id FROM Campaign WHERE name = 'LS2' limit 1]; 
     for(Lead l: Trigger.New) 
    {     
        if(l.address__c == null || l.address__c == '')        
        l.Address__c = l.street;    
        if(l.city__c == null || l.city__c == '')        
        l.City__c = l.city;   
        if(l.state__c == null || l.state__c == '')        
        l.State__c = l.state;            
        if(l.Zip__c == null || l.Zip__c == '')        
        l.Zip__c = l.postalcode;       
        if(c.size() > 0) 
        {
         if(l.RecordTypeId == rt[1].Id)      
            l.Campaign__c = c[0].Id;    
        }
}