TransWikia.com

Assigning field value from set help

Salesforce Asked by bblackinton on December 14, 2021

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.

One Answer

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);
        }
    }

Answered by Liron C on December 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP