Você está na página 1de 4

#inserting new contact from developer console

contact c = new contact();

c.firstname = 'Michael';

c.lastname = 'Oliver';

c.email = 'mo@gmail.com';

insert c;

system.debug('ID is generated as:'+ c.id);

# Adding records to list from developer console

list<string> colors = new list<string>();

colors.add('blue');

colors.add('red');

system.debug('The list size is :' + colors.size());

system.debug('The list contents are :' + colors);

#Adding records to sets from developer console

set<string> colors = new set<string>();

colors.add('blue');

colors.add('red');

system.debug('The list size is :' + colors.size());

system.debug('The list contents are :' + colors);


#Retrieve records from contacts from developer console

List<contact> con = [select id,Email, Firstname, LastName, Phone from contact];

system.debug('size is:' +con.size());

for (contact f:con)

system.debug('The details of contacts are:' + f.Firstname + '' + f.Lastname + 'Phone No:' + f.phone);

Trigger to validate the Amount field on opportunity object before inserting or updating a record

1trigger trigger_basic on Opportunity (Before Insert, Before Update) 
2{
3for (Opportunity a : Trigger.new)
4{
5if (a.Amount < 501)
6{
7a.adderror('Amount value should be greater than 500');
8}
9}
1
0
1}
1

Trigger such that when a record is added in opportunities a new trigger is invoked such that a new
contact is also created

trigger trigger_advanced on Opportunity (After insert) {

contact c= new contact();

for(opportunity f : Trigger.new)

c.AccountID = f.AccountID;

c.Lastname = 'Jobs';

c.Firstname = 'Steve';

insert c;

}
}

Try and catch to capture errors

try

contact c= new contact();

c.firstname = 'xyz';

insert c;

catch(Exception e)

system.debug('Missing Required field: Lastname');

Try and catch to capture errors (DML exception)

try

contact c= new contact();

c.firstname = 'xyz';

insert c;

catch(DMLException e)

system.debug('Error:' + e);
}

Você também pode gostar