• Neela
  • NEWBIE
  • 30 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 1
    Likes Given
  • 3
    Questions
  • 1
    Replies
public class UpdateOpportunity {
    public static void descriptionUpdate() {
        List<Opportunity> opp = [SELECT  Id,Name,Description From Opportunity WHERE StageName='Closed Won'limit 5];
        System.debug(opp);
        for ( Opportunity opps :opp)
        { 
            if(opps.Description == Null){
                opps.Description = 'This Opportunity is converted into Account';
            }
            else if(opps.Description == ''){
                opps.Description = 'This Opportunity is converted into Account';
            }
        }
              Update opp;
    }
}
  • July 29, 2021
  • Like
  • 0
CLASS:
public class Currencyconverter {
    public static decimal CurrencyconverterfromEUROtoINR(decimal eur){
        decimal inr = 0.0;
        HTTP http = new HTTP();
        HTTPRequest req= new HTTPRequest();
        req.setEndpoint('https://api.exchangeratesapi.io/latest');
        req.setMethod('GET');
        HTTPResponse res = http.send(req);
        Map <string,object> Jsonbody=(Map <string,object>)Json.deserializeUntyped(res.getBody());
        system.debug(Jsonbody);
        Map <string,object> rates=(Map<string,object>)Jsonbody.get('rates');
        decimal conversionrate= (decimal)rates.get('INR');                    
        inr = eur * conversionrate;      
        return inr;
    
}
}

Testing at anonymous window:
decimal conversionrate = Currencyconverter.CurrencyconverterfromEUROtoINR(150);
system.debug('conversionrate'+conversionrate);

ERROR:
System.NullPointerException: Attempt to de-reference a null object
  • July 22, 2021
  • Like
  • 0
The error in test class is "Illegal assignment from System.PageReference to System.HttpResponse".  PLEASE SOMEONE HELP WITH THIS IN GETTI NG MAX CODE COVERAGE.
====================================================
My controller class code:


public class vfClass {
    public String InputLabel{get;set;}
    public decimal GeoPosition{get;set;}
    public decimal GeoPosition2{get;set;}
    public PageReference makeCallout(){
        if(InputLabel==NULL || InputLabel=='' ){
            //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please Enter a Location'));
        }
        else{
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            String apikey = 'zrAR6DRKkoC4gRoku7QhnIiSjUxPNLMO';
            String endPoint = 'http://dataservice.accuweather.com/locations/v1/cities/search?q='+InputLabel+'&apikey='+apikey;
            if(Test.isRunningTest()) {
            endPoint = 'http://dataservice.accuweather.com/locations/v1/cities/search?q=Delhi&apikey=dummy';    
            }
            request.setEndpoint(endPoint);
            request.setMethod('GET');
            HttpResponse response = http.send(request);
            System.debug('response status-->'+response.getStatusCode());
            System.debug('response -->'+response);
            if (response.getStatusCode() == 200) {
                System.debug('body-->'+response.getBody());
                Object resp = JSON.deserializeUntyped(response.getBody());
                System.debug('resp-->'+resp);
                List<Object> theJsonList = (List<Object>) resp;
                System.debug('theJsonList-->'+theJsonList);
                List<Map<String, Object>> theJsonMapList = new List<Map<String, Object>>();
                
                for (Object obj : theJsonList) {
                    theJsonMapList.add((Map<String, Object>) obj);
                }
                System.debug('theJsonMapList-->'+theJsonMapList[0].get('GeoPosition'));
                Map<String,Object> GeoPositionMap = (Map<String,Object>)theJsonMapList[0].get('GeoPosition');
                System.debug('GeoPositionMap->'+GeoPositionMap.get('Latitude'));
               GeoPosition = (decimal)GeoPositionMap.get('Latitude');
               GeoPosition2 =(decimal)GeoPositionMap.get('Longitude');
                
            }
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Searching For GeoPositions'));
        }
        
        return null;
    }
}
=====================================================
Mock test code:
@isTest
global class vfnmock implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest request) {
         HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{GeoPosition": {"Latitude": 28.643,"Longitude": 77.118}}');
        response.setStatusCode(200);
        return response;
    }
}
====================================================
Test Class (that I tried):

@isTest
private class TestLocations {
     @isTest static void testCallout() {

        Test.setMock(HttpCalloutMock.class, new vfnmock());
        vfClass vfinst = new vfClass();
        HttpResponse res = vfinst.makeCallout();
        
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"example":"test"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());
    }
}

The error in test class is "Illegal assignment from System.PageReference to System.HttpResponse".  PLEASE SOMEONE HELP WITH THIS IN GETTI NG MAX CODE COVERAGE.
  • May 24, 2021
  • Like
  • 0
CLASS:
public class Currencyconverter {
    public static decimal CurrencyconverterfromEUROtoINR(decimal eur){
        decimal inr = 0.0;
        HTTP http = new HTTP();
        HTTPRequest req= new HTTPRequest();
        req.setEndpoint('https://api.exchangeratesapi.io/latest');
        req.setMethod('GET');
        HTTPResponse res = http.send(req);
        Map <string,object> Jsonbody=(Map <string,object>)Json.deserializeUntyped(res.getBody());
        system.debug(Jsonbody);
        Map <string,object> rates=(Map<string,object>)Jsonbody.get('rates');
        decimal conversionrate= (decimal)rates.get('INR');                    
        inr = eur * conversionrate;      
        return inr;
    
}
}

Testing at anonymous window:
decimal conversionrate = Currencyconverter.CurrencyconverterfromEUROtoINR(150);
system.debug('conversionrate'+conversionrate);

ERROR:
System.NullPointerException: Attempt to de-reference a null object
  • July 22, 2021
  • Like
  • 0