//3.2.16 @RestResource(urlMapping='/RainbowConnector/*') global class RainbowConnector { webService static String getUserEmail() { return UserInfo.getUserEmail(); } webService static String getMainURL(){ return ApexPages.currentPage().getUrl(); } @HttpPost webService static String formatPhoneNumberwithoutspace(String phoneNumber) { String formatPhoneNumber = phoneNumber; if (phoneNumber.startsWith('+33')) { formatPhoneNumber = '0' +phoneNumber.mid(3,phoneNumber.length()-4); } return formatPhoneNumber; } webService static String formatPhoneNumberwithoutplus(String phoneNumber) { String formatPhoneNumber = phoneNumber; if (phoneNumber.startsWith('+33')) { formatPhoneNumber = phoneNumber.mid(1,phoneNumber.length()-1); } return formatPhoneNumber; } webService static String formatPhoneNumberwithplus(String phoneNumber) { if (phoneNumber.startsWith('33')) { return '+' + phoneNumber; // Fix the issue with the concatenation } return phoneNumber; } webService static String formatPhoneNumberwithoutplusspace(String phoneNumber) { String formatPhoneNumber = phoneNumber; if (phoneNumber.startsWith('33')) { formatPhoneNumber = '0' + phoneNumber.mid(2,phoneNumber.length()-1); } return formatPhoneNumber; } webService static String formatPhoneNumber(String phoneNumber) { String formatPhoneNumber = phoneNumber; if (phoneNumber.startsWith('+33')) { formatPhoneNumber = '0' +phoneNumber.mid(3,1) +' '+ phoneNumber.mid(4,2) +' '+ phoneNumber.mid(6,2)+ ' ' +phoneNumber.mid(8,2)+ ' ' +phoneNumber.mid(10,2); } return formatPhoneNumber; } webService static String afterfirst(String phoneNumber) { String formatPhoneNumber = phoneNumber.mid(0,2)+'-'+phoneNumber.mid(2,phoneNumber.length()-1); return formatPhoneNumber; } webService static String aftersecond(String phoneNumber) { String formatPhoneNumber = phoneNumber.mid(0,3)+'-'+phoneNumber.mid(3,phoneNumber.length()-1); return formatPhoneNumber; } webService static String afterthird(String phoneNumber) { String formatPhoneNumber = phoneNumber.mid(0,4)+'-'+phoneNumber.mid(4,phoneNumber.length()-1); return formatPhoneNumber; } webService static String getCasesByPhoneNumber(String phoneNumber) { String formatedPhoneNumber = formatPhoneNumber(phoneNumber); String formatedphonewithoutspace=formatPhoneNumberwithoutspace(phoneNumber); string afterFirst=afterfirst(phoneNumber); string aftersecond=aftersecond(phoneNumber); string afterthird=afterthird(phoneNumber); List < SObject > objectList = new List(); if(phoneNumber.length() > 5){ for (Case objcase: [SELECT SuppliedName, SuppliedPhone,ContactEmail,Subject FROM Case WHERE SuppliedPhone LIKE : ('%' + formatedPhoneNumber + '%') OR SuppliedPhone LIKE : ('%' + phoneNumber+ '%')OR SuppliedPhone LIKE : ('%' + formatedphonewithoutspace+ '%') OR ContactPhone LIKE : ('%' + formatedPhoneNumber + '%') OR ContactPhone LIKE : ('%' + phoneNumber+ '%') OR ContactPhone LIKE : ('%' + formatedphonewithoutspace+ '%') OR ContactPhone LIKE : ('%' + afterFirst+ '%') OR ContactPhone LIKE : ('%' + aftersecond+ '%') OR ContactPhone LIKE : ('%' + afterthird+ '%') ]) { objectList.add(objcase); } } else { for (Case objcase: [SELECT SuppliedName, SuppliedPhone,ContactEmail,Subject FROM Case WHERE SuppliedPhone =: formatedPhoneNumber OR SuppliedPhone =: phoneNumber OR SuppliedPhone =: formatedphonewithoutspace OR ContactPhone =: formatedPhoneNumber OR ContactPhone =: phoneNumber OR ContactPhone =: formatedphonewithoutspace OR ContactPhone =: afterFirst OR ContactPhone =: aftersecond OR ContactPhone =: afterthird ]) { objectList.add(objcase); } } //find in objectList phoneNumber String JSONString = JSON.serialize(objectList); return JSONString; } webService static String getCasesByContactPhone(String ContactNumber) { String formatedPhoneNumber = formatPhoneNumber(ContactNumber); String formatedphonewithoutspace=formatPhoneNumberwithoutspace(ContactNumber); String formatPhoneNumberwithoutplus =formatPhoneNumberwithoutplus(ContactNumber); string afterFirst=afterfirst(ContactNumber); string aftersecond=aftersecond(ContactNumber); string afterthird=afterthird(ContactNumber); List < SObject > objectList = new List(); if(ContactNumber.length() > 5){ for (Case objcase: [SELECT Id,Status, CaseNumber,SuppliedName,SuppliedPhone,ContactEmail,Subject,Contact.name, Contact.phone, Account.phone FROM Case WHERE ContactPhone LIKE: ('%' + ContactNumber+ '%')OR ContactMobile LIKE: ('%' + ContactNumber+ '%') OR ContactPhone LIKE : ('%' + formatedphonewithoutspace+ '%') OR ContactPhone LIKE : ('%' + formatedPhoneNumber + '%') OR ContactMobile LIKE: ('%' + formatedphonewithoutspace+ '%') OR ContactMobile LIKE: ('%' + formatedPhoneNumber + '%') OR ContactMobile LIKE: ('%' + afterFirst+ '%') OR ContactPhone LIKE : ('%' + afterFirst+ '%') OR ContactMobile LIKE: ('%' + aftersecond+ '%') OR ContactPhone LIKE : ('%' + aftersecond+ '%') OR ContactMobile LIKE: ('%' + afterthird+ '%') OR ContactPhone LIKE : ('%' + afterthird+ '%') OR Account.phone LIKE: ('%' + ContactNumber+ '%') OR Account.phone LIKE: ('%' + ContactNumber+ '%') OR Account.phone LIKE : ('%' + formatedphonewithoutspace+ '%') OR Account.phone LIKE : ('%' + formatedPhoneNumber + '%') OR Account.phone LIKE: ('%' + formatedphonewithoutspace+ '%') OR Account.phone LIKE: ('%' + formatedPhoneNumber + '%') OR Account.phone LIKE: ('%' + formatPhoneNumberwithoutplus+ '%') OR Account.phone LIKE: ('%' + afterFirst+ '%') OR Account.phone LIKE : ('%' + afterFirst+ '%') OR Account.phone LIKE: ('%' + aftersecond+ '%') OR Account.phone LIKE : ('%' + aftersecond+ '%') OR Account.phone LIKE: ('%' + afterthird+ '%') OR Account.phone LIKE : ('%' + afterthird+ '%') ]) { objectList.add(objcase); } } else { for (Case objcase: [SELECT Id,Status, CaseNumber,SuppliedName,SuppliedPhone,ContactEmail,Subject,Contact.name, Contact.phone FROM Case WHERE ContactPhone =: ContactNumber OR ContactMobile =: ContactNumber OR ContactPhone =: formatedphonewithoutspace OR ContactPhone =: formatedPhoneNumber OR ContactMobile =: formatedphonewithoutspace OR ContactMobile =: formatedPhoneNumber OR ContactMobile =: afterFirst OR ContactPhone =: afterFirst OR ContactMobile =: aftersecond OR ContactPhone =: aftersecond OR ContactMobile =: afterthird OR ContactPhone =: afterthird OR Account.phone =: ContactNumber OR Account.phone =: ContactNumber OR Account.phone =: formatedphonewithoutspace OR Account.phone =: formatedPhoneNumber OR Account.phone =: formatedphonewithoutspace OR Account.phone =: formatedPhoneNumber OR Account.phone =: formatPhoneNumberwithoutplus OR Account.phone =: afterFirst OR Account.phone =: afterFirst OR Account.phone =: aftersecond OR Account.phone =: aftersecond OR Account.phone =: afterthird OR Account.phone =: afterthird ]) { objectList.add(objcase); } } //find in objectList phoneNumber String JSONString = JSON.serialize(objectList); return JSONString; } //case by case number webService static String getCasesByCaseNumber(String phoneNumber) { List < SObject > objectList = new List(); for (Case objcase: [SELECT Id FROM Case WHERE CaseNumber like : ('%' + phoneNumber+ '%') ]) { objectList.add(objcase); } String JSONString = JSON.serialize(objectList); return JSONString; } //end case by case number //contact by contact number webService static String getContactByContactNumber(String phoneNumber) { List < SObject > objectList = new List(); for (Contact objcon: [SELECT Id FROM Contact WHERE Id =: phoneNumber ]) { objectList.add(objcon); } String JSONString = JSON.serialize(objectList); return JSONString; } //end contact by contact number //account by account number webService static String getAccountByAccountNumber(String phoneNumber) { List < SObject > objectList = new List(); for (Account objaccount: [SELECT id FROM Account WHERE AccountNumber like : ('%' + phoneNumber+ '%') ]) { objectList.add(objaccount); } String JSONString = JSON.serialize(objectList); return JSONString; } //end account by account number //Lead by lead number webService static String getLeadByLeadNumber(String phoneNumber) { List < SObject > objectList = new List(); for (Lead objlead: [SELECT id FROM Lead WHERE Id =: phoneNumber ]) { objectList.add(objlead); } String JSONString = JSON.serialize(objectList); return JSONString; } //end lead by lead number webService static String getAccountsByPhoneNumber(String phoneNumber) { String formatedPhoneNumber = formatPhoneNumber(phoneNumber); String formatedphonewithoutspace=formatPhoneNumberwithoutspace(phoneNumber); string afterFirst=afterfirst(phoneNumber); string aftersecond=aftersecond(phoneNumber); string afterthird=afterthird(phoneNumber); List < SObject > objectList = new List(); for (Account account: [SELECT id, name, phone,BillingAddress,owner.name FROM Account WHERE phone LIKE : ('%' + formatedPhoneNumber + '%') OR phone LIKE : ('%' + phoneNumber+ '%')OR phone LIKE : ('%' + formatedphonewithoutspace+ '%')OR phone LIKE : ('%' + afterFirst+ '%')OR phone LIKE : ('%' + aftersecond+ '%')OR phone LIKE : ('%' + afterthird+ '%') ]) { objectList.add(account); } //find in objectList phoneNumber String JSONString = JSON.serialize(objectList); return JSONString; } webService static String getAccountsByFirstLastName(String firstlastname) { List < Account > accountList = new List(); for (Account account: [SELECT id, name, phone,BillingAddress ,owner.name FROM Account WHERE name = : firstlastname]) { accountList.add(account); } String JSONString = JSON.serialize(accountList); return JSONString; } webService static String getAccountsByCorrelatorData(String correlatorData) { List < Account > accountList = new List(); String JSONString = JSON.serialize(accountList); return JSONString; } webService static String getAllRecordTypes() { List recordTypeList = new List(); for (RecordType recordTypes : [SELECT Id,Name,SobjectType,Description FROM RecordType ]) { recordTypeList.add(recordTypes); } string JSONString=JSON.serialize(recordTypeList); return JSONString; } webservice static string getProfileRecordTypesCase(){ List recordTypes = new List(); for(RecordTypeInfo info: Case.SObjectType.getDescribe().getRecordTypeInfos()) { if(info.isAvailable() && info.name != 'Master' ) { recordTypes.add(info); } } string JSONString=JSON.serialize(recordTypes); return JSONString; } webservice static string getProfileRecordTypesContact(){ List recordTypes = new List(); for(RecordTypeInfo info: Contact.SObjectType.getDescribe().getRecordTypeInfos()) { if(info.isAvailable() && info.name != 'Master') { recordTypes.add(info); } } string JSONString=JSON.serialize(recordTypes); return JSONString; } webservice static string getProfileRecordTypesAccount(){ List recordTypes = new List(); for(RecordTypeInfo info: Account.SObjectType.getDescribe().getRecordTypeInfos()) { if(info.isAvailable() && info.name != 'Master') { recordTypes.add(info); } } string JSONString=JSON.serialize(recordTypes); return JSONString; } webservice static string getProfileRecordTypesLead(){ List recordTypes = new List(); for(RecordTypeInfo info: Lead.SObjectType.getDescribe().getRecordTypeInfos()) { if(info.isAvailable() && info.name != 'Master') { recordTypes.add(info); } } string JSONString=JSON.serialize(recordTypes); return JSONString; } webService static String getContactsByPhoneNumber(String phoneNumber) { String formatedPhoneNumber = formatPhoneNumber(phoneNumber); String formatedphonewithoutspace=formatPhoneNumberwithoutspace(phoneNumber); string afterFirst=afterfirst(phoneNumber); string aftersecond=aftersecond(phoneNumber); string afterthird=afterthird(phoneNumber); List < Contact > contactList = new List(); for (Contact contact: [SELECT id, name, email, phone, otherphone, mobilephone, homephone,mailingaddress, account.name FROM Contact WHERE( phone LIKE : ('%' + formatedPhoneNumber + '%') OR otherphone LIKE : ('%' + formatedPhoneNumber + '%') OR mobilephone LIKE : ('%' + formatedPhoneNumber + '%') OR homephone LIKE : ('%' + formatedPhoneNumber + '%') OR phone LIKE : ('%' + phoneNumber+ '%') OR otherphone LIKE : ('%' + phoneNumber+ '%') OR mobilephone LIKE : ('%' + phoneNumber+ '%') OR homephone LIKE : ('%' + phoneNumber+ '%') OR phone LIKE : ('%' + afterFirst+ '%') OR otherphone LIKE : ('%' + afterFirst+ '%') OR mobilephone LIKE : ('%' + afterFirst+ '%') OR homephone LIKE : ('%' + afterFirst+ '%') OR phone LIKE : ('%' + aftersecond+ '%') OR otherphone LIKE : ('%' + aftersecond+ '%') OR mobilephone LIKE : ('%' + aftersecond+ '%') OR homephone LIKE : ('%' + aftersecond+ '%') OR phone LIKE : ('%' + afterthird+ '%') OR otherphone LIKE : ('%' + afterthird+ '%') OR mobilephone LIKE : ('%' + afterthird+ '%') OR homephone LIKE : ('%' + afterthird+ '%') OR phone LIKE : ('%' + formatedphonewithoutspace+ '%') OR otherphone LIKE : ('%' + formatedphonewithoutspace+ '%') OR mobilephone LIKE : ('%' + formatedphonewithoutspace+ '%') OR homephone LIKE : ('%' + formatedphonewithoutspace+ '%') ) ]) { contactList.add(contact); } String JSONString = JSON.serialize(contactList); return JSONString; } webService static String getContactsByFirstLastName(String firstlastname) { List < Contact > contactList = new List(); for (Contact contact: [SELECT id, name, email, phone, otherphone, mobilephone, homephone, account.name FROM Contact WHERE name LIKE : ('%' + firstlastname + '%')]) { contactList.add(contact); } String JSONString = JSON.serialize(contactList); return JSONString; } webService static String getContactsByCorrelatorData(String correlatorData) { List < Contact > contactList = new List(); String JSONString = JSON.serialize(contactList); return JSONString; } webService static String getContactsByEmail(String emailContact) { List < Contact > contactList = new List(); for (Contact contact: [SELECT id, name, email, phone, otherphone, mobilephone, homephone FROM Contact WHERE email LIKE : ('%' + emailContact + '%')]) { contactList.add(contact); } String JSONString = JSON.serialize(contactList); return JSONString; } webService static String getContactByRecordId(String recordId) { List < Contact > contactList = new List(); for (Contact contact: [SELECT id, name, email, phone, otherphone, mobilephone, homephone FROM Contact WHERE Id =: recordId]) { contactList.add(contact); } String JSONString = JSON.serialize(contactList); return JSONString; } webService static String getLeadsByPhoneNumber(String phoneNumber) { String formatedPhoneNumber = formatPhoneNumber(phoneNumber); String formatedphonewithoutspace=formatPhoneNumberwithoutspace(phoneNumber); string afterFirst=afterfirst(phoneNumber); string aftersecond=aftersecond(phoneNumber); string afterthird=afterthird(phoneNumber); List < SObject > objectList = new List(); for (Lead lead: [SELECT id, name, phone,email,address FROM Lead WHERE phone LIKE : ('%' + formatedPhoneNumber + '%') OR phone LIKE : ('%' + phoneNumber + '%')OR phone LIKE : ('%' + formatedphonewithoutspace+ '%') OR MobilePhone LIKE : ('%' + formatedPhoneNumber + '%') OR MobilePhone LIKE : ('%' + phoneNumber + '%')OR MobilePhone LIKE : ('%' + formatedphonewithoutspace+ '%')OR phone LIKE : ('%' + afterFirst+ '%') OR MobilePhone LIKE : ('%' + afterFirst+ '%')OR phone LIKE : ('%' + aftersecond+ '%') OR MobilePhone LIKE : ('%' + aftersecond+ '%')OR phone LIKE : ('%' + afterthird+ '%') OR MobilePhone LIKE : ('%' + afterthird+ '%') ]) { objectList.add(lead); } String JSONString = JSON.serialize(objectList); return JSONString; } @HttpGet webService static String getRainbowConnectorProfile() { List < User > users = new List(); for (User user : Database.query('Select Id, RainbowConnectorProfile__c FROM User WHERE Id =\'' + UserInfo.getUserId() + '\'')) { users.add(user); } String JSONString = JSON.serialize(users); return JSONString; } webService static void setRainbowConnectorProfile(String userProfile) { SObject userObject = [SELECT Id, RainbowConnectorProfile__c FROM User WHERE Id =: UserInfo.getUserId() LIMIT 1]; userObject.put('RainbowConnectorProfile__c', userProfile); update userObject; } webService static String getTaskByCallId(String callId) { List < SObject > objectList = new List(); for (Task task: [SELECT Id FROM Task where CallObject =: callId]) { objectList.add(task); } String JSONString = JSON.serialize(objectList); return JSONString; } //for Caixa webService static String addCallRecord(String calldatetime, String phoneNumber, String duration, String direction, String callid, String state, String comments,String accountid) { String objectID =accountid; return objectID; } }