You need to sign in to do that
Don't have an account?
Trail head using future Mehods
Hello everyone I could use some help on the using future Methods trailhead. Please keep in mind I am not an Apex coder at all but I am trying to learn as much as I can.
Here is the Task
Create an Apex class with a method using the @future annotation that accepts a List of Account IDs and updates a custom field on the Account object with the number of contacts associated to the Account. Write unit tests that achieve 100% code coverage for the class.
Create a field on the Account object called 'Number_of_Contacts__c' of type Number. This field will hold the total number of Contacts for the Account.
Create an Apex class called 'AccountProcessor' that contains a 'countContacts' method that accepts a List of Account IDs. This method must use the @future annotation.
For each Account ID passed to the method, count the number of Contact records associated to it and update the 'Number_of_Contacts__c' field with this value.
Create an Apex test class called 'AccountProcessorTest'.
The unit tests must cover all lines of code included in the AccountProcessor class, resulting in 100% code coverage.
Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.
I have written an Apex class and a Test class but I think I am doing them both Wrong: because when I try to check the challenge i Get this error
Challenge Not yet complete... here's what's wrong:
The 'AccountProcessorTest' test class doesn't appear to be calling the 'AccountProcessor.countContacts' method between Test.startTest() and Test.stopTest().
When I run the Test class I get this error :
System.QueryException: List has no rows for assignment to SObject
Here is the CLASS:
Here is the Test Class:
I have used alot or diffrent google searches to get me this far. But I have not Idea if Im even remotly close to the right answer or not.
Here is the Task
Create an Apex class with a method using the @future annotation that accepts a List of Account IDs and updates a custom field on the Account object with the number of contacts associated to the Account. Write unit tests that achieve 100% code coverage for the class.
Create a field on the Account object called 'Number_of_Contacts__c' of type Number. This field will hold the total number of Contacts for the Account.
Create an Apex class called 'AccountProcessor' that contains a 'countContacts' method that accepts a List of Account IDs. This method must use the @future annotation.
For each Account ID passed to the method, count the number of Contact records associated to it and update the 'Number_of_Contacts__c' field with this value.
Create an Apex test class called 'AccountProcessorTest'.
The unit tests must cover all lines of code included in the AccountProcessor class, resulting in 100% code coverage.
Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.
I have written an Apex class and a Test class but I think I am doing them both Wrong: because when I try to check the challenge i Get this error
Challenge Not yet complete... here's what's wrong:
The 'AccountProcessorTest' test class doesn't appear to be calling the 'AccountProcessor.countContacts' method between Test.startTest() and Test.stopTest().
When I run the Test class I get this error :
System.QueryException: List has no rows for assignment to SObject
Here is the CLASS:
public class AccountProcessor { @future public static void someFutureMethod(List<id> scope) { Account[] updates = new Account[] {}; for (AggregateResult ar : [ select AccountId a, count(Id) c from Contact where AccountId in :scope group by AccountId ]) { updates.add(new Account( Id = (Id) ar.get('a'), Number_of_Contacts__c = (Decimal) ar.get('c') )); } update updates; } }
Here is the Test Class:
@IsTest public class AccountProcessorTest { public static testmethod void TestAccountProcessorTest() { Test.startTest(); Account a = new Account(); a.Name = 'Test Account'; Insert a; Contact cont = New Contact(); cont.FirstName ='Bob'; cont.LastName ='Masters'; cont.AccountId = a.Id; Insert cont; Test.stopTest() ; Contact ACC = [select AccountId from Contact where id = :a.id LIMIT 1]; System.assert(Cont.AccountId != null); System.assertequals(cont.id, ACC.AccountId); }}
I have used alot or diffrent google searches to get me this far. But I have not Idea if Im even remotly close to the right answer or not.
Please update your Apex class like below :-
Test class:-
Please let us know if this will help you
Thanks
Amit Chaudhary
All Answers
Please update your Apex class like below :-
Test class:-
Please let us know if this will help you
Thanks
Amit Chaudhary
-Bhanu
Thanks for the help!
this error is coming while using your code
Try testing AccountProcessor -> class and test class in new org with only @future or @future(callout = false) annotation
Hello Amit... I would like to know how I can set a specific ID in AccountProcessorTest... I tried
@IsTest
public class AccountProcessorTest {
public static testmethod void TestAccountProcessorTest() {
Test.startTest();
AccountProcessor.countContacts('0015000001I2JpXAAV');
Test.stopTest();
}
}
And got this error --> Method does not exist or incorrect signature: AccountProcessor.countContacts(String)
Thanks in advance
Please try this:
Create this class: Now create this Test class.
And this class to be called from the test class to create test Account with 3 Contacts (to be counted).
https://www.techgape.com/2019/03/best-comments-for-girl-pic-on-instagram.html
It has been a while where this has been closed with a solution as best answer below.
Question is, how do you call this anonymous using Open Execute Anonymous Window (Ctrl + E) in the developer console?
For example I want to execute this with Id (0016g000008zrVOAAY).
When I run anonymous: AccountProcessor.countContacts(0016g000008zrVOAAY);
I am getting error of: Unexpected Token '('.
Class used from Best Answer:
public class AccountProcessor { @future public static void countContacts(Set<id> setId) { List<Account> lstAccount = [select id,Number_of_Contacts__c , (select id from contacts ) from account where id in :setId ]; for( Account acc : lstAccount ) { List<Contact> lstCont = acc.contacts ; acc.Number_of_Contacts__c = lstCont.size(); } update lstAccount; } }
Thank you in advance.
https://developer.salesforce.com/forums/?id=9062I000000IE9QQAW
Basically assigning the id to a variable using Set<Id> then calling the class method using the idSet variable. This works for me:
Set<Id> idSet = new Set<Id>{'0016g000008zrVOAAY'};
AccountProcessor.countContacts(idSet);
AccountProcessorTest Class
Hi,
Test classI hope this will be not futile.
This is how I achieved 100% coverage: As I didn't actually need to add a Contact record in order to reach every line of code, I got by without having actually tested the functionality of the class.
The class itself was just as bad. As a developer, it's almost literally painful to see database queries and DML within a loop, but I was allowed to get away with it. Just here to provide an example of how "I passed" still can mean there's more to learn. I'm not perfect by any means, but this will not pass any effective code review.
rahul soni 20 (above) has a great example of how to achieve this without having DML and SOQL in a loop.
Tested the field update using Anonymous run.
Test Class used:
Anonymous code execution to validate the account updates:
Please use this as guidance only.
Do let me know if you have any issues.
@Amit Chaudhary 8 Thanks for the answer. It is very helpful. Just a thought, we can optimize the two lines:
List<Contact> lstCont = acc.contacts ; acc.Number_of_Contacts__c = lstCont.size();
as
acc.Number_of_Contacts__c = (acc.contacts).size();
I am new to this and would appreciate suggestions anyone has on optimizing code.
According to problem, We have to acheive lump sum 100% Test Coverage.
I've achieved 100% Test Coverage via doing this. Hope, This will help you too.
AccountProcessor Class.
Test ClassI've achieved 100% Test Coverage via doing this. Hope, This will help you too.
AccountProcessor Class. for electric griddles reviews (https://smokelesscooking.com/electric-griddles-reviews/)
Class of Account Processor. For review Click here (https://musikringtone.com/download/jack-sparrow-bgm-ringtone-download-2020/)
Kite Zerodha Login (https://prokeeda.com/zerodha-kite-zerodha-login-open-demat-trading-account/)
Create Android App [without coading] (https://prokeeda.com/how-to-create-android-apps-free-without-coding/)
Create Android Game (https://prokeeda.com/how-to-create-android-game-full-guide-step-by-step/)
Lucky Patcher IOS (https://prokeeda.com/lucky-patcher-for-ios/)
Dainik Bhaskar Quiz Answers Today Live Update (https://in.prokeeda.com/dainik-bhaskar-quiz-answers-today/)
E-Shram Card (https://in.prokeeda.com/e-shram-card-kya-hai/)
Upwork (https://in.prokeeda.com/upwork-kya-hai-upwork-se-paise-kaise-kamaye/)
Earn Money Online (https://in.prokeeda.com/ghar-baithe-paise-kaise-kamaye/)
Cryptocurrency (https://in.prokeeda.com/cryptocurrency-kya-hai/)
Class of Account Processor.
Class of Record Processor. For audit Click here (https://ringtonezip.com/download/jack-sparrow-bgm-ringtone/)