2

I am trying to gather Account record Ids so I can re-parent some Contact records. I only need to gather records where a certain Account field matches the same value as the corresponding Contact field.

I keep getting an error for my SOQL statement.

Error: Compile Error: unexpected token: 'SELECT' at line 10 column 111

Why can I not gather a list of Accounts based on a field value being in a list of Contacts? Please let me know where I took a wrong turn.

trigger reparentcontacts on Contact (after insert) {

List<Contact> unparentedcontacts = new List<Contact>();
for (Contact i : Trigger.new) {
    if (i.recordtypeid == 'xxxxxxxxxxxx') {
        unparentedcontacts.add(i); 
    }   
}

    List<Account> parentaccounts = [SELECT id, D_U_N_S_Number__c FROM Account WHERE D_U_N_S_Number__c IN :(SELECT id, DUNS_Number__c FROM Contact WHERE id IN :unparentedcontacts)];

    for(Contact j : unparentedcontacts) {
    j.Account = parentaccounts.id;
    }

    update unparentedcontacts;


}
4
  • 2
    you can/t query DUNS_Number__c field semi joiner can only query id fields..
    – Ratan Paul
    Mar 21, 2016 at 3:16
  • 1
    @Mercury86 I have a solution for you that will work .You just need to split your queries .Let me know how this goes Mar 21, 2016 at 3:17
  • 1
    @Mercury86 go with Mohith's solution that is the correct option.
    – Ratan Paul
    Mar 21, 2016 at 3:18
  • Thank you both! Seems like it should work, right now I got to iron out the j.account = parentaccounts.id; line. Throwing a compile error: "Initial term of field expression must be a concrete SObject:". Once I iron that out, I will let you all know if it works, which it will! Mar 21, 2016 at 3:23

1 Answer 1

3

You can capture all the necessary DUNS_Number__c in a set and use the set to fetch your accounts

//rest of your code

set<String> setDNSNo = new set<String>();
for(Contact c :SELECT id, DUNS_Number__c 
                                     FROM Contact 
                                     WHERE id IN :unparentedcontacts){
    setDNSNo.add(c.DUNS_Number__c);
}
List<Account> parentaccounts = [SELECT id, D_U_N_S_Number__c 
                                           FROM Account 
                                           WHERE D_U_N_S_Number__c IN :setDNSNo];

You must log in to answer this question.

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