You need to sign in to do that
Don't have an account?
Anne D Pyle
Syntax for Upsert and System.AssertEquals
I am working on the Developer Beginner trailhead. One of the exercises is below:
-----------
Contact jane = new Contact(FirstName='Jane',
LastName='Smith',
Email='jane.smith@example.com',
Description='Contact of the day');
insert jane;
// 1. Upsert using an idLookup field
// Create a second sObject variable.
// This variable doesn’t have any ID set.
Contact jane2 = new Contact(FirstName='Jane',
LastName='Smith',
Email='jane.smith@example.com',
Description='Prefers to be contacted by email.');
// Upsert the contact by using the idLookup field for matching.
upsert jane2 Contact.fields.Email;
// Verify that the contact has been updated
System.assertEquals('Prefers to be contacted by email.',
[SELECT Description FROM Contact WHERE Id=:jane.Id].Description);
-------------
I understand the gist of the code, but cannot find a reference in the Apex documentation about the syntax within the bolded lines. Would someone break down each piece of this code for me? In particular why does the [Select...].Description phrase require the .Description?
My dilemma has been that I have a great deal of experience with programming, but not with Java. So much of the material I'm finding assumes knowledge of Jave syntax.
-----------
Contact jane = new Contact(FirstName='Jane',
LastName='Smith',
Email='jane.smith@example.com',
Description='Contact of the day');
insert jane;
// 1. Upsert using an idLookup field
// Create a second sObject variable.
// This variable doesn’t have any ID set.
Contact jane2 = new Contact(FirstName='Jane',
LastName='Smith',
Email='jane.smith@example.com',
Description='Prefers to be contacted by email.');
// Upsert the contact by using the idLookup field for matching.
upsert jane2 Contact.fields.Email;
// Verify that the contact has been updated
System.assertEquals('Prefers to be contacted by email.',
[SELECT Description FROM Contact WHERE Id=:jane.Id].Description);
-------------
I understand the gist of the code, but cannot find a reference in the Apex documentation about the syntax within the bolded lines. Would someone break down each piece of this code for me? In particular why does the [Select...].Description phrase require the .Description?
My dilemma has been that I have a great deal of experience with programming, but not with Java. So much of the material I'm finding assumes knowledge of Jave syntax.
Hi Anne,
Below are the syntax for Upsert and Assers:
1. Upsert: this method will update existing record or create new incase record not exist.
syntax: upsert(list);
2. Assert: this method is used to check the output in test class.
syntax: System.assertEquals(acc.name, ‘testName’);
Hope this helps you, please mark it best answer if works for you.
Thanks,
Gaurav
All Answers
Hi Anne,
Below are the syntax for Upsert and Assers:
1. Upsert: this method will update existing record or create new incase record not exist.
syntax: upsert(list);
2. Assert: this method is used to check the output in test class.
syntax: System.assertEquals(acc.name, ‘testName’);
Hope this helps you, please mark it best answer if works for you.
Thanks,
Gaurav
Hope this makes it easy to understand...
[SELECT Description FROM Contact WHERE Id=:jane.Id] returns a Contact Object with a filled Description field.
Hence,
[SELECT Description FROM Contact WHERE Id=:jane.Id].Description
|_____________________________________________|
|
tmpContactObject
means "tmpContactObject.Description"
This gives the fetched value as 'Prefers to be contacted by email.'
Thanks,
Deepali Bhangale