2

Is it possible to populate lookup field on Case with ID of custom object using Apex Class?

We have 2 fields and 1 object: RC__c on Account (picklist) Responsibility_Center__c on Case (lookup) Responsibility_Center__c custom object

Criteria if: Account.RC__c (picklist) == Responsibility_Center__c.Name (custom object) Set the Responsibility_Center__c on Case

I tried to find some examples to modify the code, but unfortunatelly I don't think that I am doing it right. And I am newbie in Apex, so it is hard to write without any examples

public with sharing class CaseTriggerService {
    public static void findRC(List<Responsibility_Center__c> records)
    {
        Set<String> RcIds = new Set<String>();
        for (Responsibility_Center__c record : records) RcIds.add(record.Name);

        Map<String, Case> cases = new Map<String, Case>();
        for (Case cs : [
            SELECT Id, Responsibility_Center__c, Account.RC__c FROM Case
            WHERE Responsibility_Center__c IN : RcIds
        ]) cases.put(cs.Responsibility_Center__c, cs);

        for (Responsibility_Center__c record : records)
            if (cases.containsKey(record.Responsibility_Center__c))
                record.Name = cases.get(record.Responsibility_Center__c).Id;
    }
}
9
  • Hi! - As I see Responsibility_Center__c on Case is a Text field, not a lookup. Could you provide more details? Commented Sep 7, 2022 at 19:35
  • 1
    Cześć Piotrze! :) Responsibility_Center__c on Case is custom lookup field to Responsibility_Center__c object. Needs to be filled everytime when Case is created, based on RC__c field (picklist) field on Account.
    – ApexNewbie
    Commented Sep 7, 2022 at 19:54
  • I need to match RC__c (picklist field) on Account with the Responsibility Center record Name, and then put the id of Responsibility Center record into Case record. I hope I explained :)
    – ApexNewbie
    Commented Sep 7, 2022 at 19:56
  • I also edit the description to show which field and object we have to match each other :)
    – ApexNewbie
    Commented Sep 7, 2022 at 19:59
  • chat.stackexchange.com/rooms/139052/… Please join :) Commented Sep 7, 2022 at 20:12

1 Answer 1

0

I have no idea if it makes sense.

Probably you should replace [SELECT Id, Responsibility_Center__c, Account.RC__c FROM Case] with something more relevant.

So.

  1. We go through cases. I assumed, that there could be more cases where Account.RC__c is the same. This is why I added the list.
  2. After that SOQL retrieves all Responsibility_Center__c with a specific name.
  3. Fill case.Responsibility_Center__c with proper Id.

Feel free to ask. I would help. :)

Map<Id, List<Case>> responsibilityCenterNameToCases = new Map<Id, List<Case>>();

for (Case case : [SELECT Id, Responsibility_Center__c, Account.RC__c FROM Case]) {
    List<Case> cases = responsibilityCenterNameToCases.get(Account.RC__c);
    if (cases == null) {
        cases = new List<Case>();
    }
    cases.add(case);
    responsibilityCenterNameToCases.put(Account.RC__c, cases);
}

Set<String> responsibilityCenterNames = responsibilityCenterNameToCases.keySet();
List<Case> casesToUpdate = new List<Case>();

for (Responsibility_Center__c responsibilityCenter : [SELECT Id, Name FROM Responsibility_Center__c WHERE Name IN :responsibilityCenterNames]) {
    for (Case case : responsibilityCenterNameToCases.get(responsibilityCenter.Name)) {
        case.Responsibility_Center__c = responsibilityCenter.Id;
        casesToUpdate.add(case);
    }
}

update case;

You must log in to answer this question.

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