You need to sign in to do that
Don't have an account?
Kent Lichty 11
How to return class wrapper values back to apex from LWC
My organization is in the process of implementing Salesforce as its ERP, just as I am in the process of learning Force.com programming. I have been asked to develop a solution which I am just not sure is possible and, if it is, then it will require programming skills in LWC development that are currently beyond me. So I would be very grateful if anyone in this forum could offer some assistance.
The root of the problem is that, when custom objects were developed, it was not understood that Salesforce works much better when custom objects are designed in a “denormalized” manner, where a single “long” record would contain many fields. We were still thinking in “normalized” SQL terms, where narrower objects were created, linked by keys. Here is a simple, if not realistic, example of what I am attempting to describe:
We have a custom “Product Price” object which contains these three fields:
Product, Currency Code, Price.
So three records would look like this:
PRODUCT 1 USD 25.00
PRODUCT1 EUR 28.00
PRODUCT1 GBP 29.50
However, while the records are STORED in the above manner, it is realized that it is difficult and cumbersome to display/maintain the records using standard Salesforce functionality, so I have been asked to come up with a UI that would make it easier to enter/change the prices for each product, which would look something like this, in a datatable where the prices would be EDITABLE:
PRODUCT ID USD PRICE EUR PRICE GBP PRICE
PRODUCT1 25.00 28.00 29.50
PRODUCT2 50.00 54.00 _____
PRODUCT3 75.00 _____ 80.00
I am able to easily define and display a class wrapper which contains the information that I want to DISPLAY; that is no problem. The issue with which I am struggling is the UPDATE part, especially given the fact that I have no valid record ID because in this case, THREE record IDs would be involved, one for each record in the Product Price object. If I can get the list back to Apex (preferably only MODIFIED records in the list) back to Apex, then I would be good to go. But that is the part that I am not understanding: how to get my modified datatable records back to Apex so I can process them.
I am guessing that what I want to do IS POSSIBLE, by doing something like this, but the exact details are not apparent to me:
1: Add a button (Save/Update) that would call my Apex program imperatively, passing the list back to it. The button would fire a Javascript function which would do the following.
2: Using the concept of draft values, the function would read the list and only pass back to Apex the records which had been modified. But I have no clue how to do that. I assume this would require sending back either JSON or a map <String, Object> back to my Apex controller. This is the critical part that I am not understanding.
3: Once I get the list records back to Apex, I think I can handle the update part. I would probably send the 3 record IDs of the Product Price as part of my class wrapper so I could use them to get a handle on any records that I need to update.
So that is my problem. I am hoping that some other people have had the same issue and have been able to come up with a solution which currently eludes me. If anybody could just tell me in general the techniques that I could use to implement my goal, I can do the research and figure out the details. Also, if any could point me to any sites which describe a similar problem and solution, I would be exceedingly grateful.
The root of the problem is that, when custom objects were developed, it was not understood that Salesforce works much better when custom objects are designed in a “denormalized” manner, where a single “long” record would contain many fields. We were still thinking in “normalized” SQL terms, where narrower objects were created, linked by keys. Here is a simple, if not realistic, example of what I am attempting to describe:
We have a custom “Product Price” object which contains these three fields:
Product, Currency Code, Price.
So three records would look like this:
PRODUCT 1 USD 25.00
PRODUCT1 EUR 28.00
PRODUCT1 GBP 29.50
However, while the records are STORED in the above manner, it is realized that it is difficult and cumbersome to display/maintain the records using standard Salesforce functionality, so I have been asked to come up with a UI that would make it easier to enter/change the prices for each product, which would look something like this, in a datatable where the prices would be EDITABLE:
PRODUCT ID USD PRICE EUR PRICE GBP PRICE
PRODUCT1 25.00 28.00 29.50
PRODUCT2 50.00 54.00 _____
PRODUCT3 75.00 _____ 80.00
I am able to easily define and display a class wrapper which contains the information that I want to DISPLAY; that is no problem. The issue with which I am struggling is the UPDATE part, especially given the fact that I have no valid record ID because in this case, THREE record IDs would be involved, one for each record in the Product Price object. If I can get the list back to Apex (preferably only MODIFIED records in the list) back to Apex, then I would be good to go. But that is the part that I am not understanding: how to get my modified datatable records back to Apex so I can process them.
I am guessing that what I want to do IS POSSIBLE, by doing something like this, but the exact details are not apparent to me:
1: Add a button (Save/Update) that would call my Apex program imperatively, passing the list back to it. The button would fire a Javascript function which would do the following.
2: Using the concept of draft values, the function would read the list and only pass back to Apex the records which had been modified. But I have no clue how to do that. I assume this would require sending back either JSON or a map <String, Object> back to my Apex controller. This is the critical part that I am not understanding.
3: Once I get the list records back to Apex, I think I can handle the update part. I would probably send the 3 record IDs of the Product Price as part of my class wrapper so I could use them to get a handle on any records that I need to update.
So that is my problem. I am hoping that some other people have had the same issue and have been able to come up with a solution which currently eludes me. If anybody could just tell me in general the techniques that I could use to implement my goal, I can do the research and figure out the details. Also, if any could point me to any sites which describe a similar problem and solution, I would be exceedingly grateful.
You can find the complete help on the link given below:
https://salesforcediaries.com/2020/01/18/wrapper-class-in-lightning-web-component/
https://www.salesforcecodecrack.com/2020/01/using-wrapper-class-in-lighting-web.html (https://www.salesforcecodecrack.com/2020/01/using-wrapper-class-in-lighting-web.html)
Let me know if you still have any doubts.
Thanks,
Abhishek Bansal.
Firts of all you need to import that method in your lwc and then you can use the below code snippet: where saveRecord can be called from a button like this: You can use your the wrapper list property in place of this.realFormData and con would be the name of the parameter defined in the method of the class.
Let me know if you still have any doubts.
Thanks,
Abhishek Bansal.