0

I am trying to write a test class for the following trigger:

trigger WebToCaseCountry on Case (after insert)
{

    Set<String> countries = new Set<String>();
    for (Case c : Trigger.new)
    {
        if (!String.isEmpty(c.CountryWebFormText__c) && c.Newsletter__c == TRUE)
        {
            countries.add(c.CountryWebFormText__c);
        }
    }

    if (countries.size() > 0)
    {
        Set<String> filteredCountries = new Set<String>();
        for (Area__c a : [select Name from Area__c where Name in :countries])
        {
            filteredCountries.add(a.Name);
        }
        if (filteredCountries.size() > 0)
        {
            Case[] caseUpdates = new Case[] {};
            Contact[] contactUpdates = new Contact[] {};
            for (Case c : Trigger.new)
            {
                if (filteredCountries.contains(c.CountryWebFormText__c))
                {
                    caseUpdates.add(new Case(Id = c.Id, Country_of_Origin__c = c.CountryWebFormText__c));
                    if (c.ContactId != null)
                    {
                        contactUpdates.add(new Contact(Id = c.ContactId, Country__c = c.CountryWebFormText__c));
                    }
                }
            }
            update caseUpdates;
            update contactUpdates;
        }
    }
}

But I am getting an error with the insert statement when I try to execute it in Execute Anonymous. This is the error that I receive: "Line: 2, Column: 1 System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, WebToCaseCountry: execution of AfterInsert caused by: System.StringException: Invalid id: CA Trigger.WebToCaseCountry: line 28, column 1: []"

This is my Test Class at the moment:

@isTest public class WebToCaseCountryTest {

public static testMethod void testTrigger()
{
    //Case c = new Case (Id = '500g000000CoMzp');
    Case caseObj = new Case(//Id = c.Id, 
                            CountryWebFormText__c ='CA', Newsletter__c = TRUE);
    insert caseObj;
    System.debug(caseObj);

    System.assert(caseObj.Country_of_Origin__c == caseObj.CountryWebFormText__c);


}

}

6
  • Have you accidentally made Case.Country_of_Origin__c and ID field instead of a text field?
    – Keith C
    Commented Aug 19, 2015 at 17:28
  • Country_of_Origin__c is actually a lookup field to Area__c, could that be the problem?
    – Nik
    Commented Aug 19, 2015 at 17:31
  • Yeah, you need to assign the Area.Id field so you should make filteredCountries a Map<String, Id> and then you can lookup the required ID from the name.
    – Keith C
    Commented Aug 19, 2015 at 17:33
  • I changed it to Map and made all the other necessary changes so now the insert works but not when I try to execute this statement - System.assert(caseObj.Country_of_Origin__c == caseObj.CountryWebFormText__c); Is there something else I need to do here as it is a lookup field?
    – Nik
    Commented Aug 19, 2015 at 17:39
  • Yes. Something like System.assertEquals(caseObj.Country_of_Origin__c, [select Id from Area__c where Name = :caseObj.CountryWebFormText__c].Id); should work. But what you really need to do is read up a bit more about Id values and names in Salesforce and get how they work clear in your mind. But you will also need to insert the Area__c object in the test unless you use the bad practice of @isTest(SeeAllData=true).
    – Keith C
    Commented Aug 19, 2015 at 17:45

1 Answer 1

1

Based on the comment thread, use something like:

    Set<String, Id> filteredCountries = new Map<String, Id>();
    for (Area__c a : [select Name, Id from Area__c where Name in :countries])
    {
        filteredCountries.put(a.Name, a.Id);
    }
    if (filteredCountries.size() > 0)
    {
        Case[] caseUpdates = new Case[] {};
        Contact[] contactUpdates = new Contact[] {};
        for (Case c : Trigger.new)
        {
            if (filteredCountries.containsKey(c.CountryWebFormText__c))
            {
                Id areaId = filteredCountries.get(c.CountryWebFormText__c);
                caseUpdates.add(new Case(Id = c.Id, Country_of_Origin__c = areaId));
                if (c.ContactId != null)
                {
                    contactUpdates.add(new Contact(Id = c.ContactId, Country__c = areaId));
                }
            }
        }
        update caseUpdates;
        update contactUpdates;
    }

You must log in to answer this question.

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