1

Scenario:

I have one contact "John Doe" who owns multiple and different type properties. In the property object, each has a "Type" and "Sub-Type". In the "type" picklist, it includes Drugs stores, banks, etc. (types of commercial business) and in the "Sub-Type" are Walgreens, CVS, Chase etc.

Is it possible to create a Multi-picklist in CONTACT that whatever properties he has will be in the multi-picklist field?

Ex. John Doe is the owner of Walgreens and Chase back. So he has two (2) property records. Property Record 1 "type" says Drug Store with a "sub-type" saying Walgreens. And Property record 2 "type" says Bank and a "Sub-type" of Chase.

BUT in the Contact record itself says... Type: Drug Store; Bank & Sub-Type: Walgreens; Chase

Is this possible? Can you guide me or show me a sample code that I can mess with?

Thanks in advance

2

I was thinking you can resolve that via workflow but it seems like you can't update a multi picklist field. So here is a quick code that I wrote on the fly to explain the logic of how this can be achieved. You need to adjust it based on your object/field names and also extend it to check for duplicate properties (I assume you already have a control over creating duplicate properties on the same contact).

trigger PropertyTrigger on Property__c (after insert, after update, after delete)
{
    // Find all the parent contact IDs from the property records in the trigger
    Set <Id> contactIds = new Set <Id> ();

    for (Property__c property : trigger.isDelete ? trigger.old : trigger.new)
    {
        contactIds.add(property.Contact__c);
    }

    // Create a map of the parent contact Id and the actual contact record (instantiate via the ID)
    Map <Id, Contact> contactsMap = new Map <Id, Contact> ();

    // Query all property records related to all affected contacts in this trigger
    for (Property__c property : [SELECT Id, Type__c, Sub_Type__c, Contact__c FROM Property__c WHERE Contact__c IN :contactIds])
    {
        // First time, put entry in the map and instantiate the contact record
        if (contactsMap.get(property.Contact__c) == null)
        {
            contactsMap.put(property.Contact__c, new Contact(Id = property.Contact__c));
        }

        // No type on the contact, let's put the type from the first related property
        if (contactsMap.get(property.Contact__c).Type__c == null)
        {
            contactsMap.get(property.Contact__c).Type__c = property.Type__c + ';';
        }
        // The contact has some type set already (from another property), let's append the other property
        // multi picklist values are separated with semi colon
        else
        {
            contactsMap.get(property.Contact__c).Type__c += property.Type__c + ';';
        }

        // Same for sub type
        if (contactsMap.get(property.Contact__c).Sub_Type__c == null)
        {
            contactsMap.get(property.Contact__c).Sub_Type__c = property.Sub_Type__c + ';';
        }
        else
        {
            contactsMap.get(property.Contact__c).Sub_Type__c += property.Sub_Type__c + ';';
        }
    }

    update contactsMap.values();
}
3
  • I'm having an issue here for (Property__c property : [SELECT Id, Type__c, Sub_Type__c, Contact__c FROM Property__c WHERE Contact__c IN :contactIds]) – Ninja King Jun 25 '15 at 19:34
  • line 15 it says that... No such column 'Contact__c' on entity 'Property__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. What am I missinfg – Ninja King Jun 25 '15 at 19:35
  • That'd be the name of your lookup relationship to the contact object, I assumed the API names, you need to replace them with your own – Boris Bachovski Jun 25 '15 at 22:05

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.