Integrate Zendesk with Salesforce

Posted on December 27, 2021 Post Thumbnail

Yo Trailblazers,

In this blog post, We are going to integrate Zendesk with Salesforce.

I have a scenario, We need to fetch all the tickets from Zendesk and create cases in Salesforce using these tickets daily bases.

What you'll Learn from Blog Post :)

1. Make callout to rest api from Apex.

2. Wrapper class to parse returned response.

3. Basics of Scheduled Apex.

Let's Start :)

1. First of all create an account on Zendesk.

https://www.zendesk.com/register/?ref=341#step-1

2. After creating an account.

Enable following settings.

Enable Api & Password Access.

Api enableEnable password Access

Now we are able to make callout.

But for making callout, We need to look into Zendesk Developer Guide.

https://developer.zendesk.com/api-reference/ticketing/introduction/

Endpont Url: https://{subdomain}.zendesk.com/api/v2/tickets.json

Sample response.

{
  "assignee_id": 235323,
  "collaborator_ids": [
    35334,
    234
  ],
  "created_at": "2009-07-20T22:55:29Z",
  "custom_fields": [
    {
      "id": 27642,
      "value": "745"
    },
    {
      "id": 27648,
      "value": "yes"
    }
  ],
  "description": "The fire is very colorful.",
  "due_at": null,
  "external_id": "ahg35h3jh",
  "follower_ids": [
    35334,
    234
  ],
  "group_id": 98738,
  "has_incidents": false,
  "id": 35436,
  "organization_id": 509974,
  "priority": "high",
  "problem_id": 9873764,
  "raw_subject": "{{dc.printer_on_fire}}",
  "recipient": "[email protected]",
  "requester_id": 20978392,
  "satisfaction_rating": {
    "comment": "Great support!",
    "id": 1234,
    "score": "good"
  },
  "sharing_agreement_ids": [
    84432
  ],
  "status": "open",
  "subject": "Help, my printer is on fire!",
  "submitter_id": 76872,
  "tags": [
    "enterprise",
    "other_tag"
  ],
  "type": "incident",
  "updated_at": "2011-05-05T10:38:52Z",
  "url": "https://company.zendesk.com/api/v2/tickets/35436.json",
  "via": {
    "channel": "web"
  }
}

3. Let's create sample tickets in Zendesk.

Tickets

4. We need to create Named Credentials 

A named credential specifies a callout endpoint and its required authentication parameters. When setting up callouts, avoid setting authentication parameters for each callout by referencing named credentials.

Named creds

5. Let's create Apex Classes for Callout.

public class zendesktickets {
    public static void getTickets(){
        HttpRequest req = new HttpRequest();
        req.setEndpoint('callout:heysfzendesk/api/v2/tickets.json');
        req.setMethod('GET');
        Http http = new Http();
        HTTPResponse res = http.send(req);
        system.debug(res.getBody());
        JsonParser allTickets = (JsonParser)JSON.deserialize(res.getBody(), JsonParser.class);
		list<case> allCase = new list<case>();
        for(JsonParser.tickets t:allTickets.tickets)
        {
            case c = new case();
            c.Status = t.status;
            c.Subject = t.subject;
            c.Description = t.description;
            c.Origin = 'Phone';
            allCase.add(c);
        }
        upsert allCase;
    }  
}
public class JsonParser{
    public String next_page{get;set;}
    public String previous_page{get;set;}
    public Integer count{get;set;}
    public list<tickets> tickets{get;set;}
    public class tickets{
        public Decimal group_id{get;set;}
        public list<String> follower_ids{get;set;}
        public list<String> collaborator_ids{get;set;}
        public list<String> email_cc_ids{get;set;}
        public String organization_id{get;set;}
        public String forum_topic_id{get;set;}
        public Decimal assignee_id{get;set;}
        public String problem_id{get;set;}
        public Decimal submitter_id{get;set;}
        public Boolean has_incidents{get;set;}
        public Decimal requester_id{get;set;}
        public Boolean is_public{get;set;}
        public String recipient{get;set;}
        public String due_at{get;set;}
        public String status{get;set;}
        public list<String> tags{get;set;}
        public String priority{get;set;}
        public list<String> custom_fields{get;set;}
        public String description{get;set;}
        public String satisfaction_rating{get;set;}
        public String raw_subject{get;set;}
        public list<String> sharing_agreement_ids{get;set;}
        public String subject{get;set;}
        public list<String> fields{get;set;}
        public String type{get;set;}
        public list<String> followup_ids{get;set;}
        public String updated_at{get;set;}
        public Decimal ticket_form_id{get;set;}
        public String created_at{get;set;}
        public Decimal brand_id{get;set;}
        public Boolean allow_channelback{get;set;}
        public String external_id{get;set;}
        public Integer id{get;set;}
        public Boolean allow_attachments{get;set;}
        public String url{get;set;}
    }
}

For second class, I used https://www.adminbooster.com/tool/json2apex

After creating above 2 classes.

Run Classes.

Run Anon

After running, I have created 2 Cases in Salesforce.

Result.

6. Now, Let's create schedule Apex for daily tickets.

global class dailytickets implements Schedulable {
    global void execute(SchedulableContext ctx) {
        zendesktickets.getTickets();
    }
}

You can schedule it from Apex classes.

If you have any question Ask Me

Thanks for Reading :)

Write a comment for suggestions and hit the heart icon.


327
0

Tags: #salesforce #salesforceIntegration

Comments

There are no comments yet.
Your message is required.

Get notified of new posts