I have two sales-force org (org 1 and org 2).I am insert a new account in my org 1 now i want that every time a org 1 account created then org 2 will automatically update its account with org 1 data using rest API .
[SalesForce] Salesforce Case integration via REST API call
Related Solutions
The link which you have shared allows the user to bypass the duplicate matching rule defined in salesforce. In REST API, the method will be same. Sharing a code snippet here for enforcing the insertion of duplicate records and fetching the already existing duplicate records.
The method simply creates an Account and returns the list of duplicate accounts existing in the database.
@HttpPost
global static List<Id> fetchDuplicates(String name, String phone, String website) {
Set<Id> setDuplicateIds = new Set<id>();
Account account = new Account(Name = name, phone = phone, website = website);
Database.SaveResult sr = Database.insert(account, false);
if (!sr.isSuccess()) {
Datacloud.DuplicateResult duplicateResult;
// Insertion failed due to duplicate detected
for(Database.Error duplicateError : sr.getErrors()){
duplicateResult = ((Database.DuplicateError)duplicateError).getDuplicateResult();
}
// Fetch the Ids of the existing duplicate records
for(Datacloud.MatchResult duplicateMatchResult : duplicateResult.getMatchResults()) {
for(Datacloud.MatchRecord duplicateMatchRecord : duplicateMatchResult.getMatchRecords()) {
setDuplicateIds.add(duplicateMatchRecord.getRecord().Id);
}
}
System.debug('Duplicate records ' + setDuplicateIds);
// If the duplicate rule is an alert rule, we can try to bypass it
Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.AllowSave = true;
Database.SaveResult sr2 = Database.insert(account, dml);
if (sr2.isSuccess()) {
System.debug('Duplicate account has been inserted in Salesforce!');
}
}
// Return the existing duplicate records Ids
List<Id> lstDuplicateIds = new List<Id>();
lstDuplicateIds.addAll(setDuplicateIds);
return lstDuplicateIds;
}
Hope this helps!
First of all, ensure that your Class is global and method is webservice like below -
global class MyClass
{
webservice static void myMethod() // you can pass parameters
{
// Do something
}
}
Next if you are calling from Button Click then ensure that Button behaviour is as below-
- Behaviour : Execute Javascript
- Content source : On-Click Javascript
Ensure that you include appropriate JS files in the Script-
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
Method can be called as below -
sforce.apex.execute("MyClass","myMethod",{}"});
If method accepts a parameter, the same can be passed in the third argument comma seperated as below -
sforce.apex.execute("MyClass","myMethod",{requestString:"Sample Request",endpoint:"www.a.com/b"}"});
Now if you are calling from a VF page then the code remains same but ensure that you have added scripts to the top as below:
<apex:includeScript value="/soap/ajax/30.0/connection.js"/>
<apex:includeScript value="/soap/ajax/30.0/apex.js"/>
If you are calling from Outside Salesforce then I would suggest -
- Setting OAUTH in salesforce by creating a connected App. (App Setup>Create>Apps)
- Change the URL mapping to the top to display appropriate URL. ( This will be endpoint)
- then you can use either Curl or SOAP UI tool or hurl.it to check if your class gives proper response.
The process is bit lengthy to explain, and I found a good link that sums it all - http://www.oyecode.com/2014/08/start-building-your-own-rest-api-in.html
Best Answer
You can make your own rest api callouts from the account create trigger in org1 to the salesforce standard api of org2. or alternatively you can set up salesforce to salesforce
This Feature will allow you to sync records between the 2 orgs without the need to write additional callouts in apex.
Writing a custom rest api is something I would only do in a scenario where I have additional logic that needs to be executed or when additional abstraction is preferred in the API.