319-23 Sobjects Sobject:: Example Name Rating Ownership Type Active - C

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

319-23 Sobjects

===========================================
Sobject:
===========================================

​ ​ 1​. Every table created in the salesforce database is called sobject in Apex programming
​2​. Every sobject has equilent apex class
​3​. We will reffer to table/sobject using the apex class
Example :
Sobject Name Apex class
-------------------------------------------------------------
Account Account
Contact Contact
Loan__c Loan__c
Customer__c Customer__c

​ Example​ : Create a instance of Account


Account acc =new Account();
acc.Name='Wipro';
acc.Phone='Industry';
acc.Rating='Hot';

​ Example​ ​:​create the sobjectAccount with following data.

Name Rating Ownership Type Active__c


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TCS Hot Public New Customer Yes

Dell Warm Private Existing Yes

ReddyLab Cold Public New Customer No

Account acc =new Account();


acc.Name='TCS';
acc.Rating='Hot';
acc.Ownership='Public';
acc.Type='New Customer';
acc.Active__C='Yes';

Second format
Account acc =new Account(Name='Dell',Rating='Warm',Type='Existing',Active__c='Yes');
​Third Format :
Account acc =new Account();
acc.put('Name','Wipro');
acc.put('Rating','Hot');
acc.Put('ownership','Public');
acc.put('Type','New Customer');
​ ​Example​ : ​Create a sobject Opportunity with following data

Name StageName CloseDate Amount Type


--------------------------------------------------------------------------------------------------------------------------------------------
Admin Prospecting 10-2-2019 10000 New Customer

Wipro Quotation 12-5-2019 23000 Existing

Opportunity op =new Opportunity();


op.Name='Admin';
op.StageName='Prospecting';
op.closeDate=Date.newInstance(2019,2,10);
op.Amount=10000;
op.Type='New Customer';

Opportunity op =new Opportunity(Name='Admin',StageName='Prospecting', closeDate=Date.newInstance(2019,2,10),


Amount=10000,type='New Customer');

Opportunity op =new Opportunity();


op.put('Name','Admin');
op.put('StageName','Prospecting');
op.put('CloseDate',Date.newInstance(2019,2,10);
op.put('Amount',10000);
op.put('Type','New Customer');

​ Example​ : Create sobject Customer__c with following data

​ ​ ​ LastName__c FirstName__c Company__c Phone__c City__c Age__c


----------------------------------------------------------------------------------------------------------------------------------------------------------
M Satish Wipro 020-123 Hyd 30
K Prasad TCS 030-1234 Ban 45

Customer__c cust =new Customer__c();


cust.LastName__c='M';
cust.FirstName__c='Satish';
cust.Company__c='Wipro';
cust.Phone__c='020-123';
cust.City__c='Hyd';
cust.Age__c=30;

Customer__c cust =new Customer__c(LastName__c='M',FirstName__c='Satish',Company__c='Wipro',Phone__c='020-123',

city__c='Hyd',Age__c=30);

--------------------------------------------------------------------------------------------------------------------------------------------------
============================================================

TYPE CASTING
:============================================================

10, Satish 10-12-1999 ,true

Object a =10;
object b='Satish';
Object c =Date.newInstance(2019,2,23);

Integer age =(Integer)a;


String name=(String)b;
Date d=(Date)c;

Sobject a =new Account();


sobject b =new Contact();
Sobject c =new Opportunity();

Account acc =(Account)a;


Contact con=(Contact)b;
Opportunity op=(opportunity)c;
---------------------------------------------------------------------------------------------------------------

============================================================
Wrapper Class:
============================================================

​ ​ 1. it is a apex class .
2. it contains only datamembers
3. It is used to create user defined datatypes.
​Example ​:
​global/public class ClassName{
datamembers
}

​1. Create a wrapper class Student with name ,age ,dob


public class Student{
public String name;
public Integer age;
public Date dob;
}

2. Create a wrapper class ProductInfo with name,quantity,price


public class ProductInfo{
public String name;
public Integer quantity;
public Decimal price;
}
3. Create a wrapper class QuoteDetails with instance ofAccount ,contact
public class QuoteDetails{
public Account acc;
public Contact con;
}
4. Create a wrapper class College with name,code, fee, no_of_students
public class College{
public String name;
public String code;
public Decimal fee;
public Integer no_of_Students;
}
5. Create a wrapper class AccountData with name,industry,rating,
public class AccountData {

6. Create a wrapper class which will take account ,contact and opportunity
public class Example{
public Account acc;
public Contact con;
public Opportunity opt;
}

7. Create a wrapper class which will take Customer__c ,loan__c ,payment__c


public class Example{
public Customer__c cust;
public Loan__c loan;
public Payment__c pay;

8. Create a wrapper class which will take Account and boolean value,

public class Example{


public Sobject acc ;
public Boolean flag;
}

You might also like