0% found this document useful (0 votes)
110 views7 pages

Apex Codes-Day 4

The document contains code examples demonstrating different data manipulation language (DML) operations in Apex including insert, update, upsert, delete, merge, and undelete. It also shows how to handle exceptions and define custom exceptions in Apex.

Uploaded by

Shubham Rajiwade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views7 pages

Apex Codes-Day 4

The document contains code examples demonstrating different data manipulation language (DML) operations in Apex including insert, update, upsert, delete, merge, and undelete. It also shows how to handle exceptions and define custom exceptions in Apex.

Uploaded by

Shubham Rajiwade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

public class DMLInsert

{
public static void SingleInsert()
{
Account ac=new Account();//created an empty account

ac.name='Ace Pvt Ltd';//setting the field values


ac.Type='Prospect';
ac.Industry='Technology';
ac.AnnualRevenue=7000000;
ac.Phone='9876543210';

insert ac;//save the record

Id acctId= ac.id;
system.debug(acctId);

Account ac1=new Account(name='ac2',type='Customer',Industry='Engineering');


insert ac1;
System.debug(ac1.id);

}
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 {

public static void SingleUpdate()


{
contact con= [select firstname,lastname,email,phone from contact where
firstname='Aashay' limit 1];//fetch the record

con.Phone='9898989898';//setting the field values


con.Email='[email protected]';
con.Birthdate=date.newInstance(1996, 09, 17);

update con;//update in the database


System.debug('Record Updated:' + con.id);
}

public static void BulkUpdate()


{
list<Account> accList= [select id from account where type='Prospect'];
for(account ac:accList)
{
ac.rating='Hot';

update accList;

system.debug('Total records updated:'+accList.size());

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;

Lead exLead=[select firstname,lastname from Lead where firstname='Ayesha' limit


1];
exLead.Phone='7878787878';
exLead.Email='[email protected]';

//update exLead;

List<Lead> leadList=new List<Lead>{nwLead,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);
}

public static void BulkDelete()


{
list<Account> accList= [select id from Account where Name IN('Ace Pvt
Ltd','Axis Pvt Ltd','Ac2')];
delete accList;
for(account ac:accList)
{
system.debug(ac.Id);
}
}
}
===================================================================================
====================
public class DMLMerge {

public static void MergeExample()


{
Account ac1=[select id from account where name='Axis Pvt Ltd' limit 1];
Account ac2=[select id from account where name='Ace Pvt Ltd' limit 1];

merge ac1 ac2;


}
}
===================================================================================
====================
public class DMLUndelete
{
public static void Del()
{
contact c= [select id from contact where lastname='Khanna' limit 1];//fetch the
record

delete c;//delete the record


}

public static void Undel()


{
contact c=[select id from contact where lastname='Khanna' limit 1 ALL
ROWS];//fetch the record from the recycle bin also
undelete c;// restore the record
}
}
===================================================================================
===================
public class RelatedDML
{
public static void RelInsert()
{
Account ac=new Account(name='Conga',type='Prospect', industry='Technology');
insert ac;

String acctId=ac.Id;

Contact con=new Contact();


con.FirstName='Niharika';
con.LastName='Goel';
con.Email='[email protected]';
con.AccountId=acctId;

insert con;
}
public static void BulkRelRecord()
{
Account ac=new
Account(name='Apttus',type='Customer',industry='Technology');
insert ac;
Id acId=ac.id;

List<Contact> conList=new List<Contact>


{
new Contact(FirstName='Vaibhav',lastname='Arora',AccountId=acId),
new Contact(FirstName='Vishal',lastname='Jain',AccountId=acId),
new Contact(FirstName='Deepak',lastname='Gupta',AccountId=acId)

};

insert conlist;

}
}
===================================================================================
====================
public class RelatedUpdate
{
public static void RelUpd()
{
Contact con=[select name,Account.name from contact where
firstname='Vaibhav'];//fetch the record

con.Birthdate=date.newInstance(1998, 08, 07);//setting the field values of


contact
con.Phone='9344567780';

con.Account.Phone='7897897890';//setting the field values of account


con.Account.rating='Hot';
con.account.annualrevenue=90000000;

update con;//update the contact


update con.Account;//update the account
}
}
===================================================================================
====================
public class RelatedDelete
{
public static void RelDel(string MenuName)
{
Menu__c me=[select id from menu__c where name=:MenuName];//fetch the record

MenuOrder__c [] orders= [select id from menuOrder__c where


menu__c=:me.Id];//fetch the related records

delete me;//delete the parent record


delete orders;//delete the child record
}
}
===================================================================================
=====================
public class DMLClass
{
public static void InsertRecord()
{
list<account> accs=new list<account>
{
new account(Name='acc1',type='Prospect',industry='Technology'),
new account(Name='acc2',type='Customer',industry='Engineering'),
new account(Name='acc3',type='Prosoect',industry='Apparel'),
new account(type='Customer',industry='Technology')

};

// 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')];

Database.DeleteResult [] res= Database.delete(accs,false);

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);

Lead ld=[select FirstName,lastname from Lead limit 1];


System.debug(ld.FirstName);
system.debug(ld.lastname);
system.debug(ld.Company);
}

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());
}
}
}
===================================================================================
====================

You might also like