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
Darko VukasovicDarko Vukasovic 

Need help with Contains dot notation

Hi All, 

I am trying to create a new Task for each of the filled out fields of the New Lead that I create if 3 or more fields are filled out. So if 3 or more  of the following fields(First Name, Last Name,Email, Phone, Website, Title) are filled out I would like to create a new Task for each filled out field. In addition to that I would also like to create a New Tasks with Subject:WARNING if any of the filled out fields contain word "test".

When you paste the code below in Developer Console you can see that you get an error message "Method does not exist or incorrect signature:void contains(String) from the type Object" for the following line of Code:

if(MyLead.get(NonEmptyFields.get(i)).contains('test')){

So can you please help me with getting rid of this error message.

Here below is my code:

trigger Homework5 on Lead (before insert, after insert) {

List<Task> taskToInsertList = new List<Task>();
            for (Lead myLead:Trigger.new){
                
                List<String> NameOfFields = New List<String>();
                NameOfFields.add('FirstName');
                NameOfFields.add('LastName');
                NameOfFields.add('Email');
                NameOfFields.add('Phone');
                NameOfFields.add('Website');
                NameOfFields.add('Title');
                
                List<String> NonEmptyFields = new List<String>();                                                                                  
                for(Integer i=0;i<NameOfFields.size();i++){
                    if(myLead.get(NameOfFields.get(i)) !=null){
                        NonEmptyFields.add(NameOfFields.get(i));                        
                    }
                } 
                                                
               if(NonEmptyFields.size()>=3){                
                for(Integer i=0;i<NonEmptyFields.size(); i++){
                if(MyLead.get(NonEmptyFields.get(i)).contains('test')){ 
                Task myTask = new Task();
                myTask.whoId = myLead.Id;
                myTask.Subject = 'WARNING';
                myTask.Status = 'Not Started';
                taskToInsertList.add(myTask);
            }
                      
                else{Task myTask = new Task();
                myTask.whoId = myLead.Id;
                myTask.Status = 'Not Started';
                taskToInsertList.add(myTask);}
                
             }    
           
              insert taskToInsertList;  
          }
                
       }
  }

Thank you.

Darko
Vinayak KarandeVinayak Karande
Hi Darko,
Please see the below code and Implement it in your logic.
List<Lead> leads=[select id, name from Lead];
for(lead l:leads)
{
    System.debug(l.name);
    System.debug(l.get('Name'));
    
    if(((String)l.get('Name')).contains('anil')){
        System.debug('hi');
   }
	
    if(l.name.contains('anil'))
    {
        System.debug('hey ');
    }
}

You have to typecast with String because get() method returns the object.

Mark as the best answer if it is helpful for you.
Thanks, 
Vinayak Karande