0

Working through a simple problem, but can't wrap my head around it. I suspect it's because I cannot access set elements and probably need to switch to using something else. I'm getting illegal assignment errors.

I've got a list of bills coming in that need to have their lookup field to case assigned. Here's what I have so far:

    List<Bill_Status__c> statusesToUpdate = new List<Bill_Status__c>();
    Set<String> caseKey = new Set<String>();

    for (Case caseIds : [SELECT Id, Session__c FROM Case WHERE Session__c = :session]) {
        caseKey.add(caseIds.Id);
    }

    for (Bill_Status__c stat : statusesToAdd) {
        if(caseKey.contains(stat.Bill_Lookup__c)) {
            stat.Case__c = caseKey.Id;
            statusesToUpdate.add(stat);
        }
    }

update statusesToUpdate;

statusesToAdd is being populated way above from a simple query already.

Bill_Lookup__c is a string that should match the Case_Id, and if it does, it should update the Case__c on the Bill_Status__c object with the Id from the matched case itself.

I think my approach is close, but using the wrong tools to get it done. Essentially, I have a bunch of bills that are looking for their case using the 'Bill_Lookup__c should match CaseId'. Just not sure what I'm missing.

Any help is appreciated.

1 Answer 1

1

Use map instead

List<Bill_Status__c> statusesToUpdate = new List<Bill_Status__c>();
    map<String, Case> caseKey = new map<String, Case>();

    for (Case caseIds : [SELECT Id, Session__c FROM Case WHERE Session__c = :session]) {
        caseKey.put(caseIds.Id, caseIds);
    }

    for (Bill_Status__c stat : statusesToAdd) {
        if(caseKey.containsKey(stat.Bill_Lookup__c)) {
            stat.Case__c = caseKey.get(stat.Bill_Lookup__c ).Id;
            statusesToUpdate.add(stat);
        }
    }
1
  • Thank you so much!! Jul 23, 2020 at 19:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .