2

I'd like to add a

FROM Account WHERE Clients_for_Job_Opportunities__c INCLUDES (AVTRRT__Job__c.Account.Name)

line to my SOQL query but at the moment it looks like I can only add static values.

So really my goal is to use the includes to only look at Accounts where the AVTRRT__Job__c.Account.Name is a value in the Clients_for_Job_Opportunities__c multi-select picklist field.

Can I create a variable to hold this information within the trigger?

Thanks! PS Trigger-in-progress below:

trigger JobsEmail on AVTRRT__Job__c (before insert, before update) {
   for (AVTRRT__Job__c myJob : Trigger.new) {
      if (myJob.Send_CCP__c != NULL) { 
         List<Account> toemail = [SELECT Id, Account_Manager_eMail_1__c FROM Account WHERE Clients_for_Job_Opportunities__c INCLUDES (**AVTRRT__Job__c.Account.Name**)];
      }
   }
}
1

To do what you are looking to do you need to split the Multi-select picklist into a list of strings and use the IN function instead of INCLUDES. Something like this should work for you.

list<string> vals = account.Clients_for_Job_Opportunities__c.split(';');
string condition = ''
for(string val : vals) {
    condition += ',\'' + val + '\''
}
//remove the first comma
condition = condition.substring(1, condition.length() - 1);

string query = 'Select Id, Name FROM Account WHERE Clients_for_Job_Opportunities__c IN (' + condition + ')'
list<Account> accounts = Database.query(query);

Can't take full credit here as I learned this technique a while back from @sfdcfox. Used it a few times and works well

https://developer.salesforce.com/forums?id=906F00000008yY9IAI

1

Tried the solution mentioned above, but that din't work well for me. Modified that to work, thought it be shared, here:

Public List<String> vals {get;set;}

   Public string selectedVal{get;set;} //Will be using this in VF page to capture the user selected multiselect list values.

Public static List<sobject> sobjectList {get;set;}


    vals = selectedVal.split(';');

    string condition = '';

    for(string val : vals) {

       condition += ',\'' + val + '\'\'';

    }

    //remove the first comma

    condition = condition.substring(1, condition.length() - 1);

    String str = condition.replaceAll(',',';'); //Returns a string separated by semicolon, eg: ('A;B'), THAT IS A AND B.

    string query = 'Select Id, Name FROM Account WHERE Clients_for_Job_Opportunities__c INCLUDES (' + str + ')';//Includes works here, and str provides the string separated by Semicolon.

    sobjectList=Database.query(query);
  • Did this code really works? I think it won't even compile. – Rohit Mourya Mar 23 '17 at 11:21
0

Previous snippet allowed us to get the selected values of multiselect list as ('A;B'). This one allows us to get the values as ('A', 'B')

Public List<String> vals {get;set;}

Public string selectedVal{get;set;} //Will be using this in VF page to capture the user selected multiselect list values.

Public static List sobjectList {get;set;}

vals = selectedVal.split(';');

string condition = '';

for(string val : vals) {

   condition += '\''+val+'\''+',';

}
condition = condition.substring(0, condition.length() - 1); //Returns a string separated by semicolon, eg: ('A' , 'B'), THAT IS A OR B.

string query = 'Select Id, Name FROM Account WHERE Clients_for_Job_Opportunities__c INCLUDES (' + condition + ')';//Includes works here, and str provides the string separated by Semicolon.
Sobjectlist=Database.query(query);

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.