Skip to main content

Notifications

Service | Customer Service, Contact Center, Fie...
Answered

Creating incidents via the web API with attachments?

Posted on by Microsoft Employee

/I am exploring using the Customer Engagement API to add attachments to incidents created (images, pdfs, etc...) - does anyone know of any samples that show how this can be implemented?

Thanks!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Creating incidents via the web API with attachments?

    Thanks Clofly - I will validate if this is working for me today!

  • Verified answer
    cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Creating incidents via the web API with attachments?

    Hi TWPW,

    Here are code.

    1.create a case with web API.

    var entity = {};
    entity.title = "Test Case From API";
    entity["customerid_account@odata.bind"] = "/accounts(e1192f1a-628e-ea11-a811-000d3a35bad3)";
    
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl()   "/api/data/v8.2/incidents", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204) {
                var uri = this.getResponseHeader("OData-EntityId");
                var regExp = /\(([^)] )\)/;
                var matches = regExp.exec(uri);
                var newEntityId = matches[1];
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(entity));
    

    2. Create Notes Attachment using Web API.

    Image attached you need to pass it as base64 string as document body.

    var entity = {};
    entity.subject = "test from api";
    entity.filename = "testpicture.png";
    entity["objectid_incident@odata.bind"] = "/incidents(23e6ee7c-5812-e911-a96b-000d3a3638df)";
    entity.documentbody = "iVBORw0KGgo...";
    
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/annotations", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204) {
                var uri = this.getResponseHeader("OData-EntityId");
                var regExp = /\(([^)] )\)/;
                var matches = regExp.exec(uri);
                var newEntityId = matches[1];
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(entity));
    

    I have test them and they run successfully.

    Regards,

    Clofly

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,900 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,297 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans