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 

How to get rid of Error Message for Contain dot notation

Hi All, 

I have already posted this question but I didn't get the answer I was looking for.

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
Best Answer chosen by Darko Vukasovic
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below code.
 
trigger Homework5 on Lead ( 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){
                        system.debug('into this');
                        NonEmptyFields.add(NameOfFields.get(i));                        
                    }
                } 
                                                
               if(NonEmptyFields.size()>=3){                
                for(Integer i=0;i<NonEmptyFields.size(); i++){
                  //String  fieldvalue;
                    system.debug(NameOfFields.get(i));
                    system.debug(myLead.get(NameOfFields.get(i)));
               String fieldvalue=String.valueof(myLead.get(NameOfFields.get(i)));
                if(fieldvalue.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;  
          }
                
       }
  }

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below code.
 
trigger Homework5 on Lead ( 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){
                        system.debug('into this');
                        NonEmptyFields.add(NameOfFields.get(i));                        
                    }
                } 
                                                
               if(NonEmptyFields.size()>=3){                
                for(Integer i=0;i<NonEmptyFields.size(); i++){
                  //String  fieldvalue;
                    system.debug(NameOfFields.get(i));
                    system.debug(myLead.get(NameOfFields.get(i)));
               String fieldvalue=String.valueof(myLead.get(NameOfFields.get(i)));
                if(fieldvalue.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;  
          }
                
       }
  }

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Shan PontingShan Ponting
Good reply.
Darko VukasovicDarko Vukasovic
Hi Sai,

Thank you very much for your response. This is exactly what I was looking for. 

Can you please also explain why you used String.valueof() method in this case, I would like to understand this since i am just starting to learn to code. 

Best Regards,

Darko

 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Darko,

As we can use contains() only with string field so we are taking the string value of the field using String.valueof() .

Thanks,