Salesforce: How to configure to integrate with iPro

Modified on Wed, 11 May, 2022 at 2:18 PM


1/ Create a new remote site settings called 'iProIntegration'


i. Navigate too Setup > Security > Remote Site Settings


ii. Click the button 'New Remote Site'


Remote site url = https://www.clientdomain.com



2/ Create a new Custom Setting, to configure unique API access to client site


i. Navigate too Setup > Custom Code > Custom Settings


ii. Click the button 'New'


iii. Add the following fields to the custom settings object




iv. Enter the client specific API connection details



3/ Create the following Apex Classes


i. Navigate to Setup > Custom Code > Apex Classes


ii. Click the button 'New'


iii. Copy this code into the code window, save the file name as 'IproCreateContactBatch'


global with sharing class IproCreateContactBatch implements Database.Batchable<sobject>,Database.AllowsCallouts,Database.Stateful
{
    List<String>  conIdList ;
    List<Contact> allContactsForUpdate;

    global IproCreateContactBatch(List<Id> conIdList){
        this.conIdList = conIdList;
        this.allContactsForUpdate = new List<Contact>();
    }
    global IproCreateContactBatch(){
        this.allContactsForUpdate = new List<Contact>();
    }

    global Database.QueryLocator start(Database.BatchableContext bc){

        String query = 'SELECT Id, Salutation, FirstName, LastName, Phone, MobilePhone, Email, Mailing_Address_Country__c,';
        query += 'MailingStreet , MailingCity, MailingState, Mailing_Address_Postcode__c, Description FROM Contact WHERE ';

        if(conIdList != null){
            query += 'Id IN: conIdList';
        }
        return Database.getQueryLocator(query);
    }


    global void execute(Database.BatchableContext BC, List <Contact> scope) {

        IproUtility.OauthWrapper accessresponse = IproUtility.authorizeAPI();

        if(accessresponse != null)
        {
            String accessToken = accessresponse.access_token;

            for(Contact conRec : scope)
            {

                String body = '';
                /*if (conRec.Id != null){
                    body = 'Title=Mr(s)&FirstName='+conRec.FirstName+'&LastName='+conRec.LastName+'&Email='+conRec.Email+
                    '&EmailAlt=test1%40gmail.com&EmailAlt1=test2%40gmail.com&Telephone='+conRec.Phone+'&TelephoneAlt=123456&Mobile='+conRec.MobilePhone+
                    '&Postcode=1234AA&Address='+conRec.MailingStreet+'&StreetName=Address2&TownCity=City&CountyArea=CountyArea&CountryCode=GB&CompanyName=CompanyName&Comments=Comments&TypeId=2&DoNotMail=True&DoNotEmail=True&DoNotPhone=True&OnEmailList=True&Commision=10&Balance=10&Retainer=10&BrandId=1';

                }*/

                for(String str : IproUtility.ContactsFieldMap.keySet())
                {
                        if(conRec.get(str) != null)
                        {
                            if(String.isBlank(body))
                            {
                                body += IproUtility.ContactsFieldMap.get(str) + '=' +conRec.get(str);

                                List<String> splitName = new List<String>();
                                List<Contact> conList = [SELECT Id,Name FROM Contact WHERE Id =: conRec.Id];

                                if(!conList.isEmpty())
                                {
                                    splitName = conList[0].Name.split(' ');
                                }

                                if(!splitName.isEmpty() && splitName.size() > 1)
                                {
                                    body += '&FirstName=' + splitName[0] + '&LastName=' + splitName[1] ;
                                }
                                else
                                {
                                    body += '&FirstName=' + splitName[0] + '&LastName=';
                                }

                                body +='&TypeId=' + 'Travel Agent';

                            }
                        }
                        else
                        {
                            if(str == 'Brand__c')
                            {
                                String typeKey =(String)conRec.get(str);
                                body += '&BrandId=' + IproUtility.BrandId.get(typeKey);
                            }
                            else
                            {
                                body += '&' + IproUtility.ContactsFieldMap.get(str) + '=' + conRec.get(str);
                            }
                        }
                        body += '&sendemail=false';

                        HttpResponse response = IproUtility.postRequestAPI('/apis/contacts', accessToken, body);
                        allContactsForUpdate.add(conRec);
                        }
                }
        }
    }

    global void finish(Database.BatchableContext BC) {
    //    update allContactsForUpdate;
    }

}







Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article