Search
  • Medium One

Connect Salesforce to Medium One

Updated: May 16

This tutorial connects your Medium One account with your Salesforce account. Once connected, whenever a case is created in Salesforce, a ticket is sent to Medium One. Device data is gathered and posted back to the Salesforce’s case automatically.


In this tutorial, you will:

  1. Create a case in the Salesforce environment and view the event on Medium One

  2. Build a Workflow to add a comment to the case


Here’s what you need to get started:

  1. Medium One Prototyping Sandbox [Buy]

Step 1. Create an API Key and API Basic User

Once logged into your Medium One account, create an API Key by going to Setup -> Manage API Keys. Click on “Add New API Key” and Save. Make sure the ‘Enabled’ box is checked. Note the API Key, you will need it in Step 5.



Next, we’re going to create an API Basic User. Go to Setup -> Manage Users. Click on “Add New User”. Type in a username and password then save. Note these credentials, you will need them in Step 5.


Step 2. Create a Salesforce Free Developer Account

Sign up for a free developer salesforce account here.


Enter all required data and click “Sign me up” to create account.

Note: You will receive a welcome ‘Your Developer Edition Login Information’ email from salesforcedevs.com stating your username and asking you to set your password. Follow those instructions.


Step 3. Enable Apex Permissions in Salesforce

a.  Salesforce Viewing Mode (Lightning or Classic)

If you see a salesforce page similar to the following, you are in the new Salesforce Lightning Experience mode. Switch to Salesforce Classic mode by selecting the person icon in the upper right and then click on Switch to Salesforce Classic.

If you are already in Salesforce Classic mode, go to (b).

You will be switched to Classic Mode and see the following view.  Click on “Setup” in the top righthand corner.

b.  System Administrator Mode

Setup will open up the Administer page.  Look for System Administrator under Recent Items and click on it.

If you can't find it, go to “Administer” -> “Manage Users” -> “Profile” in the sidebar.

Under Profiles, you should find System Administrator.  Click on it.

The ‘System Administrator’ profile page will appear as shown below.

Now scroll down and check that the Author Apex permission is enabled.


Step 4. Add Medium One to Approved Sites

Click on “Setup” in the upper righthand corner on Salesforce.  Go to the quick find box on the left-hand side, type in “remote.” Under “Security Settings”, click on “Remote Site Settings.”

You will see the Remote Site page.  To add Medium One as a new remote site, click on New Remote Site.

Add remote site URL https://api.mediumone.com.

Click Save.


Step 5. Create Apex Classes

From the Salesforce Developer Force Homepage you should see your name next to the Setup button on the top right hand corner. Click on your name, then click on Developer Console underneath it.

A new window should open. Click on File → New → Apex Class. Name the class “M1”.

Replace the existing code by copying and pasting the following into the window, replacing <DEVICE_PASSWORD> with the API basic user’s password <DEVICE_NAME> with the API basic user's username, and <API_KEY> with your API key from Step 1.

public class M1 {
    private static String user_id='<DEVICE_NAME>';
    private static String password='<DEVICE_PASSWORD>';
    private static String api_key ='<API_KEY>';

    @future(callout=true)
    public static void send_data(String event){
        String url = '<a href="https://api-sandbox.mediumone.com">https://api-sandbox.mediumone.com</a>/v2/login/?no_cookies';
        Http h = new Http();
        HttpRequest req = new HttpRequest();     
        JSONGenerator gen = JSON.createGenerator(false);
        gen.writeStartObject();
        gen.writeStringField('login_id', user_id);
        gen.writeStringField('password', password);
        gen.writeStringField('api_key', api_key);
        String body = gen.getAsString();

        req.setBody(body);
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Accept', 'application/json');
        req.setEndpoint(url);
        req.setMethod('POST');
        HTTPResponse res = h.send(req);
        send_event_helper(event,res.getBody().removeStart('"').removeEnd('"'));
    }

            private static void send_event_helper(String val, String token){      
                        String url = '<a href="https://api-sandbox.mediumone.com">https://api-sandbox.mediumone.com</a>/v2/events/salesforce?token=' + token;
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        String body = '{"event_data":' + val + '}';
        req.setBody(body);
        req.setHeader('Content-Type', 'application/json');
        req.setEndpoint(url);
        req.setMethod('POST');
        HTTPResponse res = http.send(req);
        Integer status_code = res.getStatusCode();
        System.assertEquals(200, status_code);
     }
} 

Go to File→ Save.

Now, you need to create the trigger. Go to File→ new → Apex Trigger.

Name the class “m1_trigger”. Choose the sObject to be ‘Case’ and Submit.

Copy and paste the following into the window:

trigger m1_trigger on Case (after insert, after update, after undelete) {
    for(Case c: Trigger.new){
        String event = JSON.serialize(c);
        M1.send_data(event);
    }
} 

Click File ->Save.


You may now close the Developer Console window.


Now, whenever you create a new case, it will send case details to your Medium One account.


Note:

If you would like to connect your production salesforce.com account (non developer), you’ll need to create APEX test code.  Refer to the Appendix A for sample test code.


Step 6. Link Salesforce.com to Medium One

In Medium One, go to “Setup”  -> “External APIs.” Click “Add New Service Key”.

Select ‘salesforce’ from the drop down menu; then click “Save and Activate”.

If this is your first time logging to Salesforce account, you will need to allow access for Salesforce. Click ‘Allow”.

Lastly, a window will open up saying ‘Authentication successful. You may close this page.’

Now, you can post to and edit cases from M1 workflows.


Step 7. Create Salesforce Stream on your Medium One Project

In Medium One, go to “Config” -> “Data Streams”, then click on "Create New Stream".


In Create New Data Stream, enter the new stream’s name to be ‘salesforce’ and then click “Save Data Stream”.

Now, the ‘salesforce’ datastream is linked to collect all events between Salesforce and Medium One.


Checkpoint

You can quickly check that your new environment is generated correctly by creating a case and seeing its data created on Medium One.

In the homepage of Salesforce, click on “+” and then on “Cases”.

Create a new case by clicking on “New“.

Fill all required information (Account Name, Status, Case Origin, Subject, and Description) to “New Case” ; then click “Save”.

Note: If this is a new ‘Account Name’ that is not linked in your account profiles, you will quickly need to create it by clicking on magnifying glass, then on "New" and entering all necessary info to the Account page; then click “Save”.

By clicking “Save” when creating a new case, we created a new event, which was sent to Medium One. Now, we can easily check if we have received this event by opening the Data Viewer and checking the content of ‘salesforce’ data stream.


In Medium One, go to “Data Viewer” -> “Data Streams”. Click “View” on the newly created ‘salesforce’ data stream.

If the creation of case in Salesforce was successful, you will see the following log file in the data stream viewer:

You can expand the device’s log file from this event by clicking on ‘+’ sign:

NOTE: If no event appears, don’t continue and go back and make sure you followed all steps correctly.


Step 8. Create a Salesforce Workflow

In this section we will create a workflow that will trigger when a case is created.  This workflow will leave a comment on the Salesforce case.


Create a new workflow and name it “Salesforce”; then click and drag the salesforce tag “Id” as your trigger from Tags & Triggers.


Next, from Modules, under ‘Foundation’, drag Base Python onto the canvas and connect the modules. Your workflow should look like this:

Double click on Base Python , and replace the existing script with the following code:

import Salesforce

salesforce_id = IONode.get_input('in1')['event_data']['value']
salesforce_update = "Comment from Medium One"

log(salesforce_update)
Salesforce.comment_case(salesforce_id,salesforce_update) 

Then click “Save and Activate”. This workflow adds a comment “Comment from Medium One” to every case created on your Salesforce account.


Checkpoint

Now let’s create a new Salesforce case and test the workflow.

In this tutorial, we created a new case with Subject: Device not working in Salesforce.

Once we click on “Save”, this case is created. Click on Feed tab and you will see acknowledgement of the case creation. You can read the comment that the case was created.

Refresh the Salesforce page.

In the section ‘All Updates for this case’, you can view the comment from Medium One.



Congratulations! You just connected Salesforce to Medium One and automatically populated the case with a comment.


Want to learn more about what Medium One can do?

Check out our documentation and tutorials & get started on your next IoT project!



Appendix A: Add Apex Test Classes

Salesforce requires that for deployment to Production organizations, at least 75% of the Apex code must be covered by test cases. If you plan on moving off of a Developer organization, you must add the following test interfaces and tests.


The reason for creating these test classes is that you may not have a real HTTP callout in a test, so you must create mock HTTP Callout responses to demonstrate how the data should respond given the input.


In the Salesforce Developer Console, click on File→ New -> Apex Class, and name the class “TestM1SucessfulSendEvent.” Copy and paste the following code into the class.

@isTest

global class TestM1SucessfulSendEvent implements HttpCalloutMock  {

global HTTPResponse respond(HTTPRequest req) {

System.assertEquals('https://api.mediumone.com/v2/events/salesforce?token=token', req.getEndpoint());

System.assertEquals('POST', req.getMethod());

HttpResponse res = new HttpResponse();

res.setStatusCode(200);

return res;

}

} 

Click Save.  Click File→ New -> Apex Class, and name the class “TestM1SucessfulLogin.” Copy and paste the following code:

@isTest

global class TestM1SucessfulLogin implements HttpCalloutMock {

global HTTPResponse respond(HTTPRequest req) {

System.assertEquals('https://api.mediumone.com/v2/login/?no_cookies', req.getEndpoint());

System.assertEquals('POST', req.getMethod());

HttpResponse res = new HttpResponse();

res.setStatusCode(200);

return res;

}

} 

Click Save.

Create the last class and name it “M1Test.” Paste the following:

@isTest

public class M1Test {

@isTest static void testSuccessfulEvent() {

M1.login_mock = new TestM1SucessfulLogin();

M1.event_mock = new TestM1SucessfulSendEvent();

M1.send_data('{"test":"test"}');

} 

@isTest static void testSuccessfulCaseCreation() {

M1.login_mock = new TestM1SucessfulLogin();

M1.event_mock = new TestM1SucessfulSendEvent();

Case new_case = new Case();

insert new_case;

}

} 






Now you have successfully created your test classes and can deploy your code to production.


Appendix B: More Info

Check out our Salesforce integration documentation here.

© 2018 Medium One   All Rights Reserved

4633 Old Ironsides Dr. #280, Santa Clara, CA  95054                

info@mediumone.com

|

  • YouTube - White Circle