+ Start a Discussion
Etienne Gaudry 5Etienne Gaudry 5 

API Salesforce Account

Hi there, 
I need to connect salesforce to everwin accounts on insert and update. I made the trigger. But I don't know if my method and my wrapper are correct. I don't have the API key yet but it should happen.
Thanks for your help and your advice :

public class EU01AccountApi {
    private string cKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
    private string cSecret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
    private string uName = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
    private string passwd = 'Password+SecurityToken';

    public class responseWrapper {
        public string id;
        public string access_token;
        public string instance_url;
    }
    public string getRequestToken() {
        string reqBody = 'grant_type=password&client_id=' + cKey + '&client_secret=' + cSecret + '&username=' + uName + '&password=' + passwd;
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setBody(reqBody);
        req.setMethod('POST');
        req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
        HttpResponse hresp = h.send(req);
        responseWrapper wResp = (responseWrapper) JSON.deserialize(hresp.getBody(), responseWrapper.class);
        system.debug('Instance url' + wResp.instance_url);
        system.debug('session id' + wResp.access_token);
        return wResp.access_token;
    }
    @future(callout = true)
    public static void getAccountInsert(Set < id > accIdSet) {
        String accToken;
        string responseBody;
        string endPoint = 'https://ap5.salesforce.com/services/apexrest/createAccountRecord';
        EU01AccountApi obj = new EU01AccountApi();
        accToken = obj.getRequestToken();
        system.debug('access token' + accToken);
        if (accToken != '') {
            for (Account acc: [SELECT id,ID_Everwin__c,website,phone,billingstreet,billingpostalcode,
                            billingcity,billingcountry,billingregion,SIRET__c,SIREN__c,Name,
                            OwnerId,ParentId,Recommandation_compte__c,Statut_juridique__c,Raison_sociale__c,
                            Accepte_de_recevoir_les_emails__c,Code_NAF__c,Ownership 
                            FROM account WHERE id in: accIdSet]) {
                system.debug('JSON' + JSON.serialize(acc));
                Http h1 = new Http();
                HttpRequest req1 = new HttpRequest();
                req1.setHeader('Authorization', 'Bearer ' + accToken);
                req1.setHeader('Content-Type', 'application/json');
                //req1.setHeader('accept','application/json');
                req1.setMethod('POST');
                req1.setBody(JSON.serialize(acc));
                req1.setEndpoint(endPoint);
                HttpResponse hresp1 = h1.send(req1);
                system.debug('hresp1' + hresp1);
            }
        }
    }
    @future(callout = true)
    public static void getAccountUpdate(set < id > accIdSet) {
        for (Account acct: [SELECT id,ID_Everwin__c,website,phone,billingstreet,billingpostalcode,
                            billingcity,billingcountry,billingregion,SIRET__c,SIREN__c,Name,
                            OwnerId,ParentId,Recommandation_compte__c,Statut_juridique__c,Raison_sociale__c,
                            Accepte_de_recevoir_les_emails__c,Code_NAF__c,Ownership 
                            FROM account WHERE id in: accIdSet]) {
            wrapperParameter resWrap=new wrapperParameter();
            resWrap.id=acct.ID_Everwin__c;
            resWrap.website=acct.website;
            resWrap.phone=acct.phone;
            resWrap.address=acct.billingstreet;
            resWrap.postalCode=acct.billingpostalcode;
            resWrap.city=acct.billingcity;
            resWrap.country=acct.billingcountry;
            resWrap.region=acct.billingregion;
            resWrap.identifierNumber=acct.SIRET__c;
            resWrap.companyRegistrationNumber=acct.SIREN__c;
            resWrap.name=acct.Name;
            resWrap.accountManager=acct.OwnerId;
            resWrap.group=acct.ParentId;
            resWrap.providedby=acct.Recommandation_compte__c;
            resWrap.status=acct.Statut_juridique__c;
            resWrap.code=acct.Raison_sociale__c;
            resWrap.acceptEmailing=acct.Accepte_de_recevoir_les_emails__c;
            resWrap.ape=acct.Code_NAF__c;
            resWrap.legalStatus=acct.Ownership;
            String accToken;
            string responseBody;
            string endPoint = 'https://ap5.salesforce.com/services/apexrest/updateopportunityrecord/';
            EU01AccountApi obj = new EU01AccountApi();
            accToken = obj.getRequestToken();
            system.debug('access token' + accToken);
            if (accToken != '') {
                Http h1 = new Http();
                HttpRequest req1 = new HttpRequest();
                req1.setHeader('Authorization', 'Bearer ' + accToken);
                req1.setHeader('Content-Type', 'application/json');
                req1.setBody(JSON.serialize(resWrap));
                req1.setMethod('PUT');
                req1.setEndpoint(endPoint);
                HttpResponse hresp1 = h1.send(req1);
                //listWrap=(list<resultWrapper>) JSON.deserialize(hresp1.getBody(),list<resultWrapper>.class);
            }
        }
    }
    public class wrapperParameter{
        //public class items {
        public Integer id;
        public String website;
        public String phone;
        public String address;
        public String postalCode;
        public String city;
        public country country;
        public region region;
        public String identifierNumber;
        public String companyRegistrationNumber;
        public String name;
        public accountManager accountManager;
        public String group;
        public providedby providedby;
        public String status;
        public String code;
        public Integer acceptEmailing;
        public ape ape;
        public legalStatus legalStatus;
    //}
    }
}