0

I have Cases objects in my Salesforce instance, along with Recommendation objects. For each case I would like to have multiple Recommendations. The Recommendations are not unique to the case, so the relationship I'm currently using is a many-to-many / junction object, a CaseRecommendation.

When an agent is working on Case, the edit form is quite straightforward for fields directly on the Case. However, to associate multiple recommendations with a Case, with the standard UI, it's quite cumbersome. For each Recommendation, they have to click "New" and create a new relationship:

case recommendation new button

Is there a standard way to easily insert multiple CaseRecommendations (junction objects) without having to manually create a new record every time? I'm wondering if there's something like a picklist > automatic junction object creation field.

The open ended question: If there's not a nice way to do this, is there a straightforward custom way to do it you'd reccomend?

2 Answers 2

0

Junction objects are really just another form of "child record". These are managed through the Related Lists feature, as you have illustrated. To optimize the UX would require use of custom LWC (or Lightning Aura) Component(s).

You need to decide what the user experience should be, which might be to add a dual listbox (aka slush bucket in old parlance) to the record form by embedding one within your own custom LWC, populated with available recommendations, and have that custom LWC manage creating and deleting the junction objects when the recommendations are moved between the two lists.

1
  • Makes sense. One painful part of the LWC is it can't be used on a standard record edit form, so now there's two save actions an agent needs to complete
    – Andy Ray
    Commented Nov 16, 2020 at 0:18
0

If you need to massively create records you will need to create a User Interface to let the user enter the data and then save to the database with Apex.

Using LWC you can quickly create a reusable component. If you use <lightning-record-edit-form> to take advantage of default lookup input fields to select the related records to be save in the junction object.

Then if you use the lightning-record-edit-form in a loop with a <template for:each...> you will be able show the user as much blank records as the need to insert at once.

Find this gist with a POC https://gist.github.com/jotraverso/df7e26827f0123e697118ef893ecafb6

Cheers!

Update: Updated the gist with a dynamic way to handle the data that will make the component reusable.

  /**
   * Make the saveJunctions Dynamic
   */
  @AuraEnabled
  public static void saveJunctionsDynamic(
    String jsonRecords,
    String objectAPIName
  ) {
    try {
      String listType = 'List<' + objectAPIName + '>';
      List<SObject> junctionRecords = (List<SObject>) JSON.deserialize(
        jsonRecords,
        Type.forName(listType)
      );
      insert junctionRecords;
    } catch (Exception e) {
      throw new AuraHandledException(e.getMessage());
    }
  }

You must log in to answer this question.

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