Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

Post and update person image with custom services in x++

(1) ShareShare
ReportReport
Posted on by 16
I am working on custom services for updates and inserting new person's images.
 
The requirement is we are integrated with a mobile app that shows the person's image and if we need to update we can update or insert a new image for the person.
 
is it possible with a custom service if yes please share the code for it.
 
or if any idea how can we achieve it.
  • Martin Dráb Profile Picture
    Martin Dráb 230,371 Most Valuable Professional on at
    Post and update person image with custom services in x++
    Your method expects a string with the encoded in Base64. Please check out what JSON object you sent and whether the _image property really contains a Base64-encoded string. I don't know what Postman generates from your parameters. You seem to assume that it does the encoding for you but the error messages suggests otherwise. Or simply provide the right JSON object (raw input).
  • Suggested answer
    Ghzanfar Abbas Profile Picture
    Ghzanfar Abbas 16 on at
    Post and update person image with custom services in x++
    Hi Martin
     
    I have done the code as you suggested but am facing an error at Postman when I am sending a post request 
     
    My code is
     
     
    public void uploadEmployeeImage(str _image, HcmPersonRecId _personId)
        {
            HcmPersonImage personImage;
     
            ttsbegin;   
            container binaryData = BinData::loadFromBase64(_image);
            personImage = HcmPersonImage::findByPerson(_personId, true);
            if(!personImage)
            {
                personImage.Person = _personId;
                personImage.Image = binaryData;
                personImage.insert();
            }
            else
            {
                personImage.Image = binaryData;
                personImage.update();
            }
            ttscommit;     
        }
     
     
    And the Postman error
     
     
     
    Dear Martin, please tell me where I have made a mistake am waiting for your reply thanks.
     
     
     
     
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,371 Most Valuable Professional on at
    Post and update person image with custom services in x++
    Your export method returns the image as string (with binary data encoded to string with Base64).
     
    If you want the same approach on import, the type of the parameter must be string, not Bitmap. You'll then use something like BinData::loadFromBase64() to convert the string to binary data.
  • Suggested answer
    Ghzanfar Abbas Profile Picture
    Ghzanfar Abbas 16 on at
    Post and update person image with custom services in x++
    Yes, I know about the service and created the service class with the accepting a string parameter method for a return person image, and it's working fine with Postman.
     
    My service operation method code is
     
    public str getEmployeeImage(HcmPersonId _Id)
        {
            HcmPersonImage personImage;
            bindata bin = new bindata();
            str content;
            container image;
            select personImage where personImage.Person == HcmWorker::findByPersonnelNumber(_Id).Person;
            image = personImage.Image;
            bin.setData(image);
            content=bin.base64Encode();
            return content;
        }
     
    Now I want to upload or update the image from Postman or the mobile app
     
    here is my code for uplaod image
     
        public void uploadEmployeeImage(Bitmap _image, HcmPersonRecId _personId)
        {
            HcmPersonImage personImage;
           
            try
            {
                ttsbegin;
               
                 personImage = HcmPersonImage::findByPerson(_personId, true);
                if(!personImage)
                {
                    personImage.Person = _personId;
                    personImage.Image = _image;
                    personImage.insert();
                }
                else
                {
                    personImage.Image = _image;
                    personImage.update();
                }
                ttscommit;
            }
            catch
            {
                ttsabort;
            }
        }
     
     
    I am not accepting image parameter (value is empty) from Postman with this request
     
    I hope you understand my issue please tell me where I am making mistakes in the code.
     
     
     
     
     
  • Martin Dráb Profile Picture
    Martin Dráb 230,371 Most Valuable Professional on at
    Post and update person image with custom services in x++
    But it's not clear what you want us to show you.
     
    Please answer: do you know how to create a custom service accepting a single string parameter?
     
    If not, you're looking for basics of custom services; you're not yet in a situation to implement anything about images. That will come later.
     
    If you know how to create a custom services, giving you examples of that would be useless for you and a waste of time for us. It would mean that you're actually struggling with something else, but it's not clear what it is unless you write it down.
  • Suggested answer
    Ghzanfar Abbas Profile Picture
    Ghzanfar Abbas 16 on at
    Post and update person image with custom services in x++
    Hi Martin,
     
    I am very grateful to you for your quick response.
    sorry for the misunderstanding I do not want to develop the whole thing for myself. I just want if you any any articles or code examples to share with me for help.
     
     
  • Martin Dráb Profile Picture
    Martin Dráb 230,371 Most Valuable Professional on at
    Post and update person image with custom services in x++
    Aha, you want me to develop the whole thing for you. Sorry, I'm not going to do it. I'm willing to answer your question but not doing your work for you. I don't even have time for that.
     
    Maybe you should simply use the data entity instead of trying to implement a custom solution for the same thing, if you're unable to do it.
     
    And if you insist, either use my suggest or design a different solution, e.g. use a string parameter with the image encoded with Base64. It'll likely be easier for you than dealing with binary data directly.

    Choose the solution that suits you the best and try to implement. If you have no idea how to do it, e.g. how to use the data entity or how to create a trivial custom service, check out documentation and ask questions here if needed.

    Then start the implementation. For example, if you decided to use a custom service, create a simple service just accepting a parameter (without processing the data for now) and test that you can call it. Ask questions if you get into problems.
  • Suggested answer
    Ghzanfar Abbas Profile Picture
    Ghzanfar Abbas 16 on at
    Post and update person image with custom services in x++
    I want you to share with me the code how to upload image through custom service because I have no idea how to code.
    Or share the article related it thanks.
  • Martin Dráb Profile Picture
    Martin Dráb 230,371 Most Valuable Professional on at
    Post and update person image with custom services in x++
    What do you want the example to show? Are you saying that you tried to follow my suggestion ("For example, you could have a service operation accepting a party number and System.Byte[] object") and you ran into a problem?
  • Suggested answer
    Ghzanfar Abbas Profile Picture
    Ghzanfar Abbas 16 on at
    Post and update person image with custom services in x++
    Thanks Martin for quick response.
     
    Can you share code examples for the custom service?
     
    I am your response Martin.

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,642 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,371 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans