1

First-time poster here so feel free to give me pointers on forum etiquette. I have been trying to teach myself Apex the past 2 months and I have a personal project that I am working on that involves making a Rest API post request to an external platform. I have a JSON working in Postman that I posted below, but I can't seem to format my JSON in Apex with the wrapper class.I used the JSON to Apex formatting tool through this site:https://json2apex.herokuapp.com/?_ga=2.79913438.836555078.1666614432-2013718170.1665061844. If I check the debug logs, it seems to be caught up on the second "Content" list which is inside the description block. I have tried re-ordering the fields in my wrapper class and changing the variables names but I cant seem to figure out the issue so any help here would be appreciated.

Working JSON Post:

{
"fields": {
    "priority": {
        "id": "3"
    },
    "assignee": {
        "accountId": "xxxxxxx"
    },
    "components": [],
    "reporter": {
        "accountId": "xxxxxxxx"
    },
    "issuetype": {
        "id": "10002"
    },
    "project": {
        "key": "xxxxxxx"
    },
    "summary": "Testing API from Postman",
    "description": {
        "type": "doc",
        "version": 1,
        "content": [
            {
                "type": "paragraph",
                "content": [
                    {
                        "text": "Hello Postman",
                        "type": "text"
                    }
                ]
            }
        ]
    }
}

Apex Wrapper Class

public with sharing class JiraWrapperClass {
    
    public Fields fields;
    
    public class Fields {
        public Priority priority;
        public Assignee assignee;
        public Reporter reporter;
        public IssueType issuetype;
        public Project project;
        public String summary;
        public Description description;
    }

    public class Priority {
        public String id;
    }

    public class Assignee {
        public String accountId;
    }

    public class Reporter{
        public String accountId;
    }

    public class IssueType{
        public String id;
    }

    public class Project {
        public String key;
    }

    public class Content {
        public String text;
        public String type;
    }


    public class Content_Z {
        public String type;
        public List<Content> content;
    }

    public class Description {
        public String type;
        public Integer version;
        public List<Content_Z> content;
    }
}

HTTP Post Request Apex Class

public with sharing class SFJiraIntegration{
    @future (callout=true)
    public static void createJiraTicket(String caseId){
        final String PRIORITY_ID = '3';
        final String ASSIGNEE_ID = 'xxxxx';
        final String REPORTER_ID = 'xxxxxx';
        final String ISSUE_TYPE_ID = '100002';
        
        sObject s = [SELECT Subject, Description FROM Case WHERE Id = :caseId LIMIT 1];
        String c_summary = (String) s.get('Subject');
        String c_description = (String) s.get('Description');
        
        JiraWrapperClass requestBody = new JiraWrapperClass();

        JiraWrapperClass.Fields fields = new JiraWrapperClass.Fields();
        JiraWrapperClass.Priority priority = new JiraWrapperClass.Priority();
        priority.id = PRIORITY_ID;

        JiraWrapperClass.Assignee assignee = new JiraWrapperClass.Assignee();
        assignee.accountId = ASSIGNEE_ID;

        JiraWrapperClass.Reporter reporter = new JiraWrapperClass.Reporter();
        reporter.accountId = REPORTER_ID;

        JiraWrapperClass.IssueType issuetype = new JiraWrapperClass.IssueType();
        issuetype.id = ISSUE_TYPE_ID; 

        JiraWrapperClass.Project project = new JiraWrapperClass.Project();
        project.key = 'xxxxx';

        JiraWrapperClass.Content listContent = new JiraWrapperClass.Content();
        listContent.text = 'hello world from SF';
        listContent.type = 'text';

        List<JiraWrapperClass.Content> descriptionContent = new List<JiraWrapperClass.Content>();
        descriptionContent.add(listContent);

        JiraWrapperClass.Content_Z content_z = new JiraWrapperClass.Content_Z();
        content_z.type = 'paragraph';
        content_z.content = descriptionContent;

        List<JiraWrapperClass.Content_Z> ls_z = new List<JiraWrapperClass.Content_Z>();
        ls_z.add(content_z);

        JiraWrapperClass.Description description = new JiraWrapperClass.Description();
        description.type = 'doc';
        description.version = 1;
        description.content = ls_z;

        fields.priority = priority;
        fields.assignee = assignee;
        fields.reporter = reporter;
        fields.issuetype = issuetype;
        fields.project = project;
        fields.summary = c_summary;
        fields.description = description;
        requestBody.fields = fields;

        String formattedBody = JSON.serialize(requestBody);
        System.debug(formattedBody);
        

        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint('https://xxxxx');
        req.setHeader('Authorization','Basic xxxx');
        //req.setEndpoint('callout:JiraRestAPI');
        req.setHeader('Content-Type', 'application/json;charset=UTF-8');
        req.setBody(formattedBody);
        

        try {
            HttpResponse response = http.send(req);
                if(response.getStatusCode() == 200){
                    System.debug(response.getBody());
        }
        } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
        }

    }
}

JSON format in debug log

{
  "fields": {
    "summary": "Bug",
    "reporter": {
      "accountId": "xxxxxxxxx"
    },
    "project": {
      "key": "PMO60"
    },
    "priority": {
      "id": "3"
    },
    "issuetype": {
      "id": "100002"
    },
    "description": {
      "version": 1,
      "type": "doc",
      "content": [
        {
          "type": "paragraph",
          "content": [
            {
              "type": "text",
              "text": "hello world from SF"
            }
          ]
        }
      ]
    },
    "assignee": {
      "accountId": "xxxxxxxxxxxx"
    }
  }
}
6
  • 3
    if your issue is that you are not able to see json debug logs after some characters.it is working as expected, you have to downlod debug log to see complete debug statement. download debug log and open it in notepad. and check your json log. Nov 26, 2022 at 16:58
  • 1
    Welcome to Salesforce Stack Exchange (SFSE) and to SF Apex development! As @gauravsharma said, the debug logs in the console will be truncated and will not necessarily show everything. Please edit your question to elaborate on A) how else you may have determined that it is not working (and give details of exact error text or unexpected results, and/or B) if the downloaded logs look the same as your screenshot of the log in the UI.
    – Moonpie
    Nov 26, 2022 at 22:50
  • @gauravsharma Thanks for the tip about the truncation happening in the debug log, I figured that was the case but couldn't see the full JSON. Here is the JSON that it is trying to send in the HTTP request body. The "assignee" field seems to get compiled towards the bottom of the JSON which I believe is causing the problem. Just an FYI I am getting a Status Code of 400 on my HTTP request and that is why I figured it was a formatting issue.
    – CDNHOPHEAD
    Nov 27, 2022 at 0:44
  • 1
    This is why it is best to include copied/pasted exact error text when asking for help. This could be an xy problem. Regarding your concern with assignee, I do not think that is the issue. JSON data is accessed via the name (a.k.a. key) to get the values, so it does not matter the order within the JSON as long as the hierarchy is intact. (In other words, as long as assignee is at the correct level, which it is - directly under fields.)
    – Moonpie
    Nov 27, 2022 at 5:38
  • 1
    That said, the JSON you build in your wrapper class and the JSON you added to the end of your question look the same, and both of those do differ from the original JSON at the beginning of your question: they are missing the components array. That could possibly be a required element that you are not including.
    – Moonpie
    Nov 27, 2022 at 5:41

1 Answer 1

0

After all this, I found the typo/bug that was causing the 401 code. It was the issuetype id of "10002" vs "100002" in the Apex class. Thanks again @Moonpie for your help.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .