function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
manoj6492manoj6492 

Wrapper class for beginners

Hi,

       I am a trainee in sales force development ,When i was trying to find examples for wrapper class i couldnt find much most of them were based on  visualforce.I have posted a code for better understanding about wrapper class for the beginners.

@RestResource(urlMapping='/MyAccount/*')
global with sharing class wrapper
{
    @HttpGet
    global static  string MyAccount()
    {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response; 
        list<Account> acc = [Select name ,Id, (Select  name, Email from Contacts)From Account  where ActiveStatus__c=true];
        list<Accountw> aw = new list<Accountw>();
          
        for(Account ac :acc)
        {
             system.debug('ac1');
             list<ContactW> con= new list<ContactW> ();
             system.debug('newlis:'+con);
             for(contact c :ac.Contacts)
                {   
                    system.debug('contact>>'+c);
                    contactW c1 = new contactW(c.name,c.email);
                    con.add(c1);
      
                }
           
             Accountw a = new Accountw(ac.Id,ac.name,con);
             aw.add(a);           
            
        }system.debug('the list :'+Json.serialize(aw));
     return Json.serialize(aw);
    }
   global class Accountw
    { 
        string Ids{get;set;}
        string Name1{get;set;}
        list<contactW> con{get;set;}
        Accountw(String id,string name,list<contactW> con1)
        {
            Ids=id;
            Name1=name;
            con= con1;
        }
    }
    class contactW
    {
        string cname{get;set;}
        string Email1{get;set;}
         contactW(string name ,string  email)
        {
            cname = name;
            Email1 = email;
        }
    }
}

 I this code i have used REST ,JSON and Wrapper class