Skip to main content
titan blazers 님이 Apex Metadata API에 질문했습니다

Hi ,

I need the attachment_url ,under attachment attribute.

How to get this attachment_url value using apex

My Sample JSON :

    "ticket_cc_emails": [],

    "fr_escalated": false,

    "spam": false,

    "email_config_id": null,

    "group_id": 72000320936,

    "priority": 1,

    "requester_id": 72012786963,

    "responder_id": 72010582985,

    "source": 2, 

    "status": 5,

    "subject": "Updated ACP Capacity", 

    "id": 54942,

    "type": "SE Scheduler",

    "due_by": "2024-04-11T12:27:12Z",

    "fr_due_by": "2024-04-06T13:57:12Z",

    "is_escalated": false,   

    "custom_fields": {

        "cf_class75953": "Configuration",

        "cf_subclass650579": "Availability",

        "cf_class832453": null,

        "cf_subclass605425": null,

        "cf_billing_subclass": null,

        "cf_test": null,

        "cf_connect_type": null,

        "cf_connect_subtype": null,         

        "cf_subclass": null,   

        "cf_priority": false

    },

    "created_at": "2024-04-05T16:26:34Z",

    "updated_at": "2024-04-08T13:24:00Z",

    "tags": [

        "SP Post Launch Adoption"

    ],

    "attachments": [

        {

            "id": 72092546835,

            "content_type": "image/png",

            "size": 106213,

            "name": "Screenshot 2024-03-22 134756 (1).png",

            "attachment_url": "https://s3.amazonaws.com",

            "created_at": "2024-04-05T16:26:34Z",

            "updated_at": "2024-04-05T16:26:34Z"

        }

    ],

    "source_additional_info": null, 

    "nr_due_by": null,

    "nr_escalated": false

}Show Less

답변 1개
  1. 2024년 5월 11일 오후 1:29

    Hi @titan blazers,

    Hope you are doing well,

    To extract the attachment_url value from the JSON string in Apex, you can use the JSON.deserializeUntyped method to convert the JSON string into a map, and then traverse the map to access the desired attribute. Here's how you can do it:

    String jsonString = '{ "ticket_cc_emails": [], "fr_escalated": false, "spam": false, "email_config_id": null, "group_id": 72000320936, "priority": 1, "requester_id": 72012786963, "responder_id": 72010582985, "source": 2, "status": 5, "subject": "Updated ACP Capacity", "id": 54942, "type": "SE Scheduler", "due_by": "2024-04-11T12:27:12Z", "fr_due_by": "2024-04-06T13:57:12Z", "is_escalated": false, "custom_fields": { "cf_class75953": "Configuration", "cf_subclass650579": "Availability", "cf_class832453": null, "cf_subclass605425": null, "cf_billing_subclass": null, "cf_test": null, "cf_connect_type": null, "cf_connect_subtype": null, "cf_subclass": null, "cf_priority": false }, "created_at": "2024-04-05T16:26:34Z", "updated_at": "2024-04-08T13:24:00Z", "tags": [ "SP Post Launch Adoption" ], "attachments": [ { "id": 72092546835, "content_type": "image/png", "size": 106213, "name": "Screenshot 2024-03-22 134756 (1).png", "attachment_url": "https://s3.amazonaws.com", "created_at": "2024-04-05T16:26:34Z", "updated_at": "2024-04-05T16:26:34Z" } ], "source_additional_info": null, "nr_due_by": null, "nr_escalated": false }';

     

    // Deserialize the JSON string into a map

    Map<String, Object> jsonMap = (Map<String, Object>) JSON.deserializeUntyped(jsonString);

     

    // Extract the 'attachments' array from the map

    List<Object> attachments = (List<Object>) jsonMap.get('attachments');

     

    // Check if attachments exist

    if (attachments != null && !attachments.isEmpty()) {

        // Extract the first attachment object

        Map<String, Object> attachment = (Map<String, Object>) attachments[0];

        

        // Extract the 'attachment_url' attribute from the attachment object

        String attachmentUrl = (String) attachment.get('attachment_url');

        

        // Print or use the attachment URL as needed

        System.debug('Attachment URL: ' + attachmentUrl);

    } else {

        // Handle case where there are no attachments

        System.debug('No attachments found.');

    }

     

    This code snippet assumes that your JSON string has been stored in the jsonString variable. It first deserializes the JSON string into a map, then extracts the attachments array from the map. It checks if the attachments array is not null and not empty. If there are attachments, it extracts the first attachment object and then extracts the attachment_url attribute from it. Finally, it prints or uses the attachment URL as needed.

     

    Hope this content will provide more understanding to you,

    Thanks!

0/9000