Syncing Custom Fields with Salesforce

Blank 8/11/2023 12:45 - 8/11/2023 12:45
Developers

This Article acts on previous knowledge of Salesforce and having set it up after following the Tutorial here: Integrating with Salesforce
This is a quick rundown on how to sync custom fields for Organisations on Salesforce to and from Kademi

 

How to sync fields from Salesforce into Kademi

 

Step 1. Find the Object Manager in your Salesforce Console

 

Step 2. Find the Accounts Object

Step 3. Find Fields & Relationships

 

Step 4. In the top right click on "New" to start creating a new field

 

Step 5. Select the type, either number or text will do fine, and then hit next in the bottom right

 

Step 6. Fill in the inputs to name your field and give it a description and stuff

 

Step 7. Then hit next and next again till you get to the end then hit save

 

Now we want to set the field to a value on an Organisation in Salesforce


Step 8. Use the App Launcher and select Sales Console

 

Step 9. If its not already open, Select "Accounts" from this dropdown

 

Step 10. We will select our "Account" from the list and then head over to the details tab on the page that loads, we should then see our new field in the list of fields and we will hit the edit icon next to it

 

Step 11. Set a value and then hit save

We should now see the value saved to the Account

 

Now we want to setup a Sync Job Action in Kademi, This requires a bit of code development.

Head on over to your development environment and we will start with something like the below

The code for this should go into the app you are developing, Preferably in the app.js file

 

var syncJobActionProvider = {
    name: function () {
        return 'sfCrmSyncActionProvider';
    },
    title: function () {
        return 'SF Transform AAA';
    },
    onLocalObjectUpdated: function (type, id, localObject) {
        var om = services.organisationManager;
        log.info("onLocalObjectUpdated AAA type={}, id={} localObject={}", type, id, localObject);
        var remoteObj =  services.salesforceService.getSFEntity(id, "Account", ['my_new_field__c']);
        var params = formatter.newMap();
        params.put("my_new_field", remoteObj.my_new_field__c);
        om.updateOrgExtraFields(localObject, localObject.allSelectedOrgTypes(), formatter.newFormContext(params));
        log.info("Saving Extra Fields");
        
        
    }
};

var syncJobActionProviders = {
    appName: "sf-actions",
    getActions: function () {
        return [syncJobActionProvider];
    }
} 

controllerMappings
    .newImplementationBuilder('syncJobActionProviders')
    .implementationObject(syncJobActionProviders)
    .build();

 

We define the Sync Job Action and then when a Local Object is updated we will pull the custom field from Salesforce and then update it on the Organisation


Step 12. And then when we select it as a Sync Action on our job and then hit save

Once we hit save we can hit run, and then we should see our Accounts sync into Kademi as Organisations

and we can go to our org and see the Custom Field has stored

 

How to sync fields from Kademi into Salesforce

 

Acting on the Knowledge we learnt before, we now know how to set up a Sync Job Action Handler that will be called when we run our Sync Job

Salesforce and other CRMs allow for "Bidirectional Sync" which means while we can pull fields from Salesforce into Kademi, we can also push fields from Kademi into Salesforce, here is a quick tutorial on how to do that.

 

1. First find your Organisation somewhere in Kademi and head to the edit tab.

2. Find the field you want to edit, and set a value to it.

 

3. Next head over to your code editor where you are writing the Sync Job Action, and we want to use a new hook called,

beforeRemoteObjectUpdated

 this will be called right before the object is updated remotely, allowing us to append more data to the object we are about to update, It looks like this.

 

var syncJobActionProvider = {
    name: function () {
        return 'sfCrmSyncActionProvider';
    },
    title: function () {
        return 'SF Transform AAA 2';
    },
    
    beforeRemoteObjectUpdated: function (type, bodyDataObject, localObject, syncJob){
        bodyDataObject.my_new_field__c = localObject.field("my_new_field");
        log.info("beforeRemoteObjectUpdated AAA type={}, bodyDataObject={} localObject={} syncJob={}", type, JSON.stringify(bodyDataObject), localObject, syncJob);
    }
};

 we get passed our editable object (bodyDataObject) and local object (localObject) which we can use to modify what our remote object has

So we grab "my_new_field" from Kademi and need to set "my_new_field__c"
 (the __c is a denotion for custom field in Salesforce),

 

Once we have that we can run our Sync Job.

And then our job will run, it may take a few minutes depending on how many objects it has to sync.

 

Once we are finished we can head over to our object in Salesforce and see that the field is now stored.