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
John Neilan 18John Neilan 18 

Check if Text Field Contains Picklist Value

I have a trigger that is designed to write unique values of a Picklist from a child object (VIP_Type__c) to the associated Account record. I am having trouble figuring out how to convert the picklist value to a string value to check if the Account field contains the picklist value. The line of code I have is:
if(!a.VIP_Types__r.contains(vip.VIP_Type__c))

Where vip.VIP_Type__c is the picklist field and VIP_Types__r is the text field on the Account.  I am getting an error:

"Method does not exist or incorrect signature: void contains(String) from the type List<VIP_Type__c>"

Any ideas?  My full trigger is below:
 
trigger UpdateAcctVIP on VIP_Type__c (after insert, after update, after delete){
        
        Set<Id> acctIds = new Set<ID>();
        
        // Get all the Account Ids in the Set
        for(VIP_Type__c vip : Trigger.new){
            acctIds.add(vip.Account__c);
        }
        // QUery the Accounts
        List<Account> acct = new List<Account>();
        // Use the VIP Types to get all the related Types for the Account
        acct = [SELECT Id, VIP_Types__c
                FROM Account
                WHERE Id in :acctIds];
        
        // Iterate over each Account and VIP record
        for(Account a : acct){
                if(a.Id !=NULL){
                    for(VIP_Type__c vip: a.VIP_Types__r){
                        if(!a.VIP_Types__r.contains(vip.VIP_Type__c)){ // Check if the Type is already in the Account Field. if not add it otherwise skip
                            a.VIP_Types__c += vip.VIP_Type__c + ';';
                        }
                    }
                }
        }
        // Update the Account
        update acct;
    }

 
Best Answer chosen by John Neilan 18
David Zhu 🔥David Zhu 🔥
You may refer to the code below:
trigger UpdateAcctVIP on VIP_Type__c (after insert, after update, after delete){
        
        Set<Id> acctIds = new Set<ID>();
        
        // Get all the Account Ids in the Set
        if (Trigger.isDelete)
        {
            for(VIP_Type__c vip : Trigger.old){
                acctIds.add(vip.Account__c);
            }

        }
        else
        {
            for(VIP_Type__c vip : Trigger.new){
                acctIds.add(vip.Account__c);
            }
        }
        // QUery the Accounts
        List<Account> acct = new List<Account>();
        // Use the VIP Types to get all the related Types for the Account
        acct = [SELECT Id, VIP_Types__c,(Select VIP_Type__c FROM VIP_Types__r)
                FROM Account
                WHERE Id in :acctIds];
        
        // Iterate over each Account and VIP record
        for(Account a : acct){

            for(VIP_Type__c vip: a.VIP_Types__r){
                if(!a.VIP_Types__c.contains(vip.VIP_Type__c)){ // Check if the Type is already in the Account Field. if not add it otherwise skip
                    a.VIP_Types__c += vip.VIP_Type__c + ';';
                }
            }

        }
        // Update the Account
        update acct;
    }

 

All Answers

David Zhu 🔥David Zhu 🔥
You may refer to the code below:
trigger UpdateAcctVIP on VIP_Type__c (after insert, after update, after delete){
        
        Set<Id> acctIds = new Set<ID>();
        
        // Get all the Account Ids in the Set
        if (Trigger.isDelete)
        {
            for(VIP_Type__c vip : Trigger.old){
                acctIds.add(vip.Account__c);
            }

        }
        else
        {
            for(VIP_Type__c vip : Trigger.new){
                acctIds.add(vip.Account__c);
            }
        }
        // QUery the Accounts
        List<Account> acct = new List<Account>();
        // Use the VIP Types to get all the related Types for the Account
        acct = [SELECT Id, VIP_Types__c,(Select VIP_Type__c FROM VIP_Types__r)
                FROM Account
                WHERE Id in :acctIds];
        
        // Iterate over each Account and VIP record
        for(Account a : acct){

            for(VIP_Type__c vip: a.VIP_Types__r){
                if(!a.VIP_Types__c.contains(vip.VIP_Type__c)){ // Check if the Type is already in the Account Field. if not add it otherwise skip
                    a.VIP_Types__c += vip.VIP_Type__c + ';';
                }
            }

        }
        // Update the Account
        update acct;
    }

 
This was selected as the best answer
John Neilan 18John Neilan 18
Thanks David.  The trigger compiles without error, but when I run it, I get an error:  

UpdateAcctVIP: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.UpdateAcctVIP: line 34, column 1

I added a few Debug statements to the trigger and I am getting records coming through with data. The VIP_Types__c field on the Account I am testing is empty and I would actually want the trigger to re-set and re-populate that field each time.  Would the problem be with line 31
for(VIP_Type__c vip: a.VIP_Types__r){
Since a.VIP_Types__r is empty?
 
David Zhu 🔥David Zhu 🔥
Yes. you may check if it is null;

if ( a.VIP_Types__r != null) {
   for(VIP_Type__c vip: a.VIP_Types__r){
       ......
   }
}
John Neilan 18John Neilan 18
Thanks. I don't think I explained very well. I would like to loop through all the VIP_Type__c records and compile a list of the unique values for each related Account that would then get written to the VIP_Types__c text field on the account separated by semi-colons.

If I add the IF statement above, it will just bypass the loop anytime the Account field is empty, correct?  Instead, I still want it to enter the loop and just populate the VIP_Types__c field on the account with the 1st value of VIP_Type__c from the loop. Sorry, I hope that makes sense, and thanks so much for your help with this!
David Zhu 🔥David Zhu 🔥
I am wondering if there is no VIP_Type__c record associated to an account, how could you get vip_type__c value.
John Neilan 18John Neilan 18
Sorry, the naming conventions used are not the greatest and somewhat confusing.  VIP_Type__c is the custom object with a master-datail to the account and also the API name of the picklist field on  that custom object.  VIP_Types__c is the custom text area field on the Account.

The For loop looks like it's trying to loop through all the custom object records that have a value in the Account VIP_Types__c field, but since that field is empty it throws the error.  I think that's what's happening.