You need to sign in to do that
Don't have an account?
John 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:
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:
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; }
All Answers
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 Since a.VIP_Types__r is empty?
if ( a.VIP_Types__r != null) {
for(VIP_Type__c vip: a.VIP_Types__r){
......
}
}
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!
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.