• vamsi garapati
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 6
    Replies
trigger Emailnotification on Student__c (after insert) {

  List<Messaging.SingleEmailMessage> mails = new list<Messaging.SingleEmailMessage>();
  
  for (Student__c stu : Trigger.new) {
      
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      List<String> sendTo = new List<String>();
      sendTo.add(stu.Email__c);
      mail.setToAddresses(sendTo);
      mail.setSenderDisplayName('salesforceAdmin');      
      mail.setSubject('Welcome Aboard');
      String body = 'Dear ' + stu.Name__c + ', \n';
      body += 'Thankyou for Choosing sfdc.';
      body += 'You selected '+ stu.Trainer__r.Name+' as your trainer' ;
      mail.setHtmlBody(body);
      mails.add(mail);
    }
  
  Messaging.sendEmail(mails);
}
I am trying to fetch all student records whose name starts with the letter given by the user
trigger Emailnotification on Student__c (after insert) {

  List<Messaging.SingleEmailMessage> mails = new list<Messaging.SingleEmailMessage>();
  
  for (Student__c stu : Trigger.new) {
      
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      List<String> sendTo = new List<String>();
      sendTo.add(stu.Email__c);
      mail.setToAddresses(sendTo);
      mail.setSenderDisplayName('salesforceAdmin');      
      mail.setSubject('Welcome Aboard');
      String body = 'Dear ' + stu.Name__c + ', \n';
      body += 'Thankyou for Choosing sfdc.';
      body += 'You selected '+ stu.Trainer__r.Name+' as your trainer' ;
      mail.setHtmlBody(body);
      mails.add(mail);
    }
  
  Messaging.sendEmail(mails);
}
Hi,

I have written the below classes as part of the trailhead challenge for Apex REST callouts.

The class -

public class AnimalLocator {
  
  public static String getAnimalNameById(Integer id) {
    
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+id);
    request.setMethod('GET');
    
    HttpResponse response = http.send(request);
    List<Object> animals; 
    String returnValue; 
    
    // parse the JSON response
    if (response.getStatusCode() == 200) {
      Map<String, Object> result = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
      animals = (List<Object>) result.get('animals');
      System.debug(animals);
    }
    
    if (animals.size() > 0 && animals != NULL && id < animals.size()) {
      returnValue = (String) animals.get(id);
    }
    
    return returnValue;
  } 
    
}

Mock Response Class - 

@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
     // Implement this interface method
    global HTTPResponse respond(HTTPRequest request) {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"animals": ["majestic badger", "fluffy bunny", "scary bear", "chicken", "mighty moose"]}');
        response.setStatusCode(200);
        return response; 
    }
}

Test Class - 

@isTest
private class AnimalLocatorTest{
    @isTest static void AnimalLocatorMock1() {
        Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
        string result = AnimalLocator.getAnimalNameById(3);
        String expectedResult = 'chicken';
        System.assertEquals(result,expectedResult );
    }
}

I have got 100% code coverage for the main class. But when I check the challenge I get 

Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.NullPointerException: Attempt to de-reference a null object

Please tell me if any changes to the code is required. It's really frustrating as I am stuck from past 3-4 days with this unit.

Thanks & Regards,

Abhiram Sheshadri