function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kiran ChodavadiyaKiran Chodavadiya 

Update Field After verify with API Callout Response

Hello,
I have this Requirement
Make Call-out to SmartyStreets API –

SmartyStreets API is used to verify the address. In this milestone you need to consume https://smartystreets.com/products/apis/us-street-api

Requirement – The business ask is to verify the address of the event. Use the above API to verify the address & update “Location Verified” field on Event.

Must Have – Use of Error Handling and code re-usability

I tried with following code< but it showing 404 Error.
Could you please help how to come out from this Error And How i Upadate Field.
 
public class ApexHourIntigration {
    public static void verifyAddress(String Address){
        
        Http Http = new Http();
        HttpRequest Request = new HttpRequest();
        Request.setEndpoint('https://us-street.api.smartystreets.com/street-address?auth-id=6baff965-c2ed-eb53-7f43-e615db469a24&auth-token=pq7dRFqRoxtHiXKtCsOt'+ Address);
        Request.setMethod('GET');
        //Request.setHeader('contentType', 'application-json;charset=UTF-8');
        //Request.setBody('[{"candidates":10,"match":"invalid","street":"3901 SW 154th Ave","street2":"","city":"Davie","state":"FL","zipcode":"33331"}]');
        HttpResponse Response = Http.send(Request);
        
        if(Response.getStatusCode()!=200){
            system.debug(Response.getBody());
        }
        
        if(Response.getStatusCode()==200){
            Map<string,object> Results = (Map<string,object>) JSON.deserializeUntyped(Response.getBody());
            
            List<object> items = (List<object>) Results.get('items');
            for(object item :items){
                Map<string,object> components = (Map<string,object>)item;
                system.debug(components.get('components'));
           }
            try{
            	Event e = new Event();
                e.Location = 'Verified';
                update e;
           }
            catch(Exception e){
                system.debug('Error is :'+e.getMessage());
           }
        }
    }
}



 
SwethaSwetha (Salesforce Developers) 
HI Kiran,
Can you provide me the complete endpoint that includes a sample address? Thanks
Kiran ChodavadiyaKiran Chodavadiya
Hi Swetha,
Thanks for Responsing.
https://us-street.api.smartystreets.com/street-address?auth-id=6baff965-c2ed-eb53-7f43-e615db469a24&auth-token=pq7dRFqRoxtHiXKtCsOt&candidates=10&match=invalid&street=3901%20SW%20154th%20Ave&street2=&city=Davie&state=FL&zipcode=33331

 
Footprints on the MoonFootprints on the Moon
Hi Kiran,

I ran your same code, with Address argument as - '&candidates=10&match=invalid&street=3901%20SW%20154th%20Ave&street2=&city=Davie&state=FL&zipcode=33331'

It is not giving 404 Error. Check if you are passing the argument correctly.

Also, in rest of the code, you are receiving Response.getBody() as Map<string,object>, but it is List<Object>
Please refer blow snippet-
//Map<string,object> Results = (Map<string,object>) JSON.deserializeUntyped(Response.getBody());
            
List<object> Results = (List<object>) JSON.deserializeUntyped(Response.getBody());
            
//List<object> items = (List<object>) Results.get('items');
            
Map<string,object> Components;    
            
for(object item :Results){
        Components = (Map<string,object>)item;
        system.debug('COMPONENTS- '+ components.get('components'));
}
Let me know if this helps.
SwethaSwetha (Salesforce Developers) 
HI Kiran,
I have run narrowed snippet in anonymous block to see if its an issue with end point 
Http Http = new Http();
HttpRequest Request = new HttpRequest();
Request.setEndpoint('https://us-street.api.smartystreets.com/street-address?auth-id=6baff965-c2ed-eb53-7f43-e615db469a24&auth-token=pq7dRFqRoxtHiXKtCsOt&candidates=10&match=invalid&street=3901%20SW%20154th%20Ave&street2=&city=Davie&state=FL&zipcode=33331');
 Request.setMethod('GET');
HttpResponse Response = Http.send(Request);
system.debug(Response.getBody());
The endpoint works fine.

Running your class gave
"EXCEPTION: System.TypeException: Invalid conversion from runtime type List<ANY> to Map<String,ANY>
STACKTRACE: Class.ApexHourIntigration.verifyAddress: line 17, column 1"

To fix the error, please try the approach suggested in the above comment. Thanks
Kiran ChodavadiyaKiran Chodavadiya
Hi
Footprints on the Moon ,
Swetha,

Thanks for Help.
Your Solution work for me.

I have Developed following code with suggested solution and it works fine.
public class ApexHourIntigration {
    public static void verifyAddress(String Address){
        
        Http Http = new Http();
        HttpRequest Request = new HttpRequest();
        Request.setEndpoint('https://us-street.api.smartystreets.com/street-address?auth-id=6baff965-c2ed-eb53-7f43-e615db469a24&auth-token=pq7dRFqRoxtHiXKtCsOt'+ Address);
        Request.setMethod('GET');
        HttpResponse Response = Http.send(Request);
        
        if(Response.getStatusCode()!=200){
            system.debug(Response.getBody());
        }
        
        if(Response.getStatusCode()==200){
            List<object> results = (List<object>) JSON.deserializeUntyped(Response.getBody());
            Map<string,object> mapComponents = (Map<string,object>)results[0];
            Map<string,object> components = (Map<string,object>)mapComponents.get('components');
            for(string s :components.keyset()){
                if(components.get(s)==Address){
                    try{
                        Event e = new Event();
                        e.Location=Address;
                        e.DurationInMinutes=20;
                        e.ActivityDateTime=system.now();
                        insert e;
                    }
                    catch (Exception ex){
                        system.debug('Error'+ex.getMessage());
                    }
                }      
            }
        }
    }
}