• Janakiraman Chandravadhan
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 2
    Questions
  • 2
    Replies
I am facing error in the below code, basically I am trying to perform a Rest API authentication and the code doesn't seem to work. it is throwing a specific error 'Invalid type: OAuth2'. Org B has this code and Org A has the connected App and remote site settings updated.

Any help would be greatly appreciated. Thanks!

below is my code...
public class Samplerest
{
    public string name;

String clientId = 'Client Id' ;
String clientSecret = 'client Secret';
String username= 'username';
String password= 'pwd';

String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='
+clientSecret+'&username='+username+'&password='+password;

Http h = new Http();
    {   
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndpoint('https://playful-wolf-sa1kgu-dev-ed.my.salesforce.com/services/oauth2/token');
HttpResponse res = h.send(req);
        
OAuth2 objAuthenticationInfo =(OAuth2)JSON.deserialize(res.getbody(), OAuth2.class);
  }    

  // proceed further only if you get token
if(objAuthenticationInfo.access_token!=null)
{

Http h1 = new Http();
HttpRequest req1 = new HttpRequest();
string EndPt = 'https://playful-wolf-sa1kgu-dev-ed.my.salesforce.com/services/apexrest/AccountRestService?name=' +name;

//request header to send token, setMethod, and set Endpoint
req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
req1.setMethod('GET');
req1.setEndpoint(EndPt);

HttpResponse res1 = h1.send(req1);
system.debug('RESPONSE_BODY'+res1y());
}
}

 
Due to the Data storage limits in our Salesforce Instance, we have created a big object with 3 Indexed fields. Now, when I use this Big object in an apex class to search for a particular value from the Big object records, I had to include all 3 indexed fields in the WHERE Clause due to Big Object SOQL Limitation. As per the existing Apex class logic, we should be able to search even with one parameter. Please help me to find a way to bypass Big object SOQL Limitation without changing the current Apex logic?

If I execute the below Apex class as is, I am getting an error: "Filters may not have any gaps within the composite key" in the Workbench Rest API explorer
 
@RestResource(urlMapping='/locationservice')
global class LocationService 
{
    @HttpGet
    global static void doGet() 
    {
        //variable to collect the params from apexrest url
        string Zcode = RestContext.request.params.get('Zipcode');
        string ste  = RestContext.request.params.get('State');
        string city = RestContext.request.params.get('City');
        string mlrange = RestContext.request.params.get('milerange');
        string lat;
        string lon;
{
if(ste!=null)
    {
list<GeoCoordinate__b> lstPostalCodeMDT = [select Latitude__c, Longitude__c from GeoCoordinate__b where State__c= :ste];//soql to query Geocoordinate Big Object
        if(!lstPostalCodeMDT.isempty())
        {
           lat = lstPostalCodeMDT.get(0).Latitude__c;
           lon = lstPostalCodeMDT.get(0).Longitude__c;
        }
    }
if(city!=null)
    {
list<GeoCoordinate__b> lstPostalCodeMDT = [select Latitude__c, Longitude__c from GeoCoordinate__b where City__c = :city];//soql to query Geocoordinate Big Object
        if(!lstPostalCodeMDT.isempty())
        {
           lat =  lstPostalCodeMDT.get(0).Latitude__c;
           lon = lstPostalCodeMDT.get(0).Longitude__c;
        }
    }
if(Zcode!=null)
    {
        list<GeoCoordinate__b> lstPostalCodeMDT = [select Latitude__c, Longitude__c from GeoCoordinate__b where Zip_code__c = :Zcode];//soql to query Geocoordinate Big Object
        if(!lstPostalCodeMDT.isempty())
        {
           lat = lstPostalCodeMDT.get(0).Latitude__c;
           lon = lstPostalCodeMDT.get(0).Longitude__c;
        }
    }
}
decimal lt = decimal.valueOf(lat);
decimal lg = decimal.valueOf(lon);
decimal mlran = decimal.valueOf(mlrange);

//SOQL to fetch the dealer data from Account object
list<account> locationList = [SELECT Id, Name, Phone, ShippingStreet, ShippingCity, ShippingState, ShippingCountry, ShippingLatitude, ShippingLongitude, ShippingPostalCode, Website, distance(ShippingAddress, geolocation (:lt, :lg), 'mi') distance FROM Account WHERE distance(ShippingAddress, geolocation (:lt, :lg),'mi') < :mlran ORDER BY distance(ShippingAddress, geolocation (:lt, :lg), 'mi') LIMIT 20];

//pass the results in JSON format     
String locationJSON = JSON.serialize(locationList);
RestContext.response.addHeader('Content-Type', 'application/json');
RestContext.response.responseBody = Blob.valueOf(locationJSON);    
    }
}

 
Due to the Data storage limits in our Salesforce Instance, we have created a big object with 3 Indexed fields. Now, when I use this Big object in an apex class to search for a particular value from the Big object records, I had to include all 3 indexed fields in the WHERE Clause due to Big Object SOQL Limitation. As per the existing Apex class logic, we should be able to search even with one parameter. Please help me to find a way to bypass Big object SOQL Limitation without changing the current Apex logic?

If I execute the below Apex class as is, I am getting an error: "Filters may not have any gaps within the composite key" in the Workbench Rest API explorer
 
@RestResource(urlMapping='/locationservice')
global class LocationService 
{
    @HttpGet
    global static void doGet() 
    {
        //variable to collect the params from apexrest url
        string Zcode = RestContext.request.params.get('Zipcode');
        string ste  = RestContext.request.params.get('State');
        string city = RestContext.request.params.get('City');
        string mlrange = RestContext.request.params.get('milerange');
        string lat;
        string lon;
{
if(ste!=null)
    {
list<GeoCoordinate__b> lstPostalCodeMDT = [select Latitude__c, Longitude__c from GeoCoordinate__b where State__c= :ste];//soql to query Geocoordinate Big Object
        if(!lstPostalCodeMDT.isempty())
        {
           lat = lstPostalCodeMDT.get(0).Latitude__c;
           lon = lstPostalCodeMDT.get(0).Longitude__c;
        }
    }
if(city!=null)
    {
list<GeoCoordinate__b> lstPostalCodeMDT = [select Latitude__c, Longitude__c from GeoCoordinate__b where City__c = :city];//soql to query Geocoordinate Big Object
        if(!lstPostalCodeMDT.isempty())
        {
           lat =  lstPostalCodeMDT.get(0).Latitude__c;
           lon = lstPostalCodeMDT.get(0).Longitude__c;
        }
    }
if(Zcode!=null)
    {
        list<GeoCoordinate__b> lstPostalCodeMDT = [select Latitude__c, Longitude__c from GeoCoordinate__b where Zip_code__c = :Zcode];//soql to query Geocoordinate Big Object
        if(!lstPostalCodeMDT.isempty())
        {
           lat = lstPostalCodeMDT.get(0).Latitude__c;
           lon = lstPostalCodeMDT.get(0).Longitude__c;
        }
    }
}
decimal lt = decimal.valueOf(lat);
decimal lg = decimal.valueOf(lon);
decimal mlran = decimal.valueOf(mlrange);

//SOQL to fetch the dealer data from Account object
list<account> locationList = [SELECT Id, Name, Phone, ShippingStreet, ShippingCity, ShippingState, ShippingCountry, ShippingLatitude, ShippingLongitude, ShippingPostalCode, Website, distance(ShippingAddress, geolocation (:lt, :lg), 'mi') distance FROM Account WHERE distance(ShippingAddress, geolocation (:lt, :lg),'mi') < :mlran ORDER BY distance(ShippingAddress, geolocation (:lt, :lg), 'mi') LIMIT 20];

//pass the results in JSON format     
String locationJSON = JSON.serialize(locationList);
RestContext.response.addHeader('Content-Type', 'application/json');
RestContext.response.responseBody = Blob.valueOf(locationJSON);    
    }
}

 
I've completed the challenge, it has 100% coverage. I've checked all the method names. The URL is valid. I've used Work Bench and curl to test and even tested with multiple Accounts with and without contacts.

I know on other challenges, punctionation was important.  What about the defination of the return? Are there expected names?

I built a class to hold Account ID & Name along with a List of Contact names and IDs. Is this my issue?  Anyone else have a challenge with this challenge?


Any help or hints will be appreciated.

Here are snippets of my code:

@RestResource(urlMapping='/Accounts/*/contacts')
global with sharing class AccountManager {

....

global class APIAccount {
        public ID Id;
        public String Name;
        List<APIContact> Contacts;

...

@HttpGet
    global static APIAccount getAccount() {
        RestRequest request = RestContext.request;
...