Apex For Loop అంటే ఏమిటి? List & SOQL For Loop | Salesforce Telugu

Salesforce Apex లో For Loop అంటే ఏమిటి? | Salesforce Apex in Telugu
📂 Salesforce Apex 🟢 Beginner 📘 Apex Daily

Salesforce Apex లో For Loop అంటే ఏమిటి?

Salesforce Apex in Telugu | Traditional For Loop, List For Loop, SOQL For Loop — Records తో పని చేయడానికి అత్యంత ముఖ్యమైన Concept

మన Salesforce Apex Daily Series లో గత Blog లో Switch Statement గురించి తెలుసుకున్నాం.

ఈ రోజు మనం Salesforce Apex లో చాలా ముఖ్యమైన మరియు చాలా ఎక్కువగా ఉపయోగించే Concept గురించి నేర్చుకుందాం. అదే... For Loop

మీరు Salesforce Developer అవ్వాలంటే For Loop తప్పకుండా అర్థం చేసుకోవాలి. ఎందుకంటే Salesforce లో మనం ఎక్కువగా Records, Lists, Collections మరియు SOQL Query Results తో పని చేస్తుంటాం.

అలాంటి సమయంలో ఒక Record తర్వాత మరో Record ని Process చేయడానికి For Loop చాలా ఉపయోగపడుతుంది.

ముందుగా ఒక చిన్న Example చూద్దాం

మీ దగ్గర 5 Students ఉన్నారని అనుకోండి.

  • Ravi
  • Suresh
  • Kiran
  • Ramesh
  • Anil

వీళ్లందరి పేర్లు ఒక్కొక్కటిగా Print చేయాలి. ప్రతి Student కోసం ఇలా Separate Code రాయడం అవసరం లేదు.

❌ ఇలా రాయడం మంచిది కాదు Ravi Print చేయి, Suresh Print చేయి, Kiran Print చేయి, Ramesh Print చేయి, Anil Print చేయి...

దానికి బదులుగా ఒక Loop ఉపయోగిస్తే సరిపోతుంది.

✅ Loop Idea "List లో ఉన్న ప్రతి Student ని ఒక్కొక్కటిగా తీసుకుని Print చేయి." ఇదే Loop యొక్క Main Purpose.

Salesforce లో For Loop ఎందుకు ముఖ్యమైనది?

Salesforce లో మనం సాధారణంగా ఇలా పని చేస్తుంటాం:

  • Accounts
  • Contacts
  • Opportunities
  • Leads
  • Cases
  • Custom Object Records
  • Lists
  • Sets
  • SOQL Query Results

ఉదాహరణకు Salesforce లో 100 Accounts ఉన్నాయని అనుకోండి. ప్రతి Account ని Process చేయాలి. అప్పుడు 100 Accounts కోసం 100 Statements రాయము.

ఒక For Loop రాస్తాం. అది ప్రతి Account ని ఒక్కొక్కటిగా Process చేస్తుంది.

Apex లో For Loop కి ముఖ్యమైన ఉపయోగాలు

Salesforce Apex లో For Loop ను ముఖ్యంగా:

  1. Numbers Repeat చేయడానికి
  2. List లో ఉన్న Records ని Process చేయడానికి
  3. Collections పై Iterate చేయడానికి
  4. SOQL Query Results ని Process చేయడానికి

ఉపయోగిస్తాం. ఇవన్నింటిలో Salesforce Development లో Records/Collections తో ఉపయోగించే For Loop చాలా Important.

Loop Type 1 Traditional For Loop

ముందుగా Basic For Loop అర్థం చేసుకుందాం.

Apex
for (Integer i = 1; i <= 5; i++) {
    System.debug(i);
}
Output
1
2
3
4
5

ఇక్కడ మనం 1 నుండి 5 వరకు Numbers Print చేశాం.

ఈ Code ఎలా పనిచేస్తుంది?

  • Integer i = 1; → Loop 1 నుండి Start అవుతుంది.
  • i <= 5 → Condition Check చేస్తుంది. True అయితే Code Execute అవుతుంది.
  • i++ → i Value 1 పెరుగుతుంది.

అంటే 1, 2, 3, 4, 5. Condition False అయినప్పుడు Loop Stop అవుతుంది.

కానీ Salesforce లో Real-Time గా ఇంకో For Loop చాలా ఎక్కువగా ఉపయోగిస్తాం

అదే... List For Loop

Salesforce లో మనం Lists తో చాలా ఎక్కువగా పని చేస్తాం. ఒక List లో ఉన్న ప్రతి Item ని ఒక్కొక్కటిగా తీసుకోవడానికి For Loop ఉపయోగించవచ్చు.

List అంటే ఏమిటి?

💡 Definition List అంటే చాలా Values ని ఒకే చోట Store చేసుకునే Collection.
Apex
List<String> names = new List<String>{
    'Ravi',
    'Suresh',
    'Kiran'
};

ఇక్కడ మన List లో 3 Names ఉన్నాయి. ఇప్పుడు ప్రతి Name ని Print చేయాలి.

Loop Type 2 List For Loop

Apex
List<String> names = new List<String>{
    'Ravi',
    'Suresh',
    'Kiran'
};

for (String name : names) {
    System.debug(name);
}
Output
Ravi
Suresh
Kiran

ఈ For Loop ని అర్థం చేసుకుందాం

ఇక్కడ for (String name : names) అంటే... names List లో ఉన్న ప్రతి String Value ని ఒక్కొక్కటిగా name Variable లోకి తీసుకుని Process చేయి.

మొదట name = Ravi, తర్వాత name = Suresh, తర్వాత name = Kiran. అందుకే మూడు Names Print అవుతాయి.

ఇది Salesforce లో ఎందుకు Important?

Salesforce లో మనం Lists లో Records Store చేస్తాం. ఉదాహరణకు...

Apex
List<Account> accounts;

ఇది Accounts యొక్క List. ఇప్పుడు ప్రతి Account ని Process చేయాలి. అప్పుడు...

Apex
for (Account acc : accounts) {
    System.debug(acc.Name);
}

ఇలా రాస్తాం. ఇది Salesforce Developer గా మీరు చాలా సార్లు చూస్తారు.

Loop Type 3 SOQL For Loop ⭐

Salesforce Development లో ఇది చాలా ముఖ్యమైనది. మనకు Salesforce లో ఉన్న Accounts అన్నీ తీసుకుని ఒక్కొక్క Account ని Process చేయాలి అనుకుందాం.

SOQL
SELECT Id, Name FROM Account

ఇది Accounts ను తీసుకువస్తుంది. ఆ Query Result ని For Loop లోనే Process చేయవచ్చు.

Apex
for (Account acc : [SELECT Id, Name FROM Account]) {
    System.debug(acc.Name);
}

ఇది SOQL For Loop.

SOQL For Loop అంటే ఏమిటి?

💡 Definition SOQL Query ద్వారా వచ్చిన Records ను ఒక్కొక్కటిగా Process చేయడానికి ఉపయోగించే For Loop ను SOQL For Loop అంటారు.

సింపుల్‌గా: Salesforce లో Records Query చేసి, వచ్చిన ప్రతి Record మీద పని చేయాలంటే SOQL For Loop చాలా ఉపయోగపడుతుంది.

Example

మన Salesforce Org లో 5 Accounts ఉన్నాయని అనుకోండి.

SOQL
SELECT Id, Name FROM Account

మనకు 5 Accounts వస్తాయి.

For Loop
for (Account acc : [SELECT Id, Name FROM Account]) {
    System.debug(acc.Name);
}

Loop ప్రతి Account ని ఒక్కొక్కటిగా తీసుకుంటుంది.

Output
Account 1
Account 2
Account 3
Account 4
Account 5

అన్నింటినీ Process చేస్తుంది.

Real-Time Example

ఒక Company లో ఉన్న అన్ని Accounts పేర్లు Debug Log లో చూడాలి.

Apex
for (Account acc : [SELECT Id, Name FROM Account]) {
    System.debug('Account Name: ' + acc.Name);
}
Output
Account Name: ABC Company
Account Name: XYZ Company
Account Name: SFDC Telugu
...

Accounts ఎన్ని ఉన్నా Loop ఒక్కొక్కటిగా Process చేస్తుంది.

ఇంకో Real-Time Example

ప్రతి Account యొక్క Industry కూడా చూడాలి.

Apex
for (Account acc : [SELECT Id, Name, Industry FROM Account]) {
    System.debug('Account: ' + acc.Name);
    System.debug('Industry: ' + acc.Industry);
}

ఇక్కడ ప్రతి Account కోసం Name మరియు Industry తీసుకుంటున్నాం.

For Loop + If Condition

ఇప్పుడు మనం ముందు నేర్చుకున్న If Statement ని For Loop తో కలపవచ్చు. ఉదాహరణకు Technology Industry ఉన్న Accounts మాత్రమే Print చేయాలి.

Apex
for (Account acc : [SELECT Id, Name, Industry FROM Account]) {
    if (acc.Industry == 'Technology') {
        System.debug(acc.Name);
    }
}

ఇక్కడ ఏమి జరుగుతోంది?

  1. ముందుగా Salesforce నుండి Accounts తీసుకుంటున్నాం.
  2. తర్వాత ప్రతి Account ని Loop చేస్తున్నాం.
  3. తర్వాత acc.Industry == 'Technology' అని Check చేస్తున్నాం.

Technology అయితే మాత్రమే Account Name Print అవుతుంది.

ఇది Real-Time Development లో చాలా Common Pattern

మనము ఇలా చాలా సందర్భాల్లో Code రాస్తాం.

SOQL For Loop If Condition Business Logic
Apex
for (Account acc : [SELECT Id, Name, Industry FROM Account]) {

    if (acc.Industry == 'Technology') {

        System.debug('Technology Account: ' + acc.Name);

    }
}

ఇది Salesforce Developer గా మీరు అర్థం చేసుకోవాల్సిన చాలా ముఖ్యమైన Pattern.

Traditional For Loop vs List For Loop vs SOQL For Loop

Type ఎప్పుడు ఉపయోగిస్తాం?
Traditional For LoopNumbers / Fixed Repetition
List For LoopList లేదా Collection లోని Items
SOQL For LoopSOQL ద్వారా వచ్చిన Salesforce Records

Salesforce లో ఏది ఎక్కువగా ఉపయోగిస్తారు?

ఇది చాలా ముఖ్యమైన Question. Salesforce Development లో Records మరియు Collections తో పని చేసేటప్పుడు List For Loop మరియు SOQL For Loop చాలా ఎక్కువగా ఉపయోగిస్తారు.

Traditional For Loop కూడా ఉపయోగిస్తాం. కానీ Salesforce Developer గా పనిచేసేటప్పుడు మీరు ఎక్కువగా ఎదుర్కొనేది...

Apex
for (Account acc : accounts)

లేదా

Apex
for (Account acc : [SELECT Id, Name FROM Account])

లాంటి Code. అందుకే వీటిని బాగా Practice చేయండి.

ఒక ముఖ్యమైన విషయం – Governor Limits

Salesforce లో Governor Limits ఉంటాయి. అందుకే Salesforce Development లో Records తో పని చేసేటప్పుడు సరైన Coding Pattern ఉపయోగించడం చాలా ముఖ్యం.

ఉదాహరణకు ఒక Loop లో ప్రతి Record కోసం Separate SOQL Query రాయడం మంచిది కాదు.

Wrong Approach
for (Account acc : accounts) {
    // ప్రతి Account కోసం Query చేయడం మంచిది కాదు
}
⚠️ గుర్తుంచుకోండి Loop లో అవసరం లేకుండా SOQL Queryలను మళ్లీ మళ్లీ Run చేయకూడదు.

Salesforce లో Bulkification అనే Concept చాలా ముఖ్యమైనది. దాన్ని మనం తర్వాతి Blogs లో చాలా సులభంగా నేర్చుకుందాం.

Practice Task

Practice 1 – Numbers

1 నుండి 10 వరకు Numbers Print చేయండి.

for (Integer i = 1; i <= 10; i++) {
    System.debug(i);
}

Practice 2 – List

ఈ List లో ఉన్న Names అన్నీ Print చేయండి.

List<String> names = new List<String>{
    'Ravi',
    'Suresh',
    'Kiran',
    'Ramesh'
};

for (String name : names) {
    System.debug(name);
}

Practice 3 – Accounts

మీ Salesforce Org లో Accounts ఉంటే Run చేసి Debug Log లో Account Names చూడండి.

for (Account acc : [SELECT Id, Name FROM Account]) {
    System.debug(acc.Name);
}

Practice 4 – Industry

Technology Industry ఉన్న Accounts మాత్రమే Print చేయడానికి స్వయంగా Code రాయండి.

💡 Hint acc.Industry == 'Technology' ని ఉపయోగించండి.

Interview Questions

For Loop అంటే ఏమిటి?

Answer

ఒక Code ను మళ్లీ మళ్లీ Execute చేయడానికి లేదా Collection లో ఉన్న Items/Records ను ఒక్కొక్కటిగా Process చేయడానికి For Loop ఉపయోగిస్తారు.

Salesforce Apex లో For Loop ఎక్కడ ఎక్కువగా ఉపయోగిస్తారు?

Answer

Lists, Collections మరియు SOQL Query ద్వారా వచ్చిన Salesforce Records ను Process చేయడానికి ఎక్కువగా ఉపయోగిస్తారు.

List For Loop అంటే ఏమిటి?

Answer

List లేదా Collection లో ఉన్న ప్రతి Item ని ఒక్కొక్కటిగా Process చేయడానికి ఉపయోగించే For Loop.

SOQL For Loop అంటే ఏమిటి?

Answer

SOQL Query ద్వారా వచ్చిన Records ను ఒక్కొక్కటిగా Process చేయడానికి ఉపయోగించే For Loop.

Traditional For Loop మరియు SOQL For Loop మధ్య Difference ఏమిటి?

Answer

Traditional For Loop సాధారణంగా Counter లేదా Fixed Repetition కోసం ఉపయోగిస్తారు. SOQL For Loop Salesforce Records ను SOQL ద్వారా Retrieve చేసి Process చేయడానికి ఉపయోగిస్తారు.

Conclusion

ఈ రోజు మనం కేవలం For Loop Syntax మాత్రమే కాకుండా, Salesforce Development లో For Loop ను Real-Time గా ఎలా ఉపయోగిస్తారో తెలుసుకున్నాం.

ముఖ్యంగా Traditional For Loop, List For Loop, SOQL For Loop ఈ మూడు Concepts ని అర్థం చేసుకున్నాం.

Salesforce Developer గా మీరు పనిచేసేటప్పుడు Lists మరియు Salesforce Records తో పని చేసే For Loops చాలా ఎక్కువగా కనిపిస్తాయి. అందుకే ఈ రోజు ఇచ్చిన Programs ను మీరు స్వయంగా Developer Console లో Type చేసి Practice చేయండి.

Code ని Copy చేసి Run చేయడం కంటే, Code ని చూసి మీరే Type చేయడం, మార్చడం, Output ని Observe చేయడం వల్ల Coding Logic చాలా బాగా Develop అవుతుంది.

🎯 ఇంకా మరింత లోతుగా Apex నేర్చుకోవాలనుకుంటున్నారా?

ఈ Blog ద్వారా మీరు Apex Concepts ను సులభంగా అర్థం చేసుకోవచ్చు. కానీ చదవడం మాత్రమే కాకుండా, Practice చేస్తూ నేర్చుకుంటేనే నిజమైన Coding Skills వస్తాయి.

అందుకే నేను SFDC Telugu లో పూర్తి Salesforce Apex Course (తెలుగులో) రూపొందించాను.

ఈ Course లో ప్రతి Concept ను Step by Step గా, Real Time Examples తో, Coding రాని వారికీ కూడా అర్థమయ్యే విధంగా వివరించాను.

ఈ Course ను Regular గా Follow చేస్తూ Practice చేస్తే, మీరు Apex Code ను చదివి అర్థం చేసుకోవడమే కాకుండా, స్వంతంగా Code రాయడం కూడా నేర్చుకుంటారు.

మీకు Coding అసలు రాకపోయినా భయపడాల్సిన అవసరం లేదు. Concepts ను సులభంగా అర్థం చేసుకుని, ప్రతి Program ను Practice చేస్తూ వెళ్తే Apex Code ను స్వంతంగా రాయడం, Code చూసి అర్థం చేసుకోవడం క్రమంగా మీకు వస్తుంది.

📹 Apex Course కి సంబంధించిన Video Link క్రింద ఇవ్వబడింది.

Course కి Instant Access వస్తుంది. వెంటనే Learning ప్రారంభించవచ్చు.

Regular గా Practice చేయండి. Coding రాని మీరు కూడా చాలా సులభంగా Apex నేర్చుకోగలరు. 🚀

Happy Learning & Happy Coding! ❤️

ఈ Topic మీకు Easy గా అర్థమైందా?

ఈ Blog ద్వారా విషయం మీకు సులభంగా అర్థమైతే, Comment చేసి మీ అభిప్రాయాన్ని తెలియజేయండి.

ఇలాంటి Salesforce Tutorials, Updates మరియు Useful Content కోసం SFDC Telugu Channel ని Subscribe చేయండి.

అలాగే కొత్త Blog Post మరియు Salesforce Updates కోసం WhatsApp Channel లో Join అవ్వండి.

📍 ఒక చిన్న Request:
Comment లో మీరు ఏ Place నుండి ఈ Blog చదువుతున్నారో చెప్పండి.

Thank You! ❤️

Happy Learning & Happy Coding! ❤️ — SFDC Telugu | Apex Daily Series

Post a Comment

0 Comments