Salesforce Integration: Wrapper Class vs. Maps for Web Service Bulk Insert โ Which is Better?
lightning web components - LWC With Apex Wrapper class to Display current user and its Contact - Salesforce Stack Exchange
How do we implement a custom put() method for a wrapper class in APEX? - Salesforce Stack Exchange
salesforce - How to Access Nested Wrapper Class in Flow Apex Action? - Stack Overflow
Videos
Hi Salesforce community,
Iโm working on an integration that involves handling bulk insertion of Case records through a web service in Salesforce. I'm debating between using Wrapper Classes and Maps in my Apex implementation and would appreciate your thoughts on the following:
-
Performance: Which approach offers better CPU and memory optimization for handling high volumes of data (e.g., 10,000+ records)?
-
Governor Limits: Are there significant differences in how these approaches impact Salesforce governor limits, such as heap size or CPU time?
-
Complexity: Wrapper Classes seem to be more intuitive for handling validation and transformations, but is this extra effort justified for simpler integrations?
-
Scalability: Which approach scales better for large datasets or integrations with frequent data loads?
-
Use Cases: Are there specific scenarios where one clearly outperforms the other?
If anyone has tackled a similar integration or has insights from a performance or maintainability perspective, I'd love to hear your experiences or best practices.
Additionally, after completing the Case insert operation, I need to send a JSON response back to the web service containing the CaseNumber of all successfully inserted records. How can I efficiently achieve this in Apex, especially for large datasets?
Thanks in advance!
Since you're new to programming, I'm going to give you more help than you asked for...
First, get your indentation under control. Messy code is hard to read. Almost every IDE has a way to make your code pretty. For VS Code, the Format Document function is your friend. For the Salesforce Developer Console, you can select all your code and use Shift+Tab to make it pretty.
Second, variable names can be 40 characters long. ctct means nothing, while contactName is expressive and clearly tells anyone reading your code what is meant by this. Don't be stingy with your variable names; this isn't the 1980s where you could only use variable names that were 2 letters long.
Third, I'd suggest you avoid using underscores in your LWC names; it makes it so you have to write awkward <c-store_detailz> for no good reason. Use TitleCase for class names, and camelCase for properties. Avoid using excessive uppercase letters, because something like MY_WONDERFUL_PROPERTY will have to be written as -m-y_-w-o-n-d-e-r-f-u-l_-p-r-o-p-e-r-t-y when used in a template, which is not very legible and a pain to type.
Fourth, your Apex can be shortened to just a few lines of code. Here's my rendition of your Apex code:
public class CurrentUserAndContactController {
@AuraEnabled public static Map<String, String> theMethod() {
User currentUser = [
SELECT Contact.Account.Name, Contact.Name
FROM User
WHERE Id = :UserInfo.getUserId()
];
return new Map<String, String> {
'accountName' => currentUser.Contact.Account.Name,
'contactName' => currentUser.Contact.Name
};
}
}
Using wrappers are fine, but when you're just getting started learning, keeping things simple is ideal.
Finally, four important things about JavaScript. First, JavaScript is case sensitive. That means that you absolutely need to make sure everything is capitalized correctly everywhere. Second, you need to pay attention to parameters. (Data, Error) is not the same as ({ data, error }). Third, when you reference a variable or function in a class, you always need to use this.. Fourth, you need to remember the structure of your data between the client and server. You tried to access aVar[0], but it should have been something like this.aVar.usr.
Here's my rendition of your controller, noting that I didn't change the component name only to keep things simple.
import { LightningElement, wire, track } from 'lwc';
import theMethod from '@salesforce/apex/CurrentUserandContactController.theMethod';
export default class Store_Detailz extends LightningElement {
aVar;
contact;
user;
@wire(theMethod) userDataHandler({data, error}) {
if(data) {
this.aVar = data;
this.user = data.accountName;
this.contact = data.contactName;
}
}
}
JavaScript is case sensitive. Make sure you use the correct case. Salesforce returns to your wire handler function an object containing data and error properties, so these are the names you must use when destructuring the object. Change your code to:
@wire(theMethod)
aMtd({data, error}){
if(data){
this.aVar = data;
this.user = this.aVar.usr;
this.contact = this.aVar.ctct;
}
}
Note that I made sure that the data (stored into aVar - likely unnecessary) was assumed to be a WClass instance with the usr and ctct properties.