0

I'm using the following code to associate Contact to Account based on Domain field (type=text).

trigger AssociateContact on Contact (before insert) 
{
    List<String> contactEmaildomains = new List<String>();
    for(Contact contact:Trigger.new)

   {  if(contact.Autocreated__c && string.isNotBlank(contact.Domain__c))
        contactEmaildomains.add('%'+contact.Domain__c+'%');
    }
    System.debug(contactEmaildomains);
    List<Account> accounts = [SELECT 
            Id, Domain__c 
         FROM 
            Account
         WHERE 
            Domain__c LIKE :contactEmaildomains];
    System.debug(accounts);

    Map<String, Id> domains = new Map<String, Id>();

    for(Account record: accounts) 
    {
        for(String dom:record.Domain__c.Split(','))
            domains.put(dom, record.Id);
    }
    for(Contact record: Trigger.new) 
    {
        if(domains.get(record.Domain__c) != null ) 
        {
            record.AccountId = domains.get(record.Domain__c);
        } else if (string.isNotBlank(record.email) && record.Autocreated__c == true) { record.AccountId = '0016100000J1K3c';}

    }
}

I'd like to change the field type to Long Text Area. In order to do that, I need to update the trigger to avoid this error:

field 'Domain__c' can not be filtered in query call

I hope that someone can help me with that.

2

If you couldn't comment out the code,
You can trade it as string. so that sfdc would not detect the field.
need to change: contact.Domain__c
to: (string) contact.get('Domain__c')

Also change the query to dynamic query:
List<Account> accounts = Database.query('SELECT Id, Domain__c FROM Account ' + ' WHERE Domain__c LIKE '+ :contactEmaildomains);
Since Long Text Area can not be filtered. you should change query using sub query.

List<Id> accountIds = new List<Id>();
for(Contact contact:Trigger.new) {
    accountIds.add(contact.AccountId);
}
List<Account> accountList = Database.Query(
'SELECT Id, Domain__c,' +
       '(SELECT Id FROM Contacts WHERE Autocreated__c = true) ' +
'FROM Account WHERE Id IN '+ :accountIds);

Then you can loop accountList for further use.

  • Thank you. I update the trigger and now I'm encountering with the following error: Apex trigger AssociateContact caused an unexpected exception, contact your administrator: AssociateContact: execution of BeforeInsert caused by: System.QueryException: field 'Domain__c' can not be filtered in query call: () – Lihod Rachmilevitch Mar 17 '16 at 7:43
  • 1
    Long Text Area can not be filtered. you should change query using sub query. – ling Mar 17 '16 at 7:47

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.