Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mass Junction Records LWC
<template>
<lightning-card>
<lightning-button label="Save" variant="brand" onclick={handleSave}></lightning-button>
<template for:each={junctions} for:item="jEntry">
<div key={jEntry.key}>
<lightning-record-edit-form object-api-name="CaseRecommendation__c">
<lightning-input-field field-name="Recommendation__c" data-index={jEntry.key}
onchange={handleLookupChange}></lightning-input-field>
</lightning-record-edit-form>
</div>
</template>
</lightning-card>
</template>
import { api, LightningElement, track } from "lwc";
import saveJunctions from "@salesforce/apex/MassJunctionCreatorController.saveJunctions";
import saveJunctionsDynamic from "@salesforce/apex/MassJunctionCreatorController.saveJunctionsDynamic";
import { ShowToastEvent } from "lightning/platformShowToastEvent";
export default class MassJunctionCreator extends LightningElement {
@api
recordId;
@track
junctions = [];
handleLookupChange(event) {
event.preventDefault();
if (event.detail.value) {
const idx = event.currentTarget.dataset.index;
const lookupRecordId = event.currentTarget.value; //Recommendation record Id
this.junctions[idx].Recommendation__c = lookupRecordId;
}
}
handleSave(event) {
event.preventDefault();
const recordsForSave = this.junctions.filter((record) => {
return (
record.Recommendation__c !== undefined &&
record.Recommendation__c !== ""
);
});
saveJunctionsDynamic({
jsonRecords: JSON.stringify(recordsForSave),
objectAPIName: "CaseRecommendation__c"
})
.then(() => {
const evt = new ShowToastEvent({
title: "",
message: "Records saved!!",
variant: "success"
});
this.dispatchEvent(evt);
})
.catch((error) => {
const evt = new ShowToastEvent({
title: "",
message: error.body.message,
variant: "error"
});
this.dispatchEvent(evt);
});
}
connectedCallback() {
if (this.recordId) {
for (let i = 0; i < 5; i++) {
this.junctions.push({
key: i,
Case__c: this.recordId
});
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>52.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordPage">
<property name="recordId" type="String" placeholder="Your principal record Id" label="Parent Record Id" required="true" />
<supportedFormFactors>
<supportedFormFactor type="Small" />
<supportedFormFactor type="Large" />
</supportedFormFactors>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
public with sharing class MassJunctionCreatorController {
@AuraEnabled
public static void saveJunctions(String jsonRecords) {
try {
List<CaseRecommendation__c> junctionRecords = (List<CaseRecommendation__c>) JSON.deserialize(
jsonRecords,
List<CaseRecommendation__c>.class
);
insert junctionRecords;
} catch (Exception e) {
throw new AuraHandledException(e.getMessage());
}
}
/**
* 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());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment