1

I am trying to find the Name variable in a list to use the contains() method to search for the Name of the Activity_Assignment__c but am getting an error:

Method does not exist or incorrect signature: void Contains(String) from the type List<Activity_Assignment__c> (64:44)

My code is below where I am seeing the issue:

        // Build out completed list of assignments -- if there are any
    List<Activity_Assignment__c> currentAssignments = [SELECT Id, Name, Status__c, Actual_Minutes__c, Onboarding_Activity__r.Expected_Duration__c, Employee__c, Employee__r.Name, Onboarding_Activity__c, Onboarding_Activity__r.Name, Onboarding_Activity__r.URL__c, Onboarding_Activity__r.Type__c, Onboarding_Activity__r.Trailhead_Points__c, Onboarding_Activity__r.Onboarding__r.Id, Onboarding_Activity__r.Onboarding__r.Name
    FROM Activity_Assignment__c WHERE Employee__c IN :contacts];

    // Loop completed assignments to set as inactive -- if there are any
    for (Activity_Assignment__c complete : currentAssignments){
        if (complete.IsActive__c = true){
            complete.IsActive__c = false;
        }
            if (!currentAssignments.IsEmpty()){
                update currentAssignments;
            }
    }

    // Build out assignments for each contact
    List<Activity_Assignment__c> assignments = new List<Activity_Assignment__c>();
    for (Contact contact : contacts) {
        if (contact.Onboarding__c != null) {

            // Retrieve the onboarding from the map
            Onboarding__c onboarding = onboardingMap.get(contact.Onboarding__c);

            // Add each assignment to list of new assignments
            for (Onboarding_Activity__c activity : onboarding.Onboarding_Activities__r) {
                if (currentAssignments.Contains(activity.Name))
                assignments.add(
                    new Activity_Assignment__c(
                        Onboarding_Activity__c = activity.Id, 
                        Employee__c = contact.Id));
            }
        }
    }

    // Insert the assignments -- if there are any
    if (!assignments.isEmpty()) {
        insert assignments;
    }
}

}

Under the "// Add each assignment to list of new assignments" I am attempting to find the name of the activity assignment in comparison to the new activity.Name and if found, do this, or do that.

Is there a way to do this with the list?

The goal here is to find any of the currentAssignments Name on the Activity_Assignment__c, and if they exist, mark them again as completed when the new list is built out.

Revised code below that worked:

// Build out completed list of assignments -- if there are any
    List<Activity_Assignment__c> currentAssignments = [SELECT Id, Name, Status__c, Actual_Minutes__c, Onboarding_Activity__r.Expected_Duration__c, Employee__c, Employee__r.Name, Onboarding_Activity__c, Onboarding_Activity__r.Name, Onboarding_Activity__r.URL__c, Onboarding_Activity__r.Type__c, Onboarding_Activity__r.Trailhead_Points__c, Onboarding_Activity__r.Onboarding__r.Id, Onboarding_Activity__r.Onboarding__r.Name
    FROM Activity_Assignment__c WHERE Employee__c IN :contacts];

    // build out set to track name (common field) for completed assignments
    Set<String> names = new Set<String>();

    // loop to add names to set
    for (Activity_Assignment__c complete : currentAssignments){
        names.add(complete.Onboarding_Activity__r.Name);
    }

    // Loop completed assignments to set as inactive -- if there are any
    for (Activity_Assignment__c complete : currentAssignments){
        if (complete.IsActive__c = true){
            complete.IsActive__c = false;
        }
            if (!currentAssignments.IsEmpty()){
                update currentAssignments;
            }
    }

    // Build out assignments for each contact
    List<Activity_Assignment__c> assignments = new List<Activity_Assignment__c>();
    for (Contact contact : contacts) {
        if (contact.Onboarding__c != null) {

            // Retrieve the onboarding from the map
            Onboarding__c onboarding = onboardingMap.get(contact.Onboarding__c);

            // Add each assignment to list of new assignments
            for (Onboarding_Activity__c activity : onboarding.Onboarding_Activities__r) {
                if (!names.Contains(activity.Name))
                assignments.add(
                    new Activity_Assignment__c(
                        Onboarding_Activity__c = activity.Id, 
                        Employee__c = contact.Id));

                        else if (names.Contains(activity.Name))
                        assignments.add(
                            new Activity_Assignment__c(
                                Onboarding_Activity__c = activity.Id, 
                                Employee__c = contact.Id,
                                Status__c = 'Completed'));

            }
        }
    }

    // Insert the assignments -- if there are any
    if (!assignments.isEmpty()) {
        insert assignments;
    }
}

}

1 Answer 1

0

You need a separate Set to track these values:

Set<String> names = new Set<String>();

...

for (Activity_Assignment__c complete : currentAssignments){
  names.add(complete.Name));

...

 if (names.contains(activity.Name))
1
  • this worked perfectly, thank you. Aug 12, 2021 at 15:53

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.