Contents

Pass sObject to an Apex Method From LWC Component๐Ÿ“ฒ

ย 

One of the most interesting and useful thing i came across while working with LWC, is the ability to pass an ๐˜ด๐˜–๐˜ฃ๐˜ซ๐˜ฆ๐˜ค๐˜ต from LWC component to Apex๐Ÿง™. This actually saves us from writing wrapper classes and assigning the fields from the same in apex in case we need to update the same object.๐Ÿช„

If you have not used it yet, here is an example๐Ÿ”ฌ:

๐Ÿ‘‰Lets say, you need to update some fields on an Account from a LWC component based on user action. Of course ๐˜ถ๐˜ฑ๐˜ฅ๐˜ข๐˜ต๐˜ฆ๐˜™๐˜ฆ๐˜ค๐˜ฐ๐˜ณ๐˜ฅ from '๐˜ญ๐˜ช๐˜จ๐˜ฉ๐˜ต๐˜ฏ๐˜ช๐˜ฏ๐˜จ/๐˜ถ๐˜ช๐˜™๐˜ฆ๐˜ค๐˜ฐ๐˜ณ๐˜ฅ๐˜ˆ๐˜ฑ๐˜ช' should be the first option we should be consideringโœ…. But what if user normally should not have FLS to the field ๐Ÿค”๐Ÿ’ญ. Or the org is poorly setup and you dont have the time or authority to fix things.

This is where we can utilize the approach which i was mentioning above.๐Ÿ“ˆ

๐Ÿ‘‰You can create a js object with field to edit from LWC and pass it to an Apex method without using any wrapper.๐Ÿš€

 ๐˜ญ๐˜ฆ๐˜ต ๐˜ณ๐˜ฆ๐˜ค๐˜ฐ๐˜ณ๐˜ฅ = {
 ๐˜ด๐˜ฐ๐˜ฃ๐˜ซ๐˜ฆ๐˜ค๐˜ต๐˜›๐˜บ๐˜ฑ๐˜ฆ: "๐˜ˆ๐˜ค๐˜ค๐˜ฐ๐˜ถ๐˜ฏ๐˜ต",
 ๐˜๐˜ฅ: ๐˜ต๐˜ฉ๐˜ช๐˜ด.๐˜ณ๐˜ฆ๐˜ค๐˜ฐ๐˜ณ๐˜ฅ๐˜๐˜ฅ,
 ๐˜๐˜ช๐˜ฆ๐˜ญ๐˜ฅ๐˜›๐˜ฐ๐˜œ๐˜ฑ๐˜ฅ๐˜ข๐˜ต๐˜ฆ__๐˜ค: "๐˜•๐˜ฆ๐˜ธ ๐˜๐˜ข๐˜ญ๐˜ถ๐˜ฆ"
 };

๐Ÿ‘‰And then you can pass the object to an apex method from LWC component ๐Ÿš€

 ๐˜ถ๐˜ฑ๐˜ฅ๐˜ข๐˜ต๐˜ฆ๐˜ˆ๐˜ค๐˜ค๐˜ฐ๐˜ถ๐˜ฏ๐˜ต๐˜๐˜ช๐˜ฆ๐˜ญ๐˜ฅ๐˜ด({๐˜ข๐˜ค๐˜ค๐˜ฐ๐˜ถ๐˜ฏ๐˜ต๐˜›๐˜ฐ๐˜œ๐˜ฑ๐˜ฅ๐˜ข๐˜ต๐˜ฆ:๐˜ณ๐˜ฆ๐˜ค๐˜ฐ๐˜ณ๐˜ฅ})
 .๐˜ต๐˜ฉ๐˜ฆ๐˜ฏ((๐˜ณ๐˜ฆ๐˜ด) => ๐˜ค๐˜ฐ๐˜ฏ๐˜ด๐˜ฐ๐˜ญ๐˜ฆ.๐˜ญ๐˜ฐ๐˜จ("๐˜ณ๐˜ฆ๐˜ค๐˜ฐ๐˜ณ๐˜ฅ ๐˜ถ๐˜ฑ๐˜ฅ๐˜ข๐˜ต๐˜ฆ๐˜ฅ"))
 .๐˜ค๐˜ข๐˜ต๐˜ค๐˜ฉ((๐˜ฆ๐˜ณ๐˜ณ๐˜ฐ๐˜ณ) => ๐˜ค๐˜ฐ๐˜ฏ๐˜ด๐˜ฐ๐˜ญ๐˜ฆ.๐˜ฆ๐˜ณ๐˜ณ๐˜ฐ๐˜ณ("๐˜ฆ๐˜ณ๐˜ณ๐˜ฐ๐˜ณ: " + ๐˜‘๐˜š๐˜–๐˜•.๐˜ด๐˜ต๐˜ณ๐˜ช๐˜ฏ๐˜จ๐˜ช๐˜ง๐˜บ(๐˜ฆ๐˜ณ๐˜ณ๐˜ฐ๐˜ณ)));

๐Ÿ‘‰Now the Apex method can written as below๐Ÿš€:

๐˜ฑ๐˜ถ๐˜ฃ๐˜ญ๐˜ช๐˜ค ๐˜ธ๐˜ช๐˜ต๐˜ฉ ๐˜ด๐˜ฉ๐˜ข๐˜ณ๐˜ช๐˜ฏ๐˜จ ๐˜ค๐˜ญ๐˜ข๐˜ด๐˜ด ๐˜™๐˜ฆ๐˜ค๐˜ช๐˜ฆ๐˜ท๐˜ฆ๐˜ด๐˜–๐˜ฃ๐˜ซ๐˜ฆ๐˜ค๐˜ต๐˜๐˜ณ๐˜ฐ๐˜ฎ๐˜“๐˜ž๐˜Š {
 @๐˜ˆ๐˜ถ๐˜ณ๐˜ข๐˜Œ๐˜ฏ๐˜ข๐˜ฃ๐˜ญ๐˜ฆ๐˜ฅ(๐˜ค๐˜ข๐˜ค๐˜ฉ๐˜ฆ๐˜ข๐˜ฃ๐˜ญ๐˜ฆ=๐˜ง๐˜ข๐˜ญ๐˜ด๐˜ฆ)
 ๐˜ฑ๐˜ถ๐˜ฃ๐˜ญ๐˜ช๐˜ค ๐˜ด๐˜ต๐˜ข๐˜ต๐˜ช๐˜ค ๐˜‰๐˜ฐ๐˜ฐ๐˜ญ๐˜ฆ๐˜ข๐˜ฏ ๐˜ถ๐˜ฑ๐˜ฅ๐˜ข๐˜ต๐˜ฆ๐˜ˆ๐˜ค๐˜ค๐˜ฐ๐˜ถ๐˜ฏ๐˜ต๐˜๐˜ช๐˜ฆ๐˜ญ๐˜ฅ๐˜ด(๐˜ˆ๐˜ค๐˜ค๐˜ฐ๐˜ถ๐˜ฏ๐˜ต ๐˜ข๐˜ค๐˜ค๐˜ฐ๐˜ถ๐˜ฏ๐˜ต๐˜›๐˜ฐ๐˜œ๐˜ฑ๐˜ฅ๐˜ข๐˜ต๐˜ฆ){
 ๐˜ˆ๐˜ค๐˜ค๐˜ฐ๐˜ถ๐˜ฏ๐˜ต ๐˜ข๐˜ค๐˜ค=๐˜ฏ๐˜ฆ๐˜ธ ๐˜ˆ๐˜ค๐˜ค๐˜ฐ๐˜ถ๐˜ฏ๐˜ต(๐˜๐˜ฅ=๐˜ข๐˜ค๐˜ค๐˜ฐ๐˜ถ๐˜ฏ๐˜ต๐˜›๐˜ฐ๐˜œ๐˜ฑ๐˜ฅ๐˜ข๐˜ต๐˜ฆ.๐˜๐˜ฅ);
 ๐˜ช๐˜ง(๐˜š๐˜ต๐˜ณ๐˜ช๐˜ฏ๐˜จ.๐˜๐˜ด๐˜•๐˜ฐ๐˜ต๐˜‰๐˜ญ๐˜ข๐˜ฏ๐˜ฌ(๐˜ข๐˜ค๐˜ค๐˜ฐ๐˜ถ๐˜ฏ๐˜ต๐˜›๐˜ฐ๐˜œ๐˜ฑ๐˜ฅ๐˜ข๐˜ต๐˜ฆ?.๐˜๐˜ช๐˜ฆ๐˜ญ๐˜ฅ๐˜›๐˜ฐ๐˜œ๐˜ฑ๐˜ฅ๐˜ข๐˜ต๐˜ฆ__๐˜ค))
 {
 ๐˜ข๐˜ค๐˜ค.๐˜๐˜ช๐˜ฆ๐˜ญ๐˜ฅ๐˜›๐˜ฐ๐˜œ๐˜ฑ๐˜ฅ๐˜ข๐˜ต๐˜ฆ__๐˜ค=๐˜ข๐˜ค๐˜ค๐˜ฐ๐˜ถ๐˜ฏ๐˜ต๐˜›๐˜ฐ๐˜œ๐˜ฑ๐˜ฅ๐˜ข๐˜ต๐˜ฆ.๐˜๐˜ช๐˜ฆ๐˜ญ๐˜ฅ๐˜›๐˜ฐ๐˜œ๐˜ฑ๐˜ฅ๐˜ข๐˜ต๐˜ฆ__๐˜ค;
 }
 ๐˜ถ๐˜ฑ๐˜ฅ๐˜ข๐˜ต๐˜ฆ ๐˜ข๐˜ค๐˜ค;
 ๐˜ณ๐˜ฆ๐˜ต๐˜ถ๐˜ณ๐˜ฏ ๐˜ต๐˜ณ๐˜ถ๐˜ฆ; 
 }
}

Note that i have created new Account variable acc, reassigned the field we are expecting to this new variable from accountToUpdate variable and updated acc instead of directly updating accountToUpdate๐Ÿšซ. This is because the object accountToUpdate is created from front end JS and can be manipulated to update unintended fields by adding extra fields from front end. So as a better security practice๐Ÿ‘ฎ, i believe its better to add this check from Apex side๐Ÿ•ต๏ธโ€โ™€๏ธ. You can pass a list of records in similar way!๐Ÿ”ฎ

You can find the code for LWC components used in my github repo

  • LWC Component Name : passSobjectToApex
  • Apex Class Name: RecievesObjectFromLWC.cls

This Content was originally posted in linkedin View Post