0% found this document useful (0 votes)
34 views

#Inserting New Contact From Developer Console

The document contains code examples demonstrating how to work with records in Salesforce from the developer console, including inserting a new contact, adding records to lists and sets, retrieving contact records, creating triggers to validate fields and create new records, and using try/catch blocks to handle exceptions.

Uploaded by

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

#Inserting New Contact From Developer Console

The document contains code examples demonstrating how to work with records in Salesforce from the developer console, including inserting a new contact, adding records to lists and sets, retrieving contact records, creating triggers to validate fields and create new records, and using try/catch blocks to handle exceptions.

Uploaded by

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

#inserting new contact from developer console

contact c = new contact();

c.firstname = 'Michael';

c.lastname = 'Oliver';

c.email = '[email protected]';

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

You might also like