Currently, I am trying to create some Case Tags for my Case during an after insert event.

Essentially, all I am doing is taking a legacy field and splitting it, getting all the distinct tags. However, I don't know how to create a new CaseTag record.

Here is the code I have so far for my handler:

public static void CreateCaseTagsFromLegacyTags(List<Case> casesWithTags)
    List<CaseTag> newCaseTags = new List<CaseTag>();

    for(Case caseWithTag : casesWithTags)
        //I pre-filtered the records prior, so this value is never null
        //or empty
        List<String> tags = caseWithTag.LegacyTags__c.split(' ');

        for(String tag : tags)
            newCaseTags.add(new CaseTag(/* What goes there? */));

    INSERT newCaseTags;

So has anyone created a tag through apex before? I don't see why that not. I tried finding any example on how to do that and I could not find a single case where it was done (pun intended). So if there is any way to make this possible, please let me know.


Check out the docs on what the CaseTag object is comprised of.


From what I can read, you'll need to replace CaseTag(/* What goes there? */) with the following:

    Name = tag, //tag name
    ItemId = caseWithTag.Id, //Id of Tagged Item -- in your scenario, this is the case
    Type = 'Public' //other option is 'Private'

Hope this helps.

  • If you change TagDefinitionId to ItemId, it will save. I will be testing my import shortly. To confirm if it works. Jul 29 '15 at 15:36
  • Shoot, good catch! I'll update the answer,
    – James
    Jul 29 '15 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.