+ Start a Discussion
LearnerrrLearnerrr 

I have already fetch data in variables from API using http ,Now I want to add that fetched data in particular field of custom object ,How to pass that data from variables to object's field ??

v varaprasadv varaprasad
Hi Swetaben,

please check once below sample code : 

 
string xmldata = 'recevied from webservices';

object obj = new object();
obj.varaibldata = xmldata;
----
---

insert obj;

Please paste here sample data. So that we can help you.

 

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For  Support: varaprasad4sfdc@gmail.com


 
LearnerrrLearnerrr
Hi Varaprasad,

Thanks for your Help :) Can you please help me to build code for my below requirement.
Below is my webservice class and I want to call this class from custom button withot using VF page.Where Parameters to be entered:(triplog API -,Reimbursement id,Fuel,MPG,Mnt,trip Status)
global class GetTripCallout {
    public static String url = 'URL/api/trups';
    public static String changeURL = 'URL/api/tripStatus';
    public static HttpRequest req, putReq;
    public static HttpResponse res, putRes;
    public static Http h,putHTTP;
    public static TripLogJSONClass.TripLogJSONClassInner tripLog;
    public static String body, apiToken;
    public static Set<Integer> trip_IDs;

    
    
    webservice static Void GetTripCalloutForMileage(ID empReID, String contactEmail, String priMin, String priMax, String fuel, String mpg, String mnt, String apiToken, String tripStatus) {
        System.debug('inside webservice');
        String endPoint;
        List<Employee_Mileage__c> empList = new List<Employee_Mileage__c>();
        Set<Employee_Mileage__c> empSet = new Set<Employee_Mileage__c>();
        List<Employee_Mileage__c> result = new List<Employee_Mileage__c>();
        Employee_Mileage__c emp;
        try {
            h = new Http();
            req = new HttpRequest();
            endPoint = 'startDate='+priMin+'&userEmail='+contactEmail+'&endDate='+priMax+'&status='+tripStatus+'&activity=Business';
            req.setEndpoint(url+endPoint);
            System.debug('endpoint: '+url+endPoint);
            req.setMethod('GET');
            req.setTimeout(120000);
            req.setHeader('Authorization', 'apikey ' + apiToken);
            res = h.send(req);
            System.debug('res.getBody(): '+res.getBody());
            System.debug('res.getStatusCode(): '+res.getStatusCode());
            System.debug('res: '+res);
            if(res.getStatusCode() == 200) {
                System.debug('res.getBody(): '+res.getBody());
                tripLog = TripLogJSONClass.parse(res.getBody());
                List<TripLogJSONClass.Trips> listTrips = tripLog.trips;
                trip_IDs = new Set<Integer>();
                System.debug('listTrips.size(): '+listTrips.size());
                /**** Create Employee Mileage Record ****/
                for(TripLogJSONClass.Trips lt : listTrips) {
                    trip_IDs.add(lt.id);
                    emp = new Employee_Mileage__c();
                    emp.Name = 'Test ';
                    emp.Trip_Status__c = 'No Approved Yet';
                    emp.Trip_Id__c = lt.id;
                    emp.Mileage__c = lt.mileage;
                    if(emp.Mileage__c == null || emp.Mileage__c == 0) {
                        emp.Trip_Status__c = 'Approved';
                    }
                    emp.EmployeeReimbursement__c = empReID;
                    emp.Contact_Email_Id__c = contactEmail;
                    if(fuel != null && fuel != '') {
                        emp.Fuel_price__c = decimal.valueof(fuel);
                    } else {
                        emp.Fuel_price__c = null;
                    }
                    if(mpg != null && mpg != '') {
                        emp.MPG__c = decimal.valueof(mpg);
                    } else {
                        emp.MPG__c = null;
                    }
                    if(mntTires != null && mnt != '') {
                        emp.Maintenance_and_Tires__c = decimal.valueof(mnt);
                    } else {
                        emp.Maintenance_and_Tires__c = null;
                    }
                    if(lt.fromLocation != null && lt.fromLocation.display != null) {
                        emp.Trip_Origin__c = lt.fromLocation.display;
                    }
                    if(lt.toLocation != null && lt.toLocation.display != null) {
                        emp.Trip_Destination__c = lt.toLocation.display;
                    }
                    String myDate = lt.startTime;
                    String[] d = myDate.split('T');
                    emp.Trip_Date__c = Date.valueof(d[0]);
                    empList.add(emp);
                }
                System.debug('empList.size(): '+empList.size());
                if(!empList.isEmpty()) {
                    insert empList;
                }
            }
        } catch(System.CalloutException e) {
            System.debug('GET Error: '+e.getMessage());
        }
    }

    webservice static void putHTTP(String apiToken) {
        try {
            String part = '';
            putReq = new HttpRequest();
            putHTTP = new Http();
            putRes = new HttpResponse();
            body = '{"status":"Approved","ids":[';
            for (Integer i : trip_IDs) {
                part = part + String.valueof(i) +',';
            }
            part = part.removeEnd(',');
            body = body + part +']}';
            putReq.setEndpoint(changeURL);
            putReq.setMethod('PUT');     
            putReq.setBody(body);
            putReq.setTimeout(120000);
            putReq.setHeader('Authorization', 'apikey ' + apiToken);
            putRes = putHTTP.send(putReq);
            System.debug('putRes.getBody(): '+putRes.getBody());
        } catch(System.CalloutException er) {
            System.debug('PUT Error: '+er.getMessage());
        }
    }

	    
    
}
Thanks,
Sweta