Sunday, June 4, 2023

Hey Google Bard, please write a Salesforce Apex Trigger

Functional Test Case 

Given An existing Contact 
When A user updates an existing Contact email address 
Then Timestamp a field on the Contact record, Email_Updated_Date__c, with the current date/time 

Test Driven Development 

@isTest
private class ProcessContacts_Test {
@testSetup static void setup() {
// Create contact record
List<Contact> testContacts = new List<Contact>();
for(Integer i=0;i<1;i++) {
testContacts.add(new Contact(FirstName = 'GoogleBard'+i,
LastName = 'Test'));
}
insert testContacts;
}
@isTest static void email_updated_date() {
// Get the first Contact from testSetup data
Contact varContact = [SELECT Email
FROM Contact
WHERE FirstName='GoogleBard0'
LIMIT 1];
// Verify that test contact Email is null.
System.assertEquals(null, varContact.Email);
Test.startTest();
// Update the contacts email field to a new value
varContact.Email = 'update.bard@ai.com';
update varContact;
Test.stopTest();
// Assert the Bard generated trigger worked as expected
System.assertEquals(System.now(),
[SELECT Email_Updated_Date__c
FROM Contact
WHERE FirstName = 'GoogleBard0']
.Email_Updated_Date__c);
}
}
Bard Prompt 

I want you to act as a salesforce apex language interpreter. Write an apex trigger to process contacts before update and check if the field Email is being updated. If yes then update a field on that contact record Email_Updated_Date__c to the current date time

Bard Prompt Results


Unit Test Results


No comments:

Post a Comment