Thursday, 18 May 2023

Making a Request via LWC Component to Apex Method in Salesforce

Introduction: In Salesforce development, Lightning Web Components (LWC) provide a modern and efficient way to build user interfaces. One common scenario is making server-side requests to perform operations or retrieve data. In this article, we will explore how to make a request from an LWC component to an Apex method using the @wire decorator and demonstrate it with a code example.

Step 1: Create the Apex Method To begin, let's create an Apex method that will be called from the LWC component. This method can perform any server-side logic, such as retrieving data from the database or updating records. Here's an example of an Apex method called getDataFromServer:

public with sharing class MyController
@AuraEnabled public static String getDataFromServer(String input)
       // Perform server-side logic here
       return 'Response from server for input: ' + input;
     } 
}

Step 2: Create the LWC Component Next, let's create the LWC component that will make the server-side request to the Apex method. Here's an example of an LWC component called myComponent:

html
<template> <lightning-card title="Make Server Request"> <div class="slds-m-around_medium"> <lightning-input label="Input" value={inputValue} onchange={handleInputChange}></lightning-input> <lightning-button label="Submit" onclick={handleSubmit}></lightning-button> <div>{response}</div> </div> </lightning-card> </template>
javascript
import { LightningElement, wire } from 'lwc'; import getDataFromServer from '@salesforce/apex/MyController.getDataFromServer'; export default class myComponent extends LightningElement { inputValue = ''; response = ''; handleInputChange(event) { this.inputValue = event.target.value; } handleSubmit() { getDataFromServer({ input: this.inputValue }) .then(result => { this.response = result; }) .catch(error => { // Handle error console.error(error); }); } }

In the above code snippet, we import the getDataFromServer method using the @salesforce/apex module, which allows us to call Apex methods from within our LWC component. We define an inputValue property to hold the user input and a response property to display the server's response. The handleInputChange method updates the inputValue property whenever the input field changes. The handleSubmit method calls the Apex method getDataFromServer with the input value and updates the response property with the server's response.

Step 3: Wire the Apex Method to the LWC Component To wire the Apex method to the LWC component, we use the @wire decorator. In our case, we don't need to retrieve data on component initialization, so we omit the wire decorator from the component class. However, if you want to fetch data on component initialization, you can add the following code snippet to the component class:

javascript
@wire(getDataFromServer, { input: '$inputValue' }) wiredMethod({ error, data }) { if (data) { // Handle successful response this.response = data; } else if (error) { // Handle error console.error(error); } }

Conclusion: By utilizing the @wire decorator and importing the Apex method from the @salesforce/apex module, we can seamlessly make server-side requests from our LWC component to

LWC component and an equivalent Aura component in Salesforce



Salesforce, a leading customer relationship management platform, has been continuously evolving to provide developers with powerful tools and frameworks. In the world of Salesforce development, two major component frameworks have made a significant impact: Aura and Lightning Web Components (LWC). While both frameworks serve a similar purpose, there are notable differences between them. Let's dive into the key distinctions and advantages of LWC over Aura.



1.Performance and Lightweight Architecture: LWC was introduced by Salesforce in 2019 with a strong emphasis on performance. It follows modern web standards and utilizes a lightweight architecture that enables faster rendering and improved overall performance. LWC leverages browser-native capabilities, reducing the reliance on JavaScript frameworks and libraries. On the other hand, Aura, introduced earlier, relies on a custom framework and can be comparatively slower.


2.Enhanced Developer Experience: LWC offers an enhanced developer experience by leveraging the latest web development concepts. It supports a component-based architecture that encourages reusability and modularity. LWC components are built using standard web technologies such as HTML, CSS, and JavaScript, which makes it easier for developers to learn and work with. Additionally, LWC benefits from Salesforce's investment in open-source technologies like Web Components, making it compatible with other frameworks and libraries.


3.Improved Performance Debugging: Debugging is an essential part of the development process. LWC provides a more streamlined and efficient debugging experience compared to Aura. LWC's lightweight architecture allows for faster component initialization, rendering, and data binding, which simplifies the identification and resolution of performance bottlenecks. Salesforce's Lightning Web Components Developer Tools, including the Lightning Web Components Inspector, provide comprehensive debugging capabilities for LWC components.


4.Better Security and Locker Service: Security is a critical aspect of any application, and Salesforce has introduced Locker Service to enhance security within Lightning Components. LWC inherits the security features of Locker Service, which ensures better isolation between components and prevents unauthorized access to critical data. This feature was not available in Aura by default and required additional configuration and customization.


5.Improved Testing Capabilities: LWC promotes testability by allowing developers to write unit tests using Jest, a popular JavaScript testing framework. LWC's testing framework provides robust capabilities to write, execute, and debug tests, enabling developers to ensure the quality and reliability of their components. In contrast, Aura's testing framework had certain limitations and required a separate testing framework like Jasmine.


6.Forward Compatibility and Industry Standardization: Salesforce has positioned LWC as the future of Lightning Components, emphasizing its forward compatibility. While Aura components will continue to be supported, Salesforce encourages developers to adopt LWC for new development. LWC follows the Web Components standard, which is increasingly gaining industry-wide acceptance. This compatibility ensures that LWC components can be used outside the Salesforce platform, opening up possibilities for broader integration and reuse.


In conclusion, LWC represents a significant step forward in Salesforce component development. Its lightweight architecture, improved performance, enhanced developer experience, and compatibility with industry standards position it as the preferred choice for building robust and efficient components. While Aura continues to be supported, embracing LWC empowers developers to leverage modern web development practices and harness the full potential of the Salesforce platform.

Monday, 9 December 2019

Watchman error with Expo for running the React-Native app


Hey all after a very long time I am writing aboMyntra linkut some issues these days mostly in my weekend I have started a react-native course. So during the setup, I found some issues with Expo those who don't know about Expo. it is more like an emulator you can say hybrid emulator in a way because it will work for android and Ios with the same kind of setup. so writing this blog is about to mention one issue which I spend a day to fix then I realize to write it so it might help someone.

After running the expo via `sudo npm start` I got below error message which related to Watchman.




watchman --no-pretty get-sockname returned with exit code=1, signal=null, stderr=


And don't believe after spending a day I found one line code which works and fix the issue so fucking crazy .. :)

rm -rf /usr/local/var/run/watchman/root-state

You're running watchman as root but the state dir, which may contain trigger definitions and thus allow spawning arbitrary commands, is not owned by root. This is a security issue and thus watchman is refusing to start.

I'd recommend that you avoid running tools that wish to use watchman using sudo to avoid this happening again.
Thanks, that's all for today 
Happy coding guys.

Saturday, 2 March 2019

Soltuion of : Error: `gyp` failed with exit code: 1

It's been a long time to write a blog on some tech issue. This is related to setup a NodeJS project I almost spend 2-3 hours to fix some f**king issue with `gyp`  while ruining npm install.


Error:

gyp: binding.gyp not found (cwd: /Users/anilyadav/Documents/ahok/base12) while trying to load binding.gyp
gyp ERR! configure error 
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/Users/anilyadav/.nvm/versions/node/v6.9.2/lib/node_modules/node-gyp/lib/configure.js:345:16)
gyp ERR! stack     at emitTwo (events.js:106:13)
gyp ERR! stack     at ChildProcess.emit (events.js:191:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
gyp ERR! System Darwin 18.2.0
gyp ERR! command "/Users/anilyadav/.nvm/versions/node/v6.9.2/bin/node" "/Users/anilyadav/.nvm/versions/node/v6.9.2/bin/node-gyp" "rebuild"
gyp ERR! cwd /Users/anilyadav/Documents/ahok/base12
gyp ERR! node -v v6.9.2
gyp ERR! node-gyp -v v3.8.0

gyp ERR! not ok 




gyp: No Xcode or CLT version detected!
gyp ERR! configure error 
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/Users/anilyadav/.nvm/versions/node/v6.9.2/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:308:16)
gyp ERR! stack     at emitTwo (events.js:106:13)
gyp ERR! stack     at ChildProcess.emit (events.js:191:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
gyp ERR! System Darwin 18.2.0
gyp ERR! command "/Users/anilyadav/.nvm/versions/node/v6.9.2/bin/node" "/Users/anilyadav/.nvm/versions/node/v6.9.2/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/anilyadav/Documents/ahok/base12/node_modules/bcrypt
gyp ERR! node -v v6.9.2
gyp ERR! node-gyp -v v3.5.0
gyp ERR! not ok 
npm WARN b12prj@0.0.3 No repository field.
npm WARN b12prj@0.0.3 No license field.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! bcrypt@0.8.5 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the bcrypt@0.8.5 install script 'node-gyp rebuild'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the bcrypt package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs bcrypt
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls bcrypt
npm ERR! There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/anilyadav/.npm/_logs/2019-03-03T06_59_17_870Z-debug.log

Anils-MacBook-Pro:base12 anilyadav$ node-gyp rebuild



So finally found that this is something related to bcrypt version so after runing the below command it's working fine.


Solution:

$ sudo npm install --save bcrypt-nodejs && sudo  npm uninstall --save bcrypt

Hope It will help to save your time 

Sorry for my bad english. ;)



Tuesday, 22 August 2017

Expire collection data on specified number of seconds using MongoDB - TTL (Time to live)


I was working on some interesting feature of MongoDB know as TTL (Time to live) after understanding and google about the feature. I found that it's a very easy and use full feature of MongoDB for those who don't want to keep the unnecessary data/collection/documents in there MongoDB collection/table, so more like TTL is act like a background Job which executed on specific number of second. It's working based on index's in MongoDB for deleting the record/collection.

For detail understanding please visit: official documentation.

In my case : I need this to be work after every month for every single record so I added date but it's not working and because once on any collection you create a TTL index then I will not update the TTL expiration date value. So it's a bad way to do this also I notice that the TTL field are not Date type so if you are using : expireAfterSeconds   then please try to use number which is Seconds.

db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )
Javascript best book ever click :)

Thursday, 10 August 2017

Saturday, 19 November 2016

Nginx SSL setup for multiple ports

This blog is related to setup SSL cert over Nginx server if you have .cert & .key file then you are able to setup this SSL If you don't have any please create locally or purchase from SSL provides.

SSL give use a secure socket protection which help to keep our domain / website heathy.

Below is the simple steps .

1) Upload your .cert & .key file on your server like Amazon Ec2 / Digital Ocean using any tool or "scp" ubuntu command.

2) Install Nginx on your server.
 
  • sudo apt-get update
  • sudo apt-get install nginx

3) After successfully installation you need to create a ssl_certs Folder in /etc/nginx/
mkdir /etc/nginx/ssl_certs
please check once if the folder is already exist then don't need to create the folder again .

4) Put your .cert & .key file in /etc/nginx/ssl_certs folder

5) Edit: /etc/nginx/sites-available/default and add your domain as per my given configuration.

6) Restart you nginx server
  • sudo systemctl restart nginx # New version of Ubuntu 16.04
  • sudo service nginx restart # older version of Ubuntu 14.04




Salesforce CRM vs. Zoho: A Comparative Analysis

Introduction: Selecting the right customer relationship management (CRM) software is crucial for businesses seeking to streamline their sal...