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
Vinicius ZVinicius Z 

Trigger that create a task after insert in opportunity, compared by a multi-select picklist

I am new at force.com dev and I am working in a project that need to do one trigger after insert that works like this:

I will add an Opportunity, that have a field called "Contact by" (Multi-select picklist, values: email, phone), when value is email, The trigger will create a TASK and the field Subject of task get value of "Contact by" and ActivityDate get today + 5 days.

I know that work with workflows and its the best pratices, but I want to make this on a trigger, could you help me ?
Chidambar ReddyChidambar Reddy
Hi Diogo,

When you want to create Task only when Contact_by__c is Email 
trigger CreateEmailTask on Opportunity (after insert){
   List<Task> taskInsertList = new List<Task>();
   for(Opportunity opp : [SELECT Id, Contact_by__c FROM Opportunity WHERE Id IN : trigger.newMap.KeySet() AND Contact_by__c INCLUDES ('Email')]){
      taskInsertList.add(new Task(OwnerId = UserInfo.getUserId(), WhatId = opp.Id, ActivityDate = System.today().addDays(5), Subject = 'Email'));
   }
   if(taskInsertList.size()>0)
       insert taskInsertList;
}

 
Vinicius ZVinicius Z
Right, I get it. It works well. PERFECT.

But the field contact_by__c has another value, how can I solve this ? Let think that we have 4 values, Email, Phone, Site, Facebook... Each one need to do this way, I dont know, but the multi-select picklist make me confused. I was reading about split method, but does not work..

 
Chidambar ReddyChidambar Reddy
Hi Diogo,

Do you want to create a Task with the value present in it?
when the Multi-select picklist has Phone, Email, Site, Facebook
  1. How may tasks do you want to create when you select Phone and Email in the multi-select? and What are their subjects respectively?

I am assuming that you would want to create all Tasks that you select in the Multi-Select
trigger CreateEmailTask on Opportunity (after insert){
   List<Task> taskInsertList = new List<Task>();
   for(Opportunity opp : trigger.new]){
      if(opp.Contact_by__c != NULL)
          for(String s : opp.Contact_by__c.split(';'))
             taskInsertList.add(new Task(OwnerId = UserInfo.getUserId(), WhatId = opp.Id, ActivityDate = System.today().addDays(5), Subject = s));
   }
   if(taskInsertList.size()>0)
       insert taskInsertList;
}


 
Vinicius ZVinicius Z
That's good. I understand now. It works.

The last thing that I'm confused: 

If I want to addDays to Email=5, Phone=8, Site=15 and Facebook = 4?

I've tried to create a primitive integer field to put in addDays(); but doesn't work.
Vinicius ZVinicius Z
I do like this and it works, thank you so much Chidambar.
  1. trigger CreateEmailTask on Opportunity (after insert){
    List<Task> taskInsertList = new List<Task>();
    for(Opportunity opp : trigger.new){
      integer i = 0; 
      
      if(opp.Contact_by__c == 'Email'){
          i = 2;
      }
      if(opp.Contact_by__c == 'Phone'){
          i = 5;
      }
      if(opp.Contact_by__c == 'Facebook'){
          i = 10;
      }
      if(opp.Contact_by__c == 'Site'){
          i = 20;
      } 
    
      if(opp.Contact_by__c != NULL)
             if(opp.Contact_by__c == 'Email'){}
    
              for(String s : opp.Contact_by__c.split(';'))
    
                 taskInsertList.add(new Task(OwnerId = UserInfo.getUserId(), WhatId = opp.Id, ActivityDate = System.today().addDays(i), Subject = s));
    
       }
    
       if(taskInsertList.size()>0)
    
           insert taskInsertList;
    
    }
Vinicius ZVinicius Z
Failed. I dont know how to solve this. I've tested and the conditions when more than one value are in field dont work.

How can I do this ?

 
Chidambar ReddyChidambar Reddy
Hi Diogo, 

Try this
trigger CreateEmailTask on Opportunity (after insert){
   List<Task> taskInsertList = new List<Task>();
   for(Opportunity opp : trigger.new]){
      if(opp.Contact_by__c != NULL)
          for(String s : opp.Contact_by__c.split(';')){
             Integer i = 0; //set the default value here
             if(s=='Email')
                 i=2;
             else if(s=='Phone')
                 i=5;
             else if(s=='Facebook')
                 i=10;
             else if(s=='Site')
                 i=20;
             //add more conditions if you want
             taskInsertList.add(new Task(OwnerId = UserInfo.getUserId(), WhatId = opp.Id, ActivityDate = System.today().addDays(i), Subject = s));
          }//End of inner for 
   }//End of outer for
   if(taskInsertList.size()>0)
       insert taskInsertList;
}

 
Vinicius ZVinicius Z
Perfect, solved!

I understand now. Chidambar really thank you.

When I get any questions again, I return to post.

See u