Apex Codes-Day 4
Apex Codes-Day 4
{
public static void SingleInsert()
{
Account ac=new Account();//created an empty account
Id acctId= ac.id;
system.debug(acctId);
}
public static void BulkInsert()
{
List<Lead> leads=new List<Lead>
{
new Lead(FirstName='Kanav',lastname='Sharma',company='ABC'),
new Lead(FirstName='Ayesha',lastname='Gupta',company='XYZ'),
new Lead(FirstName='Rajiv',lastname='Raj',company='ABC')
};
System.debug(limits.getLimitDmlStatements());//150
insert leads;
System.debug(limits.getDmlStatements());//1
}
}
===================================================================================
====================
public class DMLUpdate {
update accList;
for(account ac:accList)
{
system.debug(ac.id);
}
}
}
===================================================================================
====================
public class DMLUpsert
{
public static void Ups()
{
Lead nwLead=new
Lead(firstname='Paras',lastname='Gupta',email='[email protected]',company='Deloitte')
;
//insert nwLead;
//update exLead;
upsert leadList;
}
}
===================================================================================
====================
public class DMLDelete
{
public static void SingleDelete()
{
Lead ld= [select id from lead where firstname='Ayesha' limit 1];//fetch the
record
delete ld;//delete the record
system.debug('Record Deleted:'+ ld.id);
}
String acctId=ac.Id;
insert con;
}
public static void BulkRelRecord()
{
Account ac=new
Account(name='Apttus',type='Customer',industry='Technology');
insert ac;
Id acId=ac.id;
};
insert conlist;
}
}
===================================================================================
====================
public class RelatedUpdate
{
public static void RelUpd()
{
Contact con=[select name,Account.name from contact where
firstname='Vaibhav'];//fetch the record
};
// insert accs;
Database.SaveResult [] result= database.insert(accs,false);//will process
the records partially
for(Database.SaveResult sr:result)
{
if(sr.isSuccess())
{
System.debug('Record created with id:'+ sr.getId());
}
else
{
Database.Error [] errs=sr.getErrors();
for(Database.Error er:errs)
{
System.debug(er.getFields());
system.debug(er.getMessage());
system.debug(er.getStatusCode());
}
}
}
}
}
===================================================================================
====================
public class DatabaseClass
{
public static void DeleteRecords()
{
Account [] accs=[select id from account where Name IN('Conga','Cognizant','Edge
Communications','Genepoint')];
for(Database.DeleteResult dr:res)
{
if(dr.isSuccess())
{
system.debug('Record deleted with the Id:'+ dr.getId());
}
else
{
database.Error [] errs= dr.getErrors();
for(database.error er:errs)
{
system.debug(er.getFields());
system.debug(er.getMessage());
system.debug(er.getStatusCode());
}
}
}
}
}
===================================================================================
=====================
public class ExceptionClass
{
public static void ExceptionMethod()
{
List<account> listAcc=new list<account>
{
new Account(name='a1',type='Prospect'),
new Account(name='a2',type='Customer'),
new Account()
};
try
{
// insert listAcc;
// Account ac=[select name from account];
//system.debug(ac);
//Account listAc=[select name from account where name='A'];
// System.debug(listAc);
catch(DMLException ex)
{
System.debug(ex.getLineNumber());
system.debug(ex.getMessage());
}
catch(QueryException ex)
{
System.debug('Some Error Occured');
system.debug(ex.getMessage());
}
catch(sObjectException ex)
{
System.debug('Some error occured');
}
catch(Exception e)
{
System.debug('');
}
finally
{
system.debug('This will be executed always');
}
}
}
===================================================================================
=====================
public class CustomExceptionClass
{
public class OtherException extends Exception//defining custom exception
{
}
public static void testException(integer i)
{
try
{
if(i<0)
{
throw new OtherException('Value cannot be Negative');//raise the
exception
}
}
catch(OtherException ex)//handling the custom exception
{
system.debug('Some error:' + ex.getMessage());
}
}
}
===================================================================================
====================