function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lightning CobraLightning Cobra 

Apex Trigger Help, using INCLUDES in SOQL

Hello,
Contacts are created by users which has a multi select picklist to capture candidates skillset. 
Job records are created by users which has a multi select picklist to capture what skill set is needed. 
Requirement: Whenever a job record is created, based on what skill set is needed the system needs to find the corresponding contacts (candidates) matching atleast 1 skillset and email them job record. 
trigger JobTrigger on Job__c (after insert) {
    
    Set<String> SkillCode = new Set<String>();
    
    // when a new job is posted, based on Skill_Code__c on job record, get matching contact records based on Candi_Skill_Codes__c
    for (Job__c J : Trigger.new) {
        
        // Skill_Code__c is a multi select picklist value
        if (J.Skill_Code__c != null) {
            
            List<String> JobOrderSkillCode = J.Skill_Code__c.split(';');
            SkillCode.addAll(JobOrderSkillCode);

        }
    }

    //Candi_Skill_Codes__c is a multi select picklist value
    for (Contact C : [SELECT Id, Alert__c, Candi_Skill_Codes__c FROM Contact WHERE Candi_Skill_Codes__c INCLUDES (:SkillCode) AND Alert__c = 'Yes']) {
        // Above query returning 0 results
        // If i change the query to something like, 
        // SELECT Id, Alert__c, Candi_Skill_Codes__c FROM Contact WHERE Candi_Skill_Codes__c INCLUDES ('sfdc', 'developer') AND Alert__c = 'Yes'
        // This query returned 2 results
        // I understand bind variables cannot be used with INCLUDES but i also don't know how to proceed in this scenario, please help
        
    }
    
    //Send an email to matched contacts

}
Best Answer chosen by Lightning Cobra
Soyab HussainSoyab Hussain
Hi Lightning Cobra,

I have specially created a formula that you can directly use to fulfil your requirement of always setting a weekday on any input
 
trigger JobTrigger on Job__c (after insert) {
    
    Set<String> SkillCode = new Set<String>();
    
    // when a new job is posted, based on Skill_Code__c on job record, get matching contact records based on Candi_Skill_Codes__c
    for (Job__c J : Trigger.new) {
        
        // Skill_Code__c is a multi select picklist value
        if (J.Skill_Code__c != null) {
            
            List<String> JobOrderSkillCode = J.Skill_Code__c.split(';');
            SkillCode.addAll(JobOrderSkillCode);

        }
    }

    if( !SkillCode.isEmpty() ) {
        String query = 'SELECT Id, Alert__c, Candi_Skill_Codes__c FROM Contact WHERE  Alert__c = \'Yes\' AND Candi_Skill_Codes__c INCLUDES(';
        for(String str : SkillCode) {
            query += '\'' + str +'\',';
        }
        query = query.removeEnd(',');
        query += ')';
        for(Contact C : Database.query(query)) {

        }
    }

}
If you found it useful please appreciate my efforts and mark it as the best answer

Regards,
Soyab
 

All Answers

Soyab HussainSoyab Hussain
Hi Lightning Cobra,

I have specially created a formula that you can directly use to fulfil your requirement of always setting a weekday on any input
 
trigger JobTrigger on Job__c (after insert) {
    
    Set<String> SkillCode = new Set<String>();
    
    // when a new job is posted, based on Skill_Code__c on job record, get matching contact records based on Candi_Skill_Codes__c
    for (Job__c J : Trigger.new) {
        
        // Skill_Code__c is a multi select picklist value
        if (J.Skill_Code__c != null) {
            
            List<String> JobOrderSkillCode = J.Skill_Code__c.split(';');
            SkillCode.addAll(JobOrderSkillCode);

        }
    }

    if( !SkillCode.isEmpty() ) {
        String query = 'SELECT Id, Alert__c, Candi_Skill_Codes__c FROM Contact WHERE  Alert__c = \'Yes\' AND Candi_Skill_Codes__c INCLUDES(';
        for(String str : SkillCode) {
            query += '\'' + str +'\',';
        }
        query = query.removeEnd(',');
        query += ')';
        for(Contact C : Database.query(query)) {

        }
    }

}
If you found it useful please appreciate my efforts and mark it as the best answer

Regards,
Soyab
 
This was selected as the best answer
Lightning CobraLightning Cobra
Cheers Soyab, worked as needed.