• Prashant Maxsteel
  • NEWBIE
  • 10 Points
  • Member since 2017
  • Senior Siebel Analyst Programmer
  • Accenture Services Pvt Ltd


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 5
    Replies
Hi
I have been trying to pass this challenge (https://trailhead.salesforce.com/en/trails/force_com_dev_intermediate/modules/apex_integration_services/units/apex_integration_webservices):

To pass this challenge, create an Apex REST class that is accessible at '/Accounts/<Account_ID>/contacts'. The service will return the account's ID and Name plus the ID and Name of all contacts associated with the account. Write unit tests that achieve 100% code coverage for the class and run your Apex tests.
The Apex class must be called 'AccountManager'.
The Apex class must have a method called 'getAccount' that is annotated with @HttpGet
The method must return the ID and Name for the requested record and all associated contacts with their ID and Name.
The unit tests must be in a separate Apex class called 'AccountManagerTest'.
The unit tests must cover all lines of code included in the AccountManager 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 getting this error:

Challenge Not yet complete... here's what's wrong: 
Executing the 'AccountManager' method failed. Either the service isn't configured with the correct urlMapping, is not global, does not have the proper method name or does not return the requested account and all of its contacts.

This is my code:
 
@RestResource(urlMapping='/Accounts/*/contacts')
global with sharing class AccountManager {
	
    @HttpGet
    global static List<sObject> getAccount(){
        RestRequest request = RestContext.request;
        List<String> urlParams = request.requestURI.split('/');
        
        String accountId;	
        for(String param: urlParams){
            if((param != 'Accounts') && (param != 'contacts'))
                accountId = param;
        }
    
        List<Account> lstAccounts = new List<Account>();
        lstAccounts = [SELECT Id, Name FROM Account WHERE Id = :accountId];
        
        List<sObject> result = new List<sObject>();
        result.add(lstAccounts[0]);
        
        List<Contact> lstContacts = [SELECT Id, Name FROM Contact WHERE AccountId = :accountId];
        for(Contact c:lstContacts){
            result.add(c);
        }
        return result;
    }
}

This is my response
 
[ {
  "attributes" : {
    "type" : "Account",
    "url" : "/services/data/v39.0/sobjects/Account/001o0000015hgVBAAY"
  },
  "Id" : "001o0000015hgVBAAY",
  "Name" : "SFDC Computing"
}, {
  "attributes" : {
    "type" : "Contact",
    "url" : "/services/data/v39.0/sobjects/Contact/003o000001DbYc8AAF"
  },
  "Id" : "003o000001DbYc8AAF",
  "Name" : "Pepito Peres"
}, {
  "attributes" : {
    "type" : "Contact",
    "url" : "/services/data/v39.0/sobjects/Contact/003o000001BsUPwAAN"
  },
  "Id" : "003o000001BsUPwAAN",
  "Name" : "Last0"
} ]

Can someone light my way?​
Hi all, 

I have a problem with this challenge :

Create a Queueable Apex class that inserts the same Contact for each Account for a specific state. Write unit tests that achieve 100% code coverage for the class.
Create an Apex class called 'AddPrimaryContact' that implements the Queueable interface.
Create a constructor for the class that accepts as its first argument a Contact sObject and a second argument as a string for the State abbreviation.
The execute method must query for a maximum of 200 Accounts with the BillingState specified by the State abbreviation passed into the constructor and insert the Contact sObject record associated to each Account. Look at the sObject clone() method.
Create an Apex test class called 'AddPrimaryContactTest'.
In the test class, insert 50 Account records for BillingState "NY" and 50 Account records for BillingState "CA". Create an instance of the AddPrimaryContact class, enqueue the job and assert that a Contact record was inserted for each of the 50 Accounts with the BillingState of "CA".
The unit tests must cover all lines of code included in the AddPrimaryContact 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 haven't 100% for my test class. 
 
@isTest
 public class AddPrimaryContactTest {
   
   
     @isTest static void TestList(){
         List<Account> Teste = new List <Account>();
         for(Integer i=0;i<50;i++){
             
             Teste.add(new Account(BillingState = 'CA', name = 'Test'+i));
         }
             for(Integer j=0;j<50;j++){
             
             Teste.add(new Account(BillingState = 'NY', name = 'Test'+j));
         
         }
         insert Teste;
         Contact co = new Contact();
          String state = 'CA';
     AddPrimaryContact apc = new AddPrimaryContact(co, state);
	Test.startTest();
	System.enqueueJob(apc);
     Test.stopTest();
         for (Account t:Teste){
             if( t.BillingState == 'CA'){
               	  
             	System.assertEquals(1, t.Number_of_contacts__c);
            
             
         }
         }      
}
 }
 
public class AddPrimaryContact implements Queueable{
    private Contact c;
    private String state;
    public  AddPrimaryContact(Contact c, String state){
        this.c = c;
        this.state = state;
        
    }
     public void execute(QueueableContext context) {
        List<Account> ListAccount = [SELECT ID, Name FROM ACCOUNT WHERE BillingState = :state LIMIT 200];
         for (Account l:ListAccount){
             for (Contact co:l.Contacts){
                 
                 c = co.clone(false,false,false,false);
                 insert c;
             }
                 
                 
             }
             
         }

}


Anyone can help me please?
Thanks!
Regarding using SOQL to assign and retrieve attributes, I need identify the Postal Code of the Contact and I do not know how to name it?

Imagine I am having the following code:

Contact[] cts = [SELECT Contact.LastName, Contact.Postal_Code FROM Contact WHERE LastName =: sName OR PostalCode =: sPostalCode]; 
  
        System.debug('Last Name forund + cts[0].LastName);
        System.debug('PostalCode found' + cts[0].PostalCode);

The problem is that Contact.PostalCode can't be found and I do not know how to name it. 

Anyone can help me?

Thanks!

Bea
 
Hi,

I have a account list (aList), i need contact ids list from the account list. How to do it. 
Here i am getting 10 accounts, suppose if each account having 10 contacts than i want all the 100 contact ids in one list.
How to write the loop for this.Below is the code where i am getting all the 10 accounts and 100 contacts in one list.


String baseQuery = 'Select Id,Name,(select Id,Name from Contacts Limit 10) from Account ';
       
String finalQuery = baseQuery +' where Id IN (select AccountId from Contact) Order By Name limit 10';

List<Account> aList = Database.Query(finalQuery);

Please give me suggestions how to get contacts ids list.

Thanks,
Bujji

  • September 17, 2014
  • Like
  • 0