Wednesday 19 August 2015

Salesforce Collections

HI Salesforce Developers ,


Developer Console

list<college> lststudent=new list<college>();
College shadan=new college();
shadan.strStudentName='susan';
Shadan.iStudentAge=20;
lststudent.add(shadan);

Retrieving values from list
for(college obj:lststudent)
{
system.debug(obj.strStudentName);
system.debug(obj.istudentage);
}

Sets
set<college> setCollege=new set<college>();
creating an object
college biet=new college();
Inserting elements into set
biet.strStudentName='Jasmine';
biet.strStudentName='Jasmine';
biet.istudentage=24;
adding object to the set instance
setcollege.add(biet);
Retrieving elements from set
for(college obj:setcollege)
{system.debug(obj.strStudentName);
system.debug(obj.istudentage);
}

Maps
map<string,string> mapname=new map<string,string>();
   mapname.put('anitha','Sfdc');
   mapname.get('anitha');



DML Operations
Inserting values into Accounts Object

list<account> a1=new list<account>();
a1.add(new account(name='Preethi'));
insert a1;

15/10/14

Lists

list<string> lststring=new list<string>();    // creating an empty list of string
lststring.add('philomenal');                        //adding element philomenal to the list
string str =lststring.get(0);                        //Retrieves the element at index 0
system.debug(lststring.get(0));                //printing the retrieved element


Inserting and retrieving multiple elements from List

list<integer> lstinteger=new list<integer>();
lstinteger.add(1000);
lstinteger.add(2000);

integer isalary =lstinteger.get(0);
isalary =lstinteger.get(1);
system.debug(lstinteger.get(0));
system.debug(lstinteger.get(1));

Sorting in Order

List<string> colors=new list<string>{'green','pink','red'};
   colors.sort();
system.assertEquals('green', colors.get(0));
system.assertequals('pink',colors.get(1));
system.debug(colors.get(0));
system.debug(colors.get(1));
add(integer,object) method

list<integer> lstinteger=new integer[3];
lstinteger.add(0,1000);
lstinteger.add(1,2000);
lstinteger.add(3,3000);
system.assertEquals(lstinteger.get(3), 3000);
system.debug(lstinteger.get(3));

Coping one List elements into another list using addall()

List<Account>lsta1= new list<Account>([select name,id from account where name='hitachi']);
List<Account>lsta2=new list<Account>([select name,id from account where billingcity='hyd']);
List<Account>lstMasterAcc= new List<Account>();
lstMasterAcc.addall(lsta1);
//System.debug(MasterAcc.get(lsta1));
lstMasterAcc.addall(lsta2);
//system.debug(MasterAcc.get(lsta2));
for(Account Accobj:lstMasterAcc)
{system.debug(lstMasterAcc.get(0));}

Coping  List elements into set using addall(set)

List<Account>lstacc=new list<Account>([select name,id from account where billingcity='hyd']);
set<account> setacc=new set<account>([select id,name from account where name='abcd']);
List<account> lstMasteracc=new List<account>();
lstmasteracc.addall(setacc);
lstmasteracc.addall(lstacc);
for(account objacc:lstmasteracc)
{
   system.debug(lstmasteracc.get(0));
}

Inserted account records into list elements
List<account> LstAcc=new list<account>([select name,id from account where billingcity='hyd']);
for(account obj:lstacc){
system.debug(lstacc.get(0));
}


Clearing the inserted elements from List
List<account> LstAcc=new list<account>([select name,id from account where billingcity='hyd']);
//for(account obj:lstacc){
lstacc.clear();
//}

Clone()
List<account> lstacc=new list<account>([select name,id from account where billingcity='hyd']);
List<Account> lstAccClone= lstacc.clone();
system.debug('cloned account is '+lstacc);

Equals(list)

List<account> lstacc=new list<account>([select name,id,createddate from account where billingcity='hyd']);
List<Account> lstAccClone= lstacc.clone();
system.debug('cloned account is '+lstacc);
lstacc.equals(lstAccClone);
system.debug('lstacc equals lstaccclone '+lstacc.equals(lstAccClone));

Debug:18:43:50:248 USER_DEBUG [6]|DEBUG|lstacc equals lstaccclone true

Equals(list) for deep clone
List<account> lstacc=new list<account>([select name,id,createddate from account where billingcity='hyd']);
List<Account> lstAccClone= lstacc.deepclone(true,false,true);
system.debug('cloned account is '+lstacc);
lstacc.equals(lstAccClone);
system.debug('lstacc equals lstaccclone '+lstacc.equals(lstAccClone));

Debug:18:42:52:074 USER_DEBUG [5]|DEBUG|lstacc equals lstaccclone false

Get(integer)
list<integer> lstinteger=new list<integer>();
lstinteger.add(1000);
lstinteger.add(2000);

integer isalary =lstinteger.get(0);
isalary =lstinteger.get(1);
system.debug(lstinteger.get(0));
system.debug(lstinteger.get(1));
HashCode()
list<integer> lstinteger=new integer[3];
lstinteger.add(0,1000);
lstinteger.add(1,2000);
lstinteger.add(3,3000);
//system.assertEquals(lstinteger.get(3), 3000);
//system.debug(lstinteger.get(3));
lstinteger.hashcode();
system.debug(lstinteger.hashcode());

Debug:   18:50:56:050 USER_DEBUG [8]|DEBUG|1301808609

IsEmpty()
list<integer> lstinteger=new integer[3];
lstinteger.add(0,1000);
lstinteger.add(1,2000);
lstinteger.add(3,3000);
system.debug(lstinteger.isempty());

Debug: 18:53:31:034 USER_DEBUG [5]|DEBUG|false

list<integer> lstinteger=new integer[3];
//lstinteger.add(0,1000);
//lstinteger.add(1,2000);
//lstinteger.add(3,3000);
lstinteger.clear();
system.debug(lstinteger.isempty());

Debug : 18:55:42:034 USER_DEBUG [7]|DEBUG|true

Remove(integer)
list<integer> lstinteger=new integer[5];
//lstinteger.clear();
lstinteger.add(0,1000);
lstinteger.add(1,2000);
lstinteger.add(2,3000);
integer iremoveinteger=lstinteger.remove(0);

  // for(integer iobj:lstinteger)
//{
   system.debug('Contents of the list'+lstinteger);

Debug: 19:07:15:047 USER_DEBUG [10]|DEBUG|Contents of the list(2000, 3000, null, null, null, null, null)

Updating account record (try catch exception)

list<account> a=new list<account>();
a=[select id,billingcity,accountnumber from account where id='00190000014Vftk'];
a.add(new account(billingcity='hyd'));
a.add(new account(accountnumber=12345));
try{
   update a;
}
catch(exception e)
{//system.debug(dml exception);
   }




Maps

map<string,string> m=new map<string,string>();
m.put('ani','sfdc');
m.put('sonu','.net');
m.put('manu','mobile');
//System.debug(m.get('sonu'));      //prints keys(sonu) corresponding value(.net)
set<string> s=m.keyset();               
System.debug(m.keyset());           //Prints all elements




Map<ID,Contact> mapAcc=new Map<id,contact>([select id,name from contact ]);
system.debug(mapacc.values()); // prints values of contact

Map<ID,Contact> mapAcc=new Map<id,contact>([select id,name from contact ]);
system.debug(mapacc.keyset()); //prints keys of contact

Map<ID,Contact> mapAcc=new Map<id,contact>([select id,name from contact ]);
system.debug(mapacc.size()); //gives the size of contacts


copy list elements into map
list<account> lstacc=new list<account>([select id,name from account where billingstate='ts']);
map<id,account> maplst=new map<id,account>(lstacc);
system.debug(lstacc);

ContainsKey()
Map<ID,account> mapAcc=new Map<id,account>([select id,name,billingcity from account ]);
system.debug(mapacc.containskey('00190000016L7jx'));




No comments:

Post a Comment