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
Kyle ArmstrongKyle Armstrong 

Compile Error: Initial term of field expression must be a concrete SObject: List<String>

I'm working with incoming leads from vendors via email.  I'm trying to have the lead associated with the correct campaign name, but am getting the error "Compile Error: Initial term of field expression must be a concrete SObject: List<String> at line 33 column 28."  I have made the line in question bold and italics.
                       
global class CreateLeadFromEmail implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult HandleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope env) {
        
        // Create an InboundEmailResult object for returning the result of the  
        // Apex Email Service 
        String campaignName;
       
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        String convertToPlainText= '';
        // Add the email plain text into the local variable  
        convertToPlainText = email.plainTextBody;
        System.debug('Email Content: ' + convertToPlainText);   
        //check if the incoming email Lead has an email address
        if(email.plainTextBody != ''){
            String[] emailBody = email.plainTextBody.split('\n', 0);
            System.Debug(emailBody);
            String LeadEmail = emailBody[6].substringAfter(': ').trim();            
            if(String.isNotBlank(LeadEmail)){
                Integer leadCount = [select count() from Lead where Email =: LeadEmail];
                List<Lead> leadList = [SELECT Id,status FROM Lead WHERE Email = : LeadEmail];
                System.debug('Lead count from the existing Lead with the requested emailId -'+ leadCount);
                //if (leadList.size() == 0 || leadList.size() > 1) {
                    try{
                        //Create new Lead as it does not exist
                        Lead createNewLead = new Lead();
                        //Set the Lead object values from the email body
                        createNewLead = CreateOrUpdateLead(createNewLead, emailBody);
                        System.debug('Before Lead insert');
                        insert createNewLead;    
                        System.debug('After Lead insert');
                        system.debug('Inserted Lead Id = '+ createNewLead.Id);
                        //Add the Lead to the correct campaign
                        if(emailBody.subject = 'Life Insurance Lead'){
                            campaignName = 'The Lead Guys';         
                        }
                        else if (emailBody.subject == 'New Webform Lead Notification'){
                            campaignName = 'Get Seen Media';
                        }
                            
                        Campaign campaignId = [Select id from Campaign where name =: campaignName]; 
                        CampaignMember leadCampaignMember = new CampaignMember(campaignid = campaignId.id, leadid = createNewLead.Id);
                        insert leadCampaignMember;
                        System.debug('New Lead record: ' + createNewLead );   
                    }
Pankaj_GanwaniPankaj_Ganwani
Hi Kyle,

Please remove <i><b> tags from line no. 33 and then try to save.
Kyle ArmstrongKyle Armstrong
@Pankaj_Ganwani - that's odd, the <i><b> and </b></i> don't show in my lines of code when I'm in Salesforce.

@sumit singh 37 - I tried replacing line 33 of code with what you recommended, but I then get an error at line 33 column 41.
Pankaj_GanwaniPankaj_Ganwani
Hi Kyle,

You will have to write email.subject instead of writing emailBody.subject at both line 33 and 36 since subject is a property of email.
Kyle ArmstrongKyle Armstrong
Pankaj,

That specific error was taken care of, but now I receive error: Compile Error: Condition expression must be of type Boolean at line 33 column 25.

Thank you,

Kyle
sumit singh 37sumit singh 37
at line number 15 you created emailBody as a list variable , so you should use index to get the value at line nuber 33 same as line number 17

use if(emailBody[0].subject == 'Life Insurance Lead')  give the appropriate index number 
 
Mahesh DMahesh D
Hi Kyle,

Please use == instead of single = in the condition.
Himanshu MaheshwariHimanshu Maheshwari
Hi

emailBody is a Array of String. So you cannot use emailBody.subject directly. You need to specify index in array. Try below code:
 
if (emailBody[0].subject = 'Life Insurance Lead') {
	campaignName = 'The Lead Guys';
} else if (emailBody[0].subject == 'New Webform Lead Notification') {
	campaignName = 'Get Seen Media';
}
 Thanks,
Himanshu
 
Pankaj_GanwaniPankaj_Ganwani
Hi Kyle,

Use below mentioned code snippet as a replacement from line no 33 to 38, it may solve your problem:
if (email.subject == 'Life Insurance Lead') {
	campaignName = 'The Lead Guys';
} else if (email.subject== 'New Webform Lead Notification') {
	campaignName = 'Get Seen Media';
}