0

I am trying to get the code below to throw and error message if a picklist field (VIP_Type__c) on a custom object already exists in the Parent's (Contact) related list.

I am getting 2 errors and I don't know what they mean or how to resolve. Can anyone help?

Line 19 - Method does not exist or incorrect signature: void contains(String) from the type Set<VIP_Type__c>

Line 27 - Method does not exist or incorrect signature: void add(String) from the type Set<VIP_Type__c>

    Set<Id> acctIds = new Set<ID>();
    Set<Id> contIds = new Set<ID>();
    map<Id, set<VIP_Type__c>> contact_type_map = new map<Id, set<VIP_Type__c>>();

// Get all the Account & Contact Ids in the Set 
        for(VIP_Type__c vip : Trigger.new){
            acctIds.add(vip.Account__c);
            contIds.add(vip.Contact__c);
            contact_type_map.put(vip.Contact__c, new set<VIP_Type__c>{vip});
    }

        List<VIP_Type__c> vipRecs = [SELECT Id,Account__c,Contact__c,VIP_Type__c
                                    FROM VIP_Type__c
                                    WHERE Contact__c in:contIds AND ID NOT in:trigger.new];

//Check for VIP Types already entered for Contact
    for(VIP_Type__c objVIP: vipRecs){

        if(contact_type_map.get(objVIP.Contact__c).contains(objVIP.VIP_Type__c)){
system.debug('@@@### - Error message:  '+contact_type_map.get(objVIP.Contact__c));


                trigger.newMap.get(objVIP.VIP_Type__c).addError('This Type already exists for this Contact');

        }
        else{
            contact_type_map.get(objVIP.Contact__c).add(objVIP.VIP_Type__c);
        }
    }
1

You've got some false assumptions in your logic. If we presume that you want a unique combination of account, contact, and VIP type, you need a different map. Here's a close approximation to what you need:

Set<Id> acctIds = new Set<ID>();
Set<Id> contIds = new Set<ID>();
Map<VIP_Type__c, VIP_Type__c> contactTypeMap = new Map<VIP_Type__c, VIP_Type__c>();
// Get all the Account & Contact Ids in the Set 
for(VIP_Type__c vip : Trigger.new) {
    acctIds.add(vip.Account__c);
    contIds.add(vip.Contact__c);
    VIP_Type__c key = new VIP_Type__c(Contact__c=vip.Contact__c, Account__c=vip.Account__c, VIP_Type__c=vip.VIP_Type__c);
    if(contactTypeMap.put( key, vip ) != null) {
        contactTypeMap.get( key ).addError( 'This type already exits for this Contact' );
    }
}

List<VIP_Type__c> vipRecs = [SELECT Id,Account__c,Contact__c,VIP_Type__c
                            FROM VIP_Type__c
                            WHERE Contact__c = :contIds AND Account__c = :acctIds];
//Check for VIP Types already entered for Contact
for(VIP_Type__c vip: vipRecs) {
    VIP_Type__c key = new VIP_Type__c(Contact__c=vip.Contact__c, Account__c=vip.Account__c, VIP_Type__c=vip.VIP_Type__c);
    if(contactTypeMap.containsKey(key)) {
        contactTypeMap.get(key).addError('This Type already exists for this Contact');
    }
}
1
  • Thanks. I got rid of the error messages, but now I'm running into something strange. The if statement for the error message I think is meant to see if the contactTypeMap contains the values of the record being updated, which it does not. However, the error message is still triggering, even though I can see in the log that the values are different. Any ideas on that?
    – J. Neilan
    Sep 4 '20 at 21:09
0

Methods on a collection need to be given an argument of the same type as the type contained in the collection.

You have your set declared as Set<VIP_Type__c>, a Set of VIP_Type__c SObjects. The data you're trying to pass to the contains() method is someRecord.VIP_Type__c, which is a string.

String != SObject type.

Change Set<VIP_Type__c> to Set<String>, and the errors should be resolved.

As a side note, this is a good illustration of why naming things is important1. Having a VIP_Type__c field on your VIP_Type__c custom SObject is allowed, but it ends up making it easier to make mistakes in code.

1: ...and one of the 2 "hard problems" in computer science, those being: cache invalidation, naming things, ...and off-by-one errors

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.