0

I'm new to coding & Salesforce. I'm trying to create a custom object. The code is below. Will I be able to use my custom object 'OrgPackages__c' right under the last line of this code and fill it in with values from a query result? My custom field 'Name' will be populated with the result from 'SELECT Name FROM Publisher'. After populating my custom field 'Name' with the names from the query, I would want to retrieve the result from my custom object.

    //custom object
    MetadataService.MetadataPort metadataservice = new MetadataService.MetadataPort();
    metadataservice .SessionHeader = new MetadataService.SessionHeader_element();
    metadataservice .SessionHeader.sessionId = UserInfo.getSessionId();

    List<MetadataService.Metadata> fields = new List<MetadataService.Metadata>();
    MetadataService.CustomObject customobject = new MetadataService.CustomObject();
    customobject.fullName = 'OrgPackages__c';
    customobject.label = 'Org Packages';
    customobject.pluralLabel = 'Org Packages';

    fields.add(customobject);
    metadataservice.createMetadata(fields);


    //custom field
    MetadataService.CustomField customField = new MetadataService.CustomField();
    customField.fullName = 'Name';
    customField.label = 'Name';
    customField.defaultvalue = 'false';
    customField.type_x = 'Text';
    fields.add(customField);
    metadataservice.createMetadata(fields);

Edit starts here:

Hi @david-reed . You're correct. I'm trying to combine values from multiple queries if it's possible. My class had to return the packages installed in the org. It should include the package name, version, license, and the dates installed/updated. However, I need to return something that looks like below. To get those details, I will be needing Publisher and PackageLicense with their NamespacePrefix to cross-reference the results from 2 separate queries. The name and version of the package will be retrieved from 'Publisher' while the license status and the dates will come from 'PackageLicense'. I was told to search for Metadata as it might help me with this.

packages:[
        {
          package:"Package1",
          version:"1.12",
          license:"Free",
          date_installed: "",
          date_updated: ""
        },
        {
          package:"Package2",
          version:"1.30",
          license:"Free",
          date_installed: "",
            date_updated: ""
        },
1
  • Based on your question history, I'm thinking this may be an X-Y problem. It sounds like you are trying to create a data structure that combines values data points from multiple queries. It is very unlikely that dynamically creating a custom object is a good solution to that problem. An Apex wrapper class is more likely to be the answer, but none of your questions have made very clear what exactly you're aiming at. More details are almost always helpful in obtaining a specific, high quality answer.
    – David Reed
    Jul 31, 2018 at 3:14

1 Answer 1

1

Your objective involves no Custom Metadata Types, Custom Objects, or metadata deployments at all. It does not appear that you need to persist any information to the database.

You need to create an Apex wrapper class that contains public properties package, version, license, date_installed, and date_updated. Your code should source the data you are interested in and perform any correlation or manipulation you need to do in memory.

You would create a List<MyWrapperClass> and iteratively add an instance of MyWrapperClass to it for each installed package, populating the data points based on the information you queried. Your REST service method can then return the list embedded in a Map with a single key (packages), which Salesforce will automatically marshal into JSON and return to the web service's caller.

Your service method's signature would be something like

@HttpGet
global static Map<String, List<MyWrapperClass>> getInstalledPackageInformation() {
    List<MyWrapperClass> wrappers = new List<MyWrapperClass>();

    // Populate the data you want.

    return new Map<String, List<MyWrapperClass>>{ 'packages' => wrappers };
}
0

You must log in to answer this question.

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