+ Start a Discussion
AviKesariAviKesari 

invalid bind expression type of contact for Id field of sobject account

I have a trigger on TASK and handler class which is on After Insert and After Update, updates custom fields on account.

for Task - contact (whoId) is required field.
when a user logs a call from contact, he may not enter the WhatID(Account). So i'm trying to query and get the WhatId from WhoId of a task adn also trying to Bulkify Code.

HandlerClass:

 public void updateAccountActivityFields(Task[] taskRecords){
 Set<String> conIDs = new Set<String>();
 for(Task t : taskRecords){
            conIDs.add(t.WhoID);
        } 
//query outside forloop
//Account and contact have lookup relationship
 List<Contact> accIDs = [SELECT AccountId FROM Contact WHERE Id IN :conIDs];

//getting ERROR at this line 
List<Account> accountsToUpdate = [SELECT Id FROM Account WHERE Id IN :accIDs];

 for(Task t: [SELECT Id, LastModifiedBy.name, type, LastModifiedDate, CreatedDate, RecordTypeId, WhoID, WhatID FROM task WHERE id in :taskRecords]){
for(Account a : accountsToUpdate){
//Logic here
}
}
}

 
Best Answer chosen by AviKesari
ManishKlkrnManishKlkrn
Hi @AviKesari

Please find the code below, which helps in bulkification and also eliminates the invalid bind error.

Public with sharing class TaskHandler{

    public void updateAccountActivityFields(Task[] taskRecords){
        Set<String> conIDs = new Set<String>();
         for(Task t : taskRecords){
                    conIDs.add(t.WhoID);
                }
//Changed the below line to remove the error and bulkify the same.
        Map<Id,Contact> conmap = new Map<Id,Contact>([SELECT AccountId,lastname,firstname FROM Contact WHERE Id IN :conIDs]);

        List<Account> accountsToUpdate = [SELECT Id FROM Account WHERE Id IN :conmap.keyset()];
        
// Suggesting you to use the Maps where ever possible, as I am not sure what your logic is.
        for(Task t: [SELECT Id, LastModifiedBy.name, type, LastModifiedDate, CreatedDate, WhoID, WhatID FROM task WHERE id in :taskRecords]){
            for(Account a : accountsToUpdate){
            //Logic here
            }
        }
    }
}

P.S: Mark this as the best answer if it helped you. Cheers!
Manish

All Answers

@anilbathula@@anilbathula@
Hi AviKesari,

You want to update the task Account id or you want to update account fields once task is created or updated?

Thanks
Anil.B
ManishKlkrnManishKlkrn
Hi @AviKesari

Please find the code below, which helps in bulkification and also eliminates the invalid bind error.

Public with sharing class TaskHandler{

    public void updateAccountActivityFields(Task[] taskRecords){
        Set<String> conIDs = new Set<String>();
         for(Task t : taskRecords){
                    conIDs.add(t.WhoID);
                }
//Changed the below line to remove the error and bulkify the same.
        Map<Id,Contact> conmap = new Map<Id,Contact>([SELECT AccountId,lastname,firstname FROM Contact WHERE Id IN :conIDs]);

        List<Account> accountsToUpdate = [SELECT Id FROM Account WHERE Id IN :conmap.keyset()];
        
// Suggesting you to use the Maps where ever possible, as I am not sure what your logic is.
        for(Task t: [SELECT Id, LastModifiedBy.name, type, LastModifiedDate, CreatedDate, WhoID, WhatID FROM task WHERE id in :taskRecords]){
            for(Account a : accountsToUpdate){
            //Logic here
            }
        }
    }
}

P.S: Mark this as the best answer if it helped you. Cheers!
Manish
This was selected as the best answer
AviKesariAviKesari
Thanks for the reply manish:

List<Account> accountsToUpdate = [SELECT Id FROM Account WHERE Id IN :conmap.keyset()];

when i debug accountsToUpdate  is null always, please can you me!
AviKesariAviKesari
when i just debug for conmap.keyset(). I see the result.