Hello I'm new to Salesforce, and need help with a trigger for cases: What I'm trying to do is display what is in a custom field 'Affiliates__c' on a task that is created for the case. I've created a new custom field on the Task object 'field1__c' so that when the case record is created the trigger will take the value of the 'Affiliates' field and insert it into the 'field1' field that I created. The error that I get when I try to save the trigger is:

Compile Error: Method does not exist or incorrect signature: [String].add(Id) at line 13 column 11

Any help would be greatly appreciated!

Here's my Trigger code:

trigger taskTrigger on Task (before insert) { 

  List<Id> casesIdOnThisTask = new List<Id>();   

  String CaseIdPrefix = Case.sObjectType.getDescribe().getKeyPrefix();     

  //Save only those Ids that belong to cases.   
  for(task t :trigger.new){ 

      //Run this logic if the task have an empty field1 if this info was already set no need to run again      
      if(t.WhatId.startsWith(CaseIdPrefix) == (t.field1__c = null) ){ 



    if(CaseIdPrefix.size() > 0){         
           //Select Only the cases we need directly on map       
          Map<id,Case>caseMap = new Map<id,Case>([select Id, Affiliates__c from Case WHERE id in: CaseIdPrefix]); 
          for(task t :trigger.new){ 

  • I would personally suggest at looking into using the Process Builder. It's a bit simpler than trying to write code for such a simple task.
    – sfdcfox
    Commented Jan 27, 2017 at 2:46

2 Answers 2


The specific compile error is on this line


CaseIdPrefix is not a collection (set or list), hence you can't use the add() method.

I think you made a simple typo and it should be


You need to use a collection to store the WhatId values (likely a Set). For example:

Set<Id> whatIds = new Set<Id>();
for (Task record : trigger.new)
  • I highly doubt just this fix will help the user, as per his req affiliates from case has to be inserted into field__c on task. The trigger is on before task insert, so to me the logic of writing trigger on task seems wrong, unless I understood it wrong.
    – Rao
    Commented Jan 26, 2017 at 23:19
  • It would fix the core misconception that caused the compile fail in the first place. It looks to my eye like the goal is to pull data down from the parent case when a task is related to one, so the parent data should already exist.
    – Adrian Larson
    Commented Jan 26, 2017 at 23:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .