-2

I've a below code to sort the contact list without case insencitive. Code:

List<Contact> contacts = new List<Contact>();
{
    contacts = [Select id,Name from Contact where someWhereCondition here];
    for(integer i=0; i<contacts.size();i++)
    {
        contacts[i] = contacts[i].toUpperCase();
    }
    contacts.sort();
}

Error: method does not exist or incorrect signature: void toUpperCase() from the type contact

1

2
  • 2
    I assume you actually want to upper case the Name for the contact. In which case you need to change contacts[i] = contacts[i].toUpperCase(); to contacts[i].Name = contacts[i].Name.toUpperCase().
    – Phil W
    Feb 2, 2022 at 17:07
  • BTW, there is zero point in assigning a new empty list to the contacts variable in its declaration when you then immediately assign the result of an SOQL query to that variable. This is a practice I see many novice programmers do, copying and pasting what they have seen without understanding what they are doing. All this does is create unnecessary garbage for the garbage collector to collect and clean up. My recommendation; combine the variable declaration with the assignment from the SOQL query, or if you want separate lines, leave the contacts variable uninitialized (so null) until the SOQL.
    – Phil W
    Feb 2, 2022 at 17:10

2 Answers 2

1

Setting aside the quality of the code provided, when you take a little time to read and understand the error message here, things become clear.

  • The error is telling you that there is no toUpperCase() method on the Contact object.
  • Salesforce does provide a toUpperCase() method, but it's in the String class.
  • contacts[i] gives you a Contact, not a String, hence your error.

To get this to work (or at least not run into errors), you'll want to call toUpperCase() on data contained within each Contact (something like contacts[i].FirstName.toUpperCase()).

Unfortunately, the Name field on Contact is a special, composite field and is read-only. You might be able to handle this through Apex by upper-casing both the first and last name (which you'd need to include in your query) and then sorting, but the better option here would be to pretty much remove all of the work you're attempting to do here in Apex and let the SOQL query do the work for you instead.

If you create a formula field to do the upper-casing of Name (via the UPPER() function), you could use that formula field in the ORDER BY clause in a SOQL query.

0

Your code is inappropriately trying to treat a Contact SObject as a String. On the assumption you want to upper case the Contact names, you could be tempted to do the following:

List<Contact> contacts;
{
    contacts = [SELECT Id, Name FROM Contact WHERE someWhereCondition here];

    for (Contact contact : contacts) {
        contact.Name = contact.Name?.toUpperCase();
    }

    contacts.sort();
}

I removed the redundant new empty list assignment and improved the for loop by switching to a collection iteration form. Finally, this specifically upper cases the Contact names. I have also used SNO to handle null names safely.

However, as Derek F correctly said, Contact.Name is not writable in this way. There are ways around this. His suggestion to use a formula field is a good one, though here is a code-level alternative:

    List<Contact> upperCaseContacts = new List<Contact>();

    for (Contact contact : contacts) {
        Map<String, Object> fieldsByName = new Map<String, Object>(contact.getPopulatedFieldsAsMap());

        fieldsByName.put('Name', contact.Name?.toUpperCase());

        upperCaseContacts.add((Contact) JSON.deserialize(JSON.serialize(fieldsByName), Contact.class));
    }

    contacts = upperCaseContacts;

This is at best a dodge, and is expensive in CPU and memory terms, and really you should go with Derek's suggestion.

By the way, this JSON.deserialize of a serialized map is a great way to build mock data (including formula fields and composite fields) in unit tests when you don't actually want to insert data into a database.

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