Apex tutorial with examples in hindi

 Apex Tutorial with Examples in Hindi

Apex एक प्रोग्रामिंग लैंग्वेज है जिसे Salesforce पर एप्लिकेशन विकसित करने के लिए डिजाइन किया गया है। यह एक Java-like भाषा है जो Salesforce प्लेटफ़ॉर्म पर क्लाउड-आधारित एप्लिकेशन बनाने और कस्टम लॉजिक को कार्यान्वित करने के लिए उपयोग की जाती है। Apex का उपयोग Salesforce के क्लाउड प्लेटफ़ॉर्म पर सर्वर-साइड लॉजिक लिखने, ट्रिगर्स बनाने, और कस्टम कार्यक्षमता जोड़ने के लिए किया जाता है।

इस ट्यूटोरियल में हम Apex के कुछ बुनियादी सिद्धांतों और उदाहरणों के बारे में जानेंगे।


1. Apex क्या है?

Apex एक ओब्जेक्ट-ओरिएंटेड प्रोग्रामिंग लैंग्वेज है, जो विशेष रूप से Salesforce के लिए डिज़ाइन की गई है। यह Salesforce के क्लाउड प्लेटफ़ॉर्म पर डेटा प्रोसेसिंग और कस्टम बिजनेस लॉजिक को लागू करने के लिए उपयोगी है।

Apex का उपयोग निम्नलिखित कार्यों के लिए किया जाता है:

  • Triggers: डेटा इनपुट/अपडेट/डिलीट के दौरान विशेष कार्य करने के लिए।
  • Web Services: Salesforce में API के माध्यम से डेटा इंटरेक्शन।
  • Batch Jobs: बड़े पैमाने पर डेटा प्रोसेसिंग के लिए।
  • Custom Controllers: कस्टम लॉजिक लागू करने के लिए Visualforce पेजेज में।

2. Apex के लाभ

  1. Server-side Programming: Apex क्लाउड प्लेटफॉर्म पर सर्वर-साइड लॉजिक चलाने के लिए डिजाइन किया गया है, जिससे आपके एप्लिकेशन की गति और दक्षता बढ़ती है।
  2. Declarative & Programmatic: Apex को "Declarative" (जैसे कि Workflow, Process Builder) और "Programmatic" (जैसे कि Triggers और Classes) दोनों तरीकों से उपयोग किया जा सकता है।
  3. Comprehensive Testing: Apex के साथ आप अपने कोड के लिए Unit Tests भी लिख सकते हैं, जिससे आपके एप्लिकेशन की गुणवत्ता और स्थिरता सुनिश्चित होती है।
  4. Tight Integration with Salesforce: Apex Salesforce के डेटा मॉडल, सुरक्षा सेटिंग्स और यूजर इंटरफेस से सीधे एकीकृत होता है।

3. Apex का सिंटैक्स (Syntax)

Apex का सिंटैक्स Java जैसा है, लेकिन Salesforce के प्लेटफॉर्म के लिए अनुकूलित है। Apex को Classes और Triggers के रूप में लिखा जा सकता है।

(i) Class Example (Apex Class)

Apex में हम class का उपयोग करके कस्टम लॉजिक लिखते हैं। एक Apex class डेटा प्रोसेसिंग और कस्टम बिजनेस लॉजिक को लागू करने के लिए होता है।

Example 1: Apex Class for Adding Two Numbers

apex
public class MathOperations { // Method to add two numbers public static Integer addNumbers(Integer num1, Integer num2) { return num1 + num2; } }

यह एक बहुत ही सरल उदाहरण है जिसमें एक MathOperations नाम की class बनाई गई है, जिसमें दो संख्याओं को जोड़ने के लिए एक static method addNumbers है। आप इसे Salesforce के UI से या किसी अन्य Apex कोड से कॉल कर सकते हैं।

Class को कॉल करना (Calling Class Method)

apex
// Calling the addNumbers method from the MathOperations class Integer result = MathOperations.addNumbers(10, 20); System.debug('Result: ' + result);

(ii) Trigger Example (Apex Trigger)

Apex में हम Triggers का उपयोग करके डेटा ऑपरेशन्स (Insert, Update, Delete) के दौरान कस्टम लॉजिक चला सकते हैं। Trigger विशेष रूप से तब काम आते हैं जब हम Salesforce डेटा में बदलाव होने पर कस्टम बिजनेस लॉजिक लागू करना चाहते हैं।

Example 2: Apex Trigger to Update Contact's Status on Account Update

apex
trigger UpdateContactStatus on Account (after update) { // Loop through the updated accounts for (Account acc : Trigger.new) { // Update the related contacts' status for (Contact con : [SELECT Id, Status FROM Contact WHERE AccountId = :acc.Id]) { con.Status = 'Updated'; } } }

इस उदाहरण में, जब भी Account को अपडेट किया जाता है, यह Trigger सभी संबंधित Contact रिकॉर्ड्स के Status को "Updated" में बदल देता है।

(iii) Handling DML Operations (Data Manipulation Language)

Salesforce में, डेटा को जोड़ने, अपडेट करने, या हटाने के लिए DML ऑपरेशन्स का उपयोग किया जाता है। Apex में insert, update, delete, और upsert ऑपरेशन्स होते हैं।

Example 3: Insert Operation

apex
// Create a new Contact Contact con = new Contact(FirstName='John', LastName='Doe', Email='john.doe@example.com'); // Insert the new Contact into Salesforce insert con;

(iv) Exception Handling in Apex

Apex में Exception Handling के लिए try-catch-finally ब्लॉक का उपयोग किया जाता है।

Example 4: Handling Exceptions in Apex

apex
try { // Try block to execute code that might cause an error Integer result = 10 / 0; } catch (DivideByZeroException e) { // Catch block to handle the error System.debug('Error: ' + e.getMessage()); } finally { // Finally block, code that should always execute System.debug('This will always run.'); }

4. Apex Testing

Apex में Unit Testing महत्वपूर्ण होता है। Salesforce प्लेटफॉर्म पर डेवलपमेंट के दौरान, आपके द्वारा लिखे गए Apex कोड को टेस्ट करना जरूरी होता है ताकि यह सुनिश्चित किया जा सके कि कोड सही तरह से काम कर रहा है।

Example 5: Apex Unit Test

apex
@isTest public class MathOperationsTest { @isTest static void testAddNumbers() { Integer result = MathOperations.addNumbers(10, 20); // Assert to verify the result System.assertEquals(30, result, 'The sum should be 30'); } }

इस उदाहरण में, हमने MathOperations class के addNumbers method के लिए एक test method testAddNumbers लिखा है। System.assertEquals का उपयोग किया गया है यह सुनिश्चित करने के लिए कि जो परिणाम वापस आ रहा है, वह अपेक्षित है।


5. Apex Best Practices

  • Bulkify your code: हमेशा अपना कोड इस तरह से लिखें कि वह बड़े पैमाने पर डेटा प्रोसेस कर सके। उदाहरण के लिए, Triggers को हमेशा bulk processing के लिए तैयार करें, ताकि Salesforce द्वारा दिए गए governor limits का उल्लंघन न हो।

  • Avoid Hardcoding: हार्डकोड वैल्यूज़ से बचें और हमेशा Custom Settings, Custom Metadata Types, या Custom Labels का उपयोग करें।

  • Use SOQL and DML Efficiently: Salesforce में SOQL (Salesforce Object Query Language) और DML (Data Manipulation Language) ऑपरेशन्स के लिए governor limits होते हैं। इनका सही उपयोग करें ताकि आपके कोड में performance issues न आएं।

  • Test Coverage: सुनिश्चित करें कि आपके द्वारा लिखे गए Apex कोड का उचित परीक्षण किया गया हो, और कम से कम 75% टेस्ट कवरेज प्राप्त करें। यह आवश्यक है ताकि आप अपने कोड को Salesforce में डेप्लॉय कर सकें।


6. Conclusion

Apex Salesforce के प्लेटफॉर्म पर कस्टम बिजनेस लॉजिक और डेटा प्रोसेसिंग के लिए एक शक्तिशाली प्रोग्रामिंग लैंग्वेज है। इस ट्यूटोरियल में हमने Apex के बुनियादी सिद्धांतों को समझा, जैसे कि Apex Classes, Triggers, DML Operations, Exception Handling, और Testing। Apex का उपयोग करने से Salesforce एप्लिकेशन में जटिल लॉजिक लागू किया जा सकता है, जिससे आपकी एप्लिकेशन की कार्यक्षमता बढ़ती है।

यदि आप Salesforce डेवलपर बनना चाहते हैं, तो Apex में कुशलता हासिल करना एक महत्वपूर्ण कदम है।

Comments

Popular posts from this blog

PrimeNG tutorial with examples using frequently used classes

Docker and Kubernetes Tutorials and QnA

oAuth in angular