Custom Geolocation Fields not populating in test classregd Test classUnit Test is Providing 0% Coverage for Apex Trigger“Required fields are missing: [ProfileId]: [ProfileId]” when running Apex Class Test for ChatterAnswersAuthProviderRegTestschema.getglobaldescribe needs test classTest Class for Triggers on Account ObjectError on Test Class - System.QueryException: List has no rows for assignment to SObjectformula fields in test class run?How to convert Datetime datatype to Date format only?Trying to access account geolocation fields in after insert triggerHello i am not able to get the result from this test class
Examples of fluid (including air) being used to transmit digital data?
What are the effects of abstaining from eating a certain flavor?
Shipped package arrived - didn't order, possible scam?
Intern not wearing safety equipment; how could I have handled this differently?
What does "spinning upon the shoals" mean?
Can you create a free-floating MASYU puzzle?
Custom Geolocation Fields not populating in test class
Can the Four Elements monk's Shape the Flowing River elemental discipline create stairs by expending a single ki point?
Need a non-volatile memory IC with near unlimited read/write operations capability
As a supervisor, what feedback would you expect from a PhD who quits?
Why do airports remove/realign runways?
Wouldn't putting an electronic key inside a small Faraday cage render it completely useless?
How did the Time Lords put a whole "Star" in a Tardis?
Why did RFK loathe LBJ?
Sense of humor in your sci-fi stories
What exactly is a "murder hobo"?
What factors could lead to bishops establishing monastic armies?
How to understand flavors and when to use combination of them?
Was it ever illegal to name a pig "Napoleon" in France?
How did the IEC decide to create kibibytes?
Why does the Misal rico de Cisneros uses the word "Qiſſa", and what is it supposed to mean? Why not "Miſſa" (Missa)?
When moving a unique_ptr into a lambda, why is it not possible to call reset?
Category-theoretic treatment of diffs, patches and merging?
Why SQL does not use the indexed view?
Custom Geolocation Fields not populating in test class
regd Test classUnit Test is Providing 0% Coverage for Apex Trigger“Required fields are missing: [ProfileId]: [ProfileId]” when running Apex Class Test for ChatterAnswersAuthProviderRegTestschema.getglobaldescribe needs test classTest Class for Triggers on Account ObjectError on Test Class - System.QueryException: List has no rows for assignment to SObjectformula fields in test class run?How to convert Datetime datatype to Date format only?Trying to access account geolocation fields in after insert triggerHello i am not able to get the result from this test class
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm trying to create a test class for a trigger that executes business logic based on the a custom Geolocation field on the quote (this calls a class that does and http callout to retrieve the street address via reverse-geolocation).
However, the Geolocation codes are always being read as null, even when they are populated in the test class.
Here is the test class:
@isTest
private class quoteTriggerTest
@TestSetup static void createOpportunity()
Opportunity opp = new Opportunity();
opp.Name = 'Test Opportunity';
opp.CloseDate = date.today().addDays(30);
opp.StageName = 'Qualification';
insert opp;
@isTest static void newQuoteWithGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote with Geolocation';
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
insert q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
@isTest static void newQuoteWithoutGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test
Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote without Geolocation';
insert q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
q.Name = 'Updated Test Quote without Geolocation';
update q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
@isTest static void updatedQuoteWithGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test
Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote Updated with Geolocation';
insert q;
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
update q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
And here is the trigger:
trigger quoteTrigger on Quote (after insert, after update)
List<Quote> quoteList = new List<Quote>();
if(Trigger.isInsert)
for(Quote q : Trigger.new)
if (q.Job_Site_Geolocation__c != NULL)
quoteList.add(q);
if(Trigger.isUpdate)
for(Quote q : Trigger.new)
Quote oldQuote = Trigger.oldMap.get(q.Id);
Quote newQuote = Trigger.newMap.get(q.Id);
if(oldQuote.Job_Site_Geolocation__c !=
newQuote.Job_Site_Geolocation__c)
quoteList.add(q);
for(Quote q : quoteList)
System.debug('Quote Id: ');
retrieveAddress.getAddress(quoteList);
I can see from the debug log that the variables in the test class do have the geolocation codes, but when the the DML gets executed and the trigger fires, the geolocation field is null.
When I manually create a Quote with the geolocation data in the database, the trigger also fails to see that the geolocation field is not null.
Is there something additional I need to do in the trigger so that it can see the geolocation field?
apex trigger
|
show 2 more comments
I'm trying to create a test class for a trigger that executes business logic based on the a custom Geolocation field on the quote (this calls a class that does and http callout to retrieve the street address via reverse-geolocation).
However, the Geolocation codes are always being read as null, even when they are populated in the test class.
Here is the test class:
@isTest
private class quoteTriggerTest
@TestSetup static void createOpportunity()
Opportunity opp = new Opportunity();
opp.Name = 'Test Opportunity';
opp.CloseDate = date.today().addDays(30);
opp.StageName = 'Qualification';
insert opp;
@isTest static void newQuoteWithGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote with Geolocation';
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
insert q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
@isTest static void newQuoteWithoutGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test
Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote without Geolocation';
insert q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
q.Name = 'Updated Test Quote without Geolocation';
update q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
@isTest static void updatedQuoteWithGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test
Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote Updated with Geolocation';
insert q;
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
update q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
And here is the trigger:
trigger quoteTrigger on Quote (after insert, after update)
List<Quote> quoteList = new List<Quote>();
if(Trigger.isInsert)
for(Quote q : Trigger.new)
if (q.Job_Site_Geolocation__c != NULL)
quoteList.add(q);
if(Trigger.isUpdate)
for(Quote q : Trigger.new)
Quote oldQuote = Trigger.oldMap.get(q.Id);
Quote newQuote = Trigger.newMap.get(q.Id);
if(oldQuote.Job_Site_Geolocation__c !=
newQuote.Job_Site_Geolocation__c)
quoteList.add(q);
for(Quote q : quoteList)
System.debug('Quote Id: ');
retrieveAddress.getAddress(quoteList);
I can see from the debug log that the variables in the test class do have the geolocation codes, but when the the DML gets executed and the trigger fires, the geolocation field is null.
When I manually create a Quote with the geolocation data in the database, the trigger also fails to see that the geolocation field is not null.
Is there something additional I need to do in the trigger so that it can see the geolocation field?
apex trigger
Here is the full debug log from running the test class: github.com/mlregal/debug-logs/blob/master/…
– TheDevAdmin
8 hours ago
Can you add code forretrieveAddress.getAddress
and are you using Future or queuable?
– Pranay Jaiswal
7 hours ago
@pranay-jaiswal The class isn't actually being called because the quoteList variable is empty.
– TheDevAdmin
7 hours ago
You mean in your test methodupdatedQuoteWithGeolocation
after DML update ,Job_Site_Geolocation__c
is still blank?
– Pranay Jaiswal
7 hours ago
1
You have to query it from the database after doing update. ,q = [SELECT Id , Job_Site_Geolocation__c FROM Quote WHERE Id=:q.id]; System.debug(q.Job_Site_Geolocation__c ;
– Pranay Jaiswal
7 hours ago
|
show 2 more comments
I'm trying to create a test class for a trigger that executes business logic based on the a custom Geolocation field on the quote (this calls a class that does and http callout to retrieve the street address via reverse-geolocation).
However, the Geolocation codes are always being read as null, even when they are populated in the test class.
Here is the test class:
@isTest
private class quoteTriggerTest
@TestSetup static void createOpportunity()
Opportunity opp = new Opportunity();
opp.Name = 'Test Opportunity';
opp.CloseDate = date.today().addDays(30);
opp.StageName = 'Qualification';
insert opp;
@isTest static void newQuoteWithGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote with Geolocation';
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
insert q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
@isTest static void newQuoteWithoutGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test
Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote without Geolocation';
insert q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
q.Name = 'Updated Test Quote without Geolocation';
update q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
@isTest static void updatedQuoteWithGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test
Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote Updated with Geolocation';
insert q;
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
update q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
And here is the trigger:
trigger quoteTrigger on Quote (after insert, after update)
List<Quote> quoteList = new List<Quote>();
if(Trigger.isInsert)
for(Quote q : Trigger.new)
if (q.Job_Site_Geolocation__c != NULL)
quoteList.add(q);
if(Trigger.isUpdate)
for(Quote q : Trigger.new)
Quote oldQuote = Trigger.oldMap.get(q.Id);
Quote newQuote = Trigger.newMap.get(q.Id);
if(oldQuote.Job_Site_Geolocation__c !=
newQuote.Job_Site_Geolocation__c)
quoteList.add(q);
for(Quote q : quoteList)
System.debug('Quote Id: ');
retrieveAddress.getAddress(quoteList);
I can see from the debug log that the variables in the test class do have the geolocation codes, but when the the DML gets executed and the trigger fires, the geolocation field is null.
When I manually create a Quote with the geolocation data in the database, the trigger also fails to see that the geolocation field is not null.
Is there something additional I need to do in the trigger so that it can see the geolocation field?
apex trigger
I'm trying to create a test class for a trigger that executes business logic based on the a custom Geolocation field on the quote (this calls a class that does and http callout to retrieve the street address via reverse-geolocation).
However, the Geolocation codes are always being read as null, even when they are populated in the test class.
Here is the test class:
@isTest
private class quoteTriggerTest
@TestSetup static void createOpportunity()
Opportunity opp = new Opportunity();
opp.Name = 'Test Opportunity';
opp.CloseDate = date.today().addDays(30);
opp.StageName = 'Qualification';
insert opp;
@isTest static void newQuoteWithGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote with Geolocation';
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
insert q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
@isTest static void newQuoteWithoutGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test
Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote without Geolocation';
insert q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
q.Name = 'Updated Test Quote without Geolocation';
update q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
@isTest static void updatedQuoteWithGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test
Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote Updated with Geolocation';
insert q;
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
update q;
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__c);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
And here is the trigger:
trigger quoteTrigger on Quote (after insert, after update)
List<Quote> quoteList = new List<Quote>();
if(Trigger.isInsert)
for(Quote q : Trigger.new)
if (q.Job_Site_Geolocation__c != NULL)
quoteList.add(q);
if(Trigger.isUpdate)
for(Quote q : Trigger.new)
Quote oldQuote = Trigger.oldMap.get(q.Id);
Quote newQuote = Trigger.newMap.get(q.Id);
if(oldQuote.Job_Site_Geolocation__c !=
newQuote.Job_Site_Geolocation__c)
quoteList.add(q);
for(Quote q : quoteList)
System.debug('Quote Id: ');
retrieveAddress.getAddress(quoteList);
I can see from the debug log that the variables in the test class do have the geolocation codes, but when the the DML gets executed and the trigger fires, the geolocation field is null.
When I manually create a Quote with the geolocation data in the database, the trigger also fails to see that the geolocation field is not null.
Is there something additional I need to do in the trigger so that it can see the geolocation field?
apex trigger
apex trigger
asked 8 hours ago
TheDevAdminTheDevAdmin
1031 silver badge13 bronze badges
1031 silver badge13 bronze badges
Here is the full debug log from running the test class: github.com/mlregal/debug-logs/blob/master/…
– TheDevAdmin
8 hours ago
Can you add code forretrieveAddress.getAddress
and are you using Future or queuable?
– Pranay Jaiswal
7 hours ago
@pranay-jaiswal The class isn't actually being called because the quoteList variable is empty.
– TheDevAdmin
7 hours ago
You mean in your test methodupdatedQuoteWithGeolocation
after DML update ,Job_Site_Geolocation__c
is still blank?
– Pranay Jaiswal
7 hours ago
1
You have to query it from the database after doing update. ,q = [SELECT Id , Job_Site_Geolocation__c FROM Quote WHERE Id=:q.id]; System.debug(q.Job_Site_Geolocation__c ;
– Pranay Jaiswal
7 hours ago
|
show 2 more comments
Here is the full debug log from running the test class: github.com/mlregal/debug-logs/blob/master/…
– TheDevAdmin
8 hours ago
Can you add code forretrieveAddress.getAddress
and are you using Future or queuable?
– Pranay Jaiswal
7 hours ago
@pranay-jaiswal The class isn't actually being called because the quoteList variable is empty.
– TheDevAdmin
7 hours ago
You mean in your test methodupdatedQuoteWithGeolocation
after DML update ,Job_Site_Geolocation__c
is still blank?
– Pranay Jaiswal
7 hours ago
1
You have to query it from the database after doing update. ,q = [SELECT Id , Job_Site_Geolocation__c FROM Quote WHERE Id=:q.id]; System.debug(q.Job_Site_Geolocation__c ;
– Pranay Jaiswal
7 hours ago
Here is the full debug log from running the test class: github.com/mlregal/debug-logs/blob/master/…
– TheDevAdmin
8 hours ago
Here is the full debug log from running the test class: github.com/mlregal/debug-logs/blob/master/…
– TheDevAdmin
8 hours ago
Can you add code for
retrieveAddress.getAddress
and are you using Future or queuable?– Pranay Jaiswal
7 hours ago
Can you add code for
retrieveAddress.getAddress
and are you using Future or queuable?– Pranay Jaiswal
7 hours ago
@pranay-jaiswal The class isn't actually being called because the quoteList variable is empty.
– TheDevAdmin
7 hours ago
@pranay-jaiswal The class isn't actually being called because the quoteList variable is empty.
– TheDevAdmin
7 hours ago
You mean in your test method
updatedQuoteWithGeolocation
after DML update , Job_Site_Geolocation__c
is still blank?– Pranay Jaiswal
7 hours ago
You mean in your test method
updatedQuoteWithGeolocation
after DML update , Job_Site_Geolocation__c
is still blank?– Pranay Jaiswal
7 hours ago
1
1
You have to query it from the database after doing update. ,
q = [SELECT Id , Job_Site_Geolocation__c FROM Quote WHERE Id=:q.id]; System.debug(q.Job_Site_Geolocation__c ;
– Pranay Jaiswal
7 hours ago
You have to query it from the database after doing update. ,
q = [SELECT Id , Job_Site_Geolocation__c FROM Quote WHERE Id=:q.id]; System.debug(q.Job_Site_Geolocation__c ;
– Pranay Jaiswal
7 hours ago
|
show 2 more comments
2 Answers
2
active
oldest
votes
In addition to Phil's points, when you do DML that causes automation to run, and then you want to test that the automation worked (or in production code, get the results of that automation), you need to re-query the record(s). Until you do, the object only has what you've set on it (except the record Id, which does get added to the in-memory object when you insert it). That's the main reason why your System.debug()
statements show NULL for those fields. And, you can't do much with the compound Geolocation field, including use it in SOQL. You have to work with the lat/long components. So, your newQuoteWithGeolocation()
test will end up looking something like this:
@isTest static void newQuoteWithGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote with Geolocation';
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
// RetrieveAddressMock() could be the name of your class that implements the
// HttpCalloutMock interface
// https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
Test.setMock(HttpCalloutMock.class, new RetrieveAddressMock());
Test.startTest();
insert q;
Test.stopTest();
q = [SELECT Id, Name, Job_Site_Geolocation__latitude__s, Job_Site_Geolocation__longitude__s,
Job_Site_Street__c, Job_Site_City__c, Job_Site_State_Province__c,
Job_Site_Postal_Zip__c, Job_Site_Country__c
FROM Quote
WHERE Name = 'Test Quote with Geolocation'
LIMIT 1]
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__latitude__s + ',' + q.Job_Site_Geolocation__longitude__s);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
// Debugs are fine while you're writing your code, but you'll want some asserts
System.assertEquals('[Street address returned by mock class]',q.Job_Site_Street__c,'Street address does not match mock');
System.assertEquals(36.072528, + q.Job_Site_Geolocation__latitude__s,'Latitude got changed.');
1
This pointed me in the right direction - it looks like the Trigger can't actually read the geolocation fields either. When I updated the Trigger to look at Job_Site_Geolocation__latitude__s and Job_Site_Geolocation__longitude__s, the test ran correctly.
– TheDevAdmin
7 hours ago
1
Yep, I was about to update my answer with this - did some tests, and directly debugging the compound Geo field always gave NULL.
– Thomas Taylor
7 hours ago
add a comment |
You need to create a callout mock and use Test.startTest/Test.stopTest around the code that invokes the callout. See the documentation for details.
Fundamentally, tests cannot use real callouts. Using a mock, your tests can check that the code calling the callout behaves correctly for the details it gets back. This lets you simulate "happy path" and failure cases in your tests.
(I may have misinterpreted your question, reading it again, but consider these points)
– Phil W
8 hours ago
Yes, I have that set up, but right now, I'm stuck at getting the trigger to recognize that the records I've inserted/updated have actual values in the geolocation fields so they get added to the quoteList variable.
– TheDevAdmin
8 hours ago
Yeah, I wasn't sure as I couldn't see anything in your test class.
– Phil W
7 hours ago
Do you have some process builder, workflow or lightning flow, or a separate trigger, that could be clearing these values?
– Phil W
7 hours ago
No. This is a brand new org and this is the only automation other than the CPQ managed package, which doesn't touch these custom fields.
– TheDevAdmin
7 hours ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "459"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f268396%2fcustom-geolocation-fields-not-populating-in-test-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In addition to Phil's points, when you do DML that causes automation to run, and then you want to test that the automation worked (or in production code, get the results of that automation), you need to re-query the record(s). Until you do, the object only has what you've set on it (except the record Id, which does get added to the in-memory object when you insert it). That's the main reason why your System.debug()
statements show NULL for those fields. And, you can't do much with the compound Geolocation field, including use it in SOQL. You have to work with the lat/long components. So, your newQuoteWithGeolocation()
test will end up looking something like this:
@isTest static void newQuoteWithGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote with Geolocation';
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
// RetrieveAddressMock() could be the name of your class that implements the
// HttpCalloutMock interface
// https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
Test.setMock(HttpCalloutMock.class, new RetrieveAddressMock());
Test.startTest();
insert q;
Test.stopTest();
q = [SELECT Id, Name, Job_Site_Geolocation__latitude__s, Job_Site_Geolocation__longitude__s,
Job_Site_Street__c, Job_Site_City__c, Job_Site_State_Province__c,
Job_Site_Postal_Zip__c, Job_Site_Country__c
FROM Quote
WHERE Name = 'Test Quote with Geolocation'
LIMIT 1]
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__latitude__s + ',' + q.Job_Site_Geolocation__longitude__s);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
// Debugs are fine while you're writing your code, but you'll want some asserts
System.assertEquals('[Street address returned by mock class]',q.Job_Site_Street__c,'Street address does not match mock');
System.assertEquals(36.072528, + q.Job_Site_Geolocation__latitude__s,'Latitude got changed.');
1
This pointed me in the right direction - it looks like the Trigger can't actually read the geolocation fields either. When I updated the Trigger to look at Job_Site_Geolocation__latitude__s and Job_Site_Geolocation__longitude__s, the test ran correctly.
– TheDevAdmin
7 hours ago
1
Yep, I was about to update my answer with this - did some tests, and directly debugging the compound Geo field always gave NULL.
– Thomas Taylor
7 hours ago
add a comment |
In addition to Phil's points, when you do DML that causes automation to run, and then you want to test that the automation worked (or in production code, get the results of that automation), you need to re-query the record(s). Until you do, the object only has what you've set on it (except the record Id, which does get added to the in-memory object when you insert it). That's the main reason why your System.debug()
statements show NULL for those fields. And, you can't do much with the compound Geolocation field, including use it in SOQL. You have to work with the lat/long components. So, your newQuoteWithGeolocation()
test will end up looking something like this:
@isTest static void newQuoteWithGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote with Geolocation';
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
// RetrieveAddressMock() could be the name of your class that implements the
// HttpCalloutMock interface
// https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
Test.setMock(HttpCalloutMock.class, new RetrieveAddressMock());
Test.startTest();
insert q;
Test.stopTest();
q = [SELECT Id, Name, Job_Site_Geolocation__latitude__s, Job_Site_Geolocation__longitude__s,
Job_Site_Street__c, Job_Site_City__c, Job_Site_State_Province__c,
Job_Site_Postal_Zip__c, Job_Site_Country__c
FROM Quote
WHERE Name = 'Test Quote with Geolocation'
LIMIT 1]
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__latitude__s + ',' + q.Job_Site_Geolocation__longitude__s);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
// Debugs are fine while you're writing your code, but you'll want some asserts
System.assertEquals('[Street address returned by mock class]',q.Job_Site_Street__c,'Street address does not match mock');
System.assertEquals(36.072528, + q.Job_Site_Geolocation__latitude__s,'Latitude got changed.');
1
This pointed me in the right direction - it looks like the Trigger can't actually read the geolocation fields either. When I updated the Trigger to look at Job_Site_Geolocation__latitude__s and Job_Site_Geolocation__longitude__s, the test ran correctly.
– TheDevAdmin
7 hours ago
1
Yep, I was about to update my answer with this - did some tests, and directly debugging the compound Geo field always gave NULL.
– Thomas Taylor
7 hours ago
add a comment |
In addition to Phil's points, when you do DML that causes automation to run, and then you want to test that the automation worked (or in production code, get the results of that automation), you need to re-query the record(s). Until you do, the object only has what you've set on it (except the record Id, which does get added to the in-memory object when you insert it). That's the main reason why your System.debug()
statements show NULL for those fields. And, you can't do much with the compound Geolocation field, including use it in SOQL. You have to work with the lat/long components. So, your newQuoteWithGeolocation()
test will end up looking something like this:
@isTest static void newQuoteWithGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote with Geolocation';
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
// RetrieveAddressMock() could be the name of your class that implements the
// HttpCalloutMock interface
// https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
Test.setMock(HttpCalloutMock.class, new RetrieveAddressMock());
Test.startTest();
insert q;
Test.stopTest();
q = [SELECT Id, Name, Job_Site_Geolocation__latitude__s, Job_Site_Geolocation__longitude__s,
Job_Site_Street__c, Job_Site_City__c, Job_Site_State_Province__c,
Job_Site_Postal_Zip__c, Job_Site_Country__c
FROM Quote
WHERE Name = 'Test Quote with Geolocation'
LIMIT 1]
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__latitude__s + ',' + q.Job_Site_Geolocation__longitude__s);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
// Debugs are fine while you're writing your code, but you'll want some asserts
System.assertEquals('[Street address returned by mock class]',q.Job_Site_Street__c,'Street address does not match mock');
System.assertEquals(36.072528, + q.Job_Site_Geolocation__latitude__s,'Latitude got changed.');
In addition to Phil's points, when you do DML that causes automation to run, and then you want to test that the automation worked (or in production code, get the results of that automation), you need to re-query the record(s). Until you do, the object only has what you've set on it (except the record Id, which does get added to the in-memory object when you insert it). That's the main reason why your System.debug()
statements show NULL for those fields. And, you can't do much with the compound Geolocation field, including use it in SOQL. You have to work with the lat/long components. So, your newQuoteWithGeolocation()
test will end up looking something like this:
@isTest static void newQuoteWithGeolocation()
Quote q = new Quote ();
q.OpportunityId = [SELECT Id FROM Opportunity WHERE Name='Test Opportunity' LIMIT 1].Id;
q.Name = 'Test Quote with Geolocation';
q.Job_Site_Geolocation__latitude__s = 36.072528;
q.Job_Site_Geolocation__longitude__s = -95.925575;
// RetrieveAddressMock() could be the name of your class that implements the
// HttpCalloutMock interface
// https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm
Test.setMock(HttpCalloutMock.class, new RetrieveAddressMock());
Test.startTest();
insert q;
Test.stopTest();
q = [SELECT Id, Name, Job_Site_Geolocation__latitude__s, Job_Site_Geolocation__longitude__s,
Job_Site_Street__c, Job_Site_City__c, Job_Site_State_Province__c,
Job_Site_Postal_Zip__c, Job_Site_Country__c
FROM Quote
WHERE Name = 'Test Quote with Geolocation'
LIMIT 1]
System.debug('Quote Name: ' + q.Name);
System.debug('Quote Geolocation: ' + q.Job_Site_Geolocation__latitude__s + ',' + q.Job_Site_Geolocation__longitude__s);
System.debug('Job Site Address: ' + q.Job_Site_Street__c + ', ' +
q.Job_Site_City__c + ', ' + q.Job_Site_State_Province__c + ' ' +
q.Job_Site_Postal_Zip__c + ', ' + q.Job_Site_Country__c);
// Debugs are fine while you're writing your code, but you'll want some asserts
System.assertEquals('[Street address returned by mock class]',q.Job_Site_Street__c,'Street address does not match mock');
System.assertEquals(36.072528, + q.Job_Site_Geolocation__latitude__s,'Latitude got changed.');
edited 7 hours ago
answered 7 hours ago
Thomas TaylorThomas Taylor
3,10114 silver badges33 bronze badges
3,10114 silver badges33 bronze badges
1
This pointed me in the right direction - it looks like the Trigger can't actually read the geolocation fields either. When I updated the Trigger to look at Job_Site_Geolocation__latitude__s and Job_Site_Geolocation__longitude__s, the test ran correctly.
– TheDevAdmin
7 hours ago
1
Yep, I was about to update my answer with this - did some tests, and directly debugging the compound Geo field always gave NULL.
– Thomas Taylor
7 hours ago
add a comment |
1
This pointed me in the right direction - it looks like the Trigger can't actually read the geolocation fields either. When I updated the Trigger to look at Job_Site_Geolocation__latitude__s and Job_Site_Geolocation__longitude__s, the test ran correctly.
– TheDevAdmin
7 hours ago
1
Yep, I was about to update my answer with this - did some tests, and directly debugging the compound Geo field always gave NULL.
– Thomas Taylor
7 hours ago
1
1
This pointed me in the right direction - it looks like the Trigger can't actually read the geolocation fields either. When I updated the Trigger to look at Job_Site_Geolocation__latitude__s and Job_Site_Geolocation__longitude__s, the test ran correctly.
– TheDevAdmin
7 hours ago
This pointed me in the right direction - it looks like the Trigger can't actually read the geolocation fields either. When I updated the Trigger to look at Job_Site_Geolocation__latitude__s and Job_Site_Geolocation__longitude__s, the test ran correctly.
– TheDevAdmin
7 hours ago
1
1
Yep, I was about to update my answer with this - did some tests, and directly debugging the compound Geo field always gave NULL.
– Thomas Taylor
7 hours ago
Yep, I was about to update my answer with this - did some tests, and directly debugging the compound Geo field always gave NULL.
– Thomas Taylor
7 hours ago
add a comment |
You need to create a callout mock and use Test.startTest/Test.stopTest around the code that invokes the callout. See the documentation for details.
Fundamentally, tests cannot use real callouts. Using a mock, your tests can check that the code calling the callout behaves correctly for the details it gets back. This lets you simulate "happy path" and failure cases in your tests.
(I may have misinterpreted your question, reading it again, but consider these points)
– Phil W
8 hours ago
Yes, I have that set up, but right now, I'm stuck at getting the trigger to recognize that the records I've inserted/updated have actual values in the geolocation fields so they get added to the quoteList variable.
– TheDevAdmin
8 hours ago
Yeah, I wasn't sure as I couldn't see anything in your test class.
– Phil W
7 hours ago
Do you have some process builder, workflow or lightning flow, or a separate trigger, that could be clearing these values?
– Phil W
7 hours ago
No. This is a brand new org and this is the only automation other than the CPQ managed package, which doesn't touch these custom fields.
– TheDevAdmin
7 hours ago
add a comment |
You need to create a callout mock and use Test.startTest/Test.stopTest around the code that invokes the callout. See the documentation for details.
Fundamentally, tests cannot use real callouts. Using a mock, your tests can check that the code calling the callout behaves correctly for the details it gets back. This lets you simulate "happy path" and failure cases in your tests.
(I may have misinterpreted your question, reading it again, but consider these points)
– Phil W
8 hours ago
Yes, I have that set up, but right now, I'm stuck at getting the trigger to recognize that the records I've inserted/updated have actual values in the geolocation fields so they get added to the quoteList variable.
– TheDevAdmin
8 hours ago
Yeah, I wasn't sure as I couldn't see anything in your test class.
– Phil W
7 hours ago
Do you have some process builder, workflow or lightning flow, or a separate trigger, that could be clearing these values?
– Phil W
7 hours ago
No. This is a brand new org and this is the only automation other than the CPQ managed package, which doesn't touch these custom fields.
– TheDevAdmin
7 hours ago
add a comment |
You need to create a callout mock and use Test.startTest/Test.stopTest around the code that invokes the callout. See the documentation for details.
Fundamentally, tests cannot use real callouts. Using a mock, your tests can check that the code calling the callout behaves correctly for the details it gets back. This lets you simulate "happy path" and failure cases in your tests.
You need to create a callout mock and use Test.startTest/Test.stopTest around the code that invokes the callout. See the documentation for details.
Fundamentally, tests cannot use real callouts. Using a mock, your tests can check that the code calling the callout behaves correctly for the details it gets back. This lets you simulate "happy path" and failure cases in your tests.
answered 8 hours ago
Phil WPhil W
2,1731 gold badge3 silver badges11 bronze badges
2,1731 gold badge3 silver badges11 bronze badges
(I may have misinterpreted your question, reading it again, but consider these points)
– Phil W
8 hours ago
Yes, I have that set up, but right now, I'm stuck at getting the trigger to recognize that the records I've inserted/updated have actual values in the geolocation fields so they get added to the quoteList variable.
– TheDevAdmin
8 hours ago
Yeah, I wasn't sure as I couldn't see anything in your test class.
– Phil W
7 hours ago
Do you have some process builder, workflow or lightning flow, or a separate trigger, that could be clearing these values?
– Phil W
7 hours ago
No. This is a brand new org and this is the only automation other than the CPQ managed package, which doesn't touch these custom fields.
– TheDevAdmin
7 hours ago
add a comment |
(I may have misinterpreted your question, reading it again, but consider these points)
– Phil W
8 hours ago
Yes, I have that set up, but right now, I'm stuck at getting the trigger to recognize that the records I've inserted/updated have actual values in the geolocation fields so they get added to the quoteList variable.
– TheDevAdmin
8 hours ago
Yeah, I wasn't sure as I couldn't see anything in your test class.
– Phil W
7 hours ago
Do you have some process builder, workflow or lightning flow, or a separate trigger, that could be clearing these values?
– Phil W
7 hours ago
No. This is a brand new org and this is the only automation other than the CPQ managed package, which doesn't touch these custom fields.
– TheDevAdmin
7 hours ago
(I may have misinterpreted your question, reading it again, but consider these points)
– Phil W
8 hours ago
(I may have misinterpreted your question, reading it again, but consider these points)
– Phil W
8 hours ago
Yes, I have that set up, but right now, I'm stuck at getting the trigger to recognize that the records I've inserted/updated have actual values in the geolocation fields so they get added to the quoteList variable.
– TheDevAdmin
8 hours ago
Yes, I have that set up, but right now, I'm stuck at getting the trigger to recognize that the records I've inserted/updated have actual values in the geolocation fields so they get added to the quoteList variable.
– TheDevAdmin
8 hours ago
Yeah, I wasn't sure as I couldn't see anything in your test class.
– Phil W
7 hours ago
Yeah, I wasn't sure as I couldn't see anything in your test class.
– Phil W
7 hours ago
Do you have some process builder, workflow or lightning flow, or a separate trigger, that could be clearing these values?
– Phil W
7 hours ago
Do you have some process builder, workflow or lightning flow, or a separate trigger, that could be clearing these values?
– Phil W
7 hours ago
No. This is a brand new org and this is the only automation other than the CPQ managed package, which doesn't touch these custom fields.
– TheDevAdmin
7 hours ago
No. This is a brand new org and this is the only automation other than the CPQ managed package, which doesn't touch these custom fields.
– TheDevAdmin
7 hours ago
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f268396%2fcustom-geolocation-fields-not-populating-in-test-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Here is the full debug log from running the test class: github.com/mlregal/debug-logs/blob/master/…
– TheDevAdmin
8 hours ago
Can you add code for
retrieveAddress.getAddress
and are you using Future or queuable?– Pranay Jaiswal
7 hours ago
@pranay-jaiswal The class isn't actually being called because the quoteList variable is empty.
– TheDevAdmin
7 hours ago
You mean in your test method
updatedQuoteWithGeolocation
after DML update ,Job_Site_Geolocation__c
is still blank?– Pranay Jaiswal
7 hours ago
1
You have to query it from the database after doing update. ,
q = [SELECT Id , Job_Site_Geolocation__c FROM Quote WHERE Id=:q.id]; System.debug(q.Job_Site_Geolocation__c ;
– Pranay Jaiswal
7 hours ago