Mapped Picklist field values in a wrapper class in incorrect format












2














I want to retrieve the picklist values of all picklist fields from a standard object. Below part of the code



 //Wrapper class 
public class FieldMetaDataWrapper {
@AuraEnabled
public String fieldapiname {get;set;}
@AuraEnabled
public String fieldtype {get;set;}
@AuraEnabled
public Boolean isPicklist {get;set;}
@AuraEnabled
public List<String> picklistValues {get;set;}

public FieldMetaDataWrapper(String fieldapiname, String fieldtype, Boolean isPicklist, List<String> picklistValues){
this.fieldapiname = fieldapiname;
this.fieldtype = fieldtype;
this.isPicklist =isPicklist;
this.picklistValues =picklistValues;
}
}


@AuraEnabled
public static List<fieldMetaDataWrapper> getFieldMetaData(){

List<FieldMetaDataWrapper> fieldData = new List<FieldMetaDataWrapper>();
Set<String>fieldAPINames = new Set<String>();

<Code to get all the fieldAPINames of the Object>

// map of all fields in the object
Schema.DescribeSObjectResult descSobj=User.getSObjectType().getDescribe();
Map<String, Schema.SObjectField> objectFields = descSobj.fields.getMap();
Map<String, List<String>> pickValuemap = new Map<String, list<String>>();
List<String> pickvalues = new List<String>();

// iterate over the requested fields and get the describe info for each one.
for(String field : fieldAPINames){
Schema.DescribeFieldResult dr = objectFields.get(field).getDescribe();
Schema.DisplayType isPick = dr.getType();
List<Schema.PicklistEntry> pl = dr.getPickListValues();
if(String.valueOf(isPick) == 'PICKLIST'){
for (Schema.PicklistEntry a : pl){
String name = a.getValue();
pickvalues.add(name);
}
pickValuemap.put(field, pickvalues);
fieldData.add(new FieldMetaDataWrapper(field, String.valueOf(dr.getType()), true, pickValuemap.get(field)));
}else{
fieldData.add(new FieldMetaDataWrapper(field, String.valueOf(dr.getType()), false, null));
}


return fieldData;
}


But in the wrapper object, wherever the ispicklist boolean is true, all the picklist values of all fields are getting added. For example for the statecode picklist field, the JSON list comes as below. Can't get my head around as to why the entire list is being added here .



{
"fieldapiname": "statecode",
"fieldtype": "PICKLIST",
"isPicklist": true,
"picklistValues": [
<<Entire picklist values of all picklist fields of the Object>>
]
}









share|improve this question



























    2














    I want to retrieve the picklist values of all picklist fields from a standard object. Below part of the code



     //Wrapper class 
    public class FieldMetaDataWrapper {
    @AuraEnabled
    public String fieldapiname {get;set;}
    @AuraEnabled
    public String fieldtype {get;set;}
    @AuraEnabled
    public Boolean isPicklist {get;set;}
    @AuraEnabled
    public List<String> picklistValues {get;set;}

    public FieldMetaDataWrapper(String fieldapiname, String fieldtype, Boolean isPicklist, List<String> picklistValues){
    this.fieldapiname = fieldapiname;
    this.fieldtype = fieldtype;
    this.isPicklist =isPicklist;
    this.picklistValues =picklistValues;
    }
    }


    @AuraEnabled
    public static List<fieldMetaDataWrapper> getFieldMetaData(){

    List<FieldMetaDataWrapper> fieldData = new List<FieldMetaDataWrapper>();
    Set<String>fieldAPINames = new Set<String>();

    <Code to get all the fieldAPINames of the Object>

    // map of all fields in the object
    Schema.DescribeSObjectResult descSobj=User.getSObjectType().getDescribe();
    Map<String, Schema.SObjectField> objectFields = descSobj.fields.getMap();
    Map<String, List<String>> pickValuemap = new Map<String, list<String>>();
    List<String> pickvalues = new List<String>();

    // iterate over the requested fields and get the describe info for each one.
    for(String field : fieldAPINames){
    Schema.DescribeFieldResult dr = objectFields.get(field).getDescribe();
    Schema.DisplayType isPick = dr.getType();
    List<Schema.PicklistEntry> pl = dr.getPickListValues();
    if(String.valueOf(isPick) == 'PICKLIST'){
    for (Schema.PicklistEntry a : pl){
    String name = a.getValue();
    pickvalues.add(name);
    }
    pickValuemap.put(field, pickvalues);
    fieldData.add(new FieldMetaDataWrapper(field, String.valueOf(dr.getType()), true, pickValuemap.get(field)));
    }else{
    fieldData.add(new FieldMetaDataWrapper(field, String.valueOf(dr.getType()), false, null));
    }


    return fieldData;
    }


    But in the wrapper object, wherever the ispicklist boolean is true, all the picklist values of all fields are getting added. For example for the statecode picklist field, the JSON list comes as below. Can't get my head around as to why the entire list is being added here .



    {
    "fieldapiname": "statecode",
    "fieldtype": "PICKLIST",
    "isPicklist": true,
    "picklistValues": [
    <<Entire picklist values of all picklist fields of the Object>>
    ]
    }









    share|improve this question

























      2












      2








      2







      I want to retrieve the picklist values of all picklist fields from a standard object. Below part of the code



       //Wrapper class 
      public class FieldMetaDataWrapper {
      @AuraEnabled
      public String fieldapiname {get;set;}
      @AuraEnabled
      public String fieldtype {get;set;}
      @AuraEnabled
      public Boolean isPicklist {get;set;}
      @AuraEnabled
      public List<String> picklistValues {get;set;}

      public FieldMetaDataWrapper(String fieldapiname, String fieldtype, Boolean isPicklist, List<String> picklistValues){
      this.fieldapiname = fieldapiname;
      this.fieldtype = fieldtype;
      this.isPicklist =isPicklist;
      this.picklistValues =picklistValues;
      }
      }


      @AuraEnabled
      public static List<fieldMetaDataWrapper> getFieldMetaData(){

      List<FieldMetaDataWrapper> fieldData = new List<FieldMetaDataWrapper>();
      Set<String>fieldAPINames = new Set<String>();

      <Code to get all the fieldAPINames of the Object>

      // map of all fields in the object
      Schema.DescribeSObjectResult descSobj=User.getSObjectType().getDescribe();
      Map<String, Schema.SObjectField> objectFields = descSobj.fields.getMap();
      Map<String, List<String>> pickValuemap = new Map<String, list<String>>();
      List<String> pickvalues = new List<String>();

      // iterate over the requested fields and get the describe info for each one.
      for(String field : fieldAPINames){
      Schema.DescribeFieldResult dr = objectFields.get(field).getDescribe();
      Schema.DisplayType isPick = dr.getType();
      List<Schema.PicklistEntry> pl = dr.getPickListValues();
      if(String.valueOf(isPick) == 'PICKLIST'){
      for (Schema.PicklistEntry a : pl){
      String name = a.getValue();
      pickvalues.add(name);
      }
      pickValuemap.put(field, pickvalues);
      fieldData.add(new FieldMetaDataWrapper(field, String.valueOf(dr.getType()), true, pickValuemap.get(field)));
      }else{
      fieldData.add(new FieldMetaDataWrapper(field, String.valueOf(dr.getType()), false, null));
      }


      return fieldData;
      }


      But in the wrapper object, wherever the ispicklist boolean is true, all the picklist values of all fields are getting added. For example for the statecode picklist field, the JSON list comes as below. Can't get my head around as to why the entire list is being added here .



      {
      "fieldapiname": "statecode",
      "fieldtype": "PICKLIST",
      "isPicklist": true,
      "picklistValues": [
      <<Entire picklist values of all picklist fields of the Object>>
      ]
      }









      share|improve this question













      I want to retrieve the picklist values of all picklist fields from a standard object. Below part of the code



       //Wrapper class 
      public class FieldMetaDataWrapper {
      @AuraEnabled
      public String fieldapiname {get;set;}
      @AuraEnabled
      public String fieldtype {get;set;}
      @AuraEnabled
      public Boolean isPicklist {get;set;}
      @AuraEnabled
      public List<String> picklistValues {get;set;}

      public FieldMetaDataWrapper(String fieldapiname, String fieldtype, Boolean isPicklist, List<String> picklistValues){
      this.fieldapiname = fieldapiname;
      this.fieldtype = fieldtype;
      this.isPicklist =isPicklist;
      this.picklistValues =picklistValues;
      }
      }


      @AuraEnabled
      public static List<fieldMetaDataWrapper> getFieldMetaData(){

      List<FieldMetaDataWrapper> fieldData = new List<FieldMetaDataWrapper>();
      Set<String>fieldAPINames = new Set<String>();

      <Code to get all the fieldAPINames of the Object>

      // map of all fields in the object
      Schema.DescribeSObjectResult descSobj=User.getSObjectType().getDescribe();
      Map<String, Schema.SObjectField> objectFields = descSobj.fields.getMap();
      Map<String, List<String>> pickValuemap = new Map<String, list<String>>();
      List<String> pickvalues = new List<String>();

      // iterate over the requested fields and get the describe info for each one.
      for(String field : fieldAPINames){
      Schema.DescribeFieldResult dr = objectFields.get(field).getDescribe();
      Schema.DisplayType isPick = dr.getType();
      List<Schema.PicklistEntry> pl = dr.getPickListValues();
      if(String.valueOf(isPick) == 'PICKLIST'){
      for (Schema.PicklistEntry a : pl){
      String name = a.getValue();
      pickvalues.add(name);
      }
      pickValuemap.put(field, pickvalues);
      fieldData.add(new FieldMetaDataWrapper(field, String.valueOf(dr.getType()), true, pickValuemap.get(field)));
      }else{
      fieldData.add(new FieldMetaDataWrapper(field, String.valueOf(dr.getType()), false, null));
      }


      return fieldData;
      }


      But in the wrapper object, wherever the ispicklist boolean is true, all the picklist values of all fields are getting added. For example for the statecode picklist field, the JSON list comes as below. Can't get my head around as to why the entire list is being added here .



      {
      "fieldapiname": "statecode",
      "fieldtype": "PICKLIST",
      "isPicklist": true,
      "picklistValues": [
      <<Entire picklist values of all picklist fields of the Object>>
      ]
      }






      apex schema






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 9 at 16:52









      Brav

      238318




      238318






















          1 Answer
          1






          active

          oldest

          votes


















          4














          You're initializing the pickvalues only once, so you're really just updating the same list over and over again. To demonstrate this clearly, try this code:



          String a = new String { 'Hello' };
          String b = a;
          b.add('World');
          System.debug(String.join(a,' ')); // Output is "Hello World"


          You need to move this line:



          List<String> pickvalues = new List<String>();


          To here:



            if(String.valueOf(isPick) == 'PICKLIST'){
          List<String> pickvalues = new List<String>();


          This will give you a new object for each picklist.





          P.S. You can use the enum directly:



            if(isPick == Schema.DisplayType.PICKLIST){


          It's slightly longer but avoids the runtime conversion and possible typos.





          P.P.S. Don't forget about the MULTIPICKLIST and COMBOBOX varieties (if applicable).






          share|improve this answer





















          • yes.. Very silly of me to miss that.. Thank you..
            – Brav
            Dec 9 at 17:24










          • @Brav You're welcome! Always glad to help.
            – sfdcfox
            Dec 9 at 17:35











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "459"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f241908%2fmapped-picklist-field-values-in-a-wrapper-class-in-incorrect-format%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          You're initializing the pickvalues only once, so you're really just updating the same list over and over again. To demonstrate this clearly, try this code:



          String a = new String { 'Hello' };
          String b = a;
          b.add('World');
          System.debug(String.join(a,' ')); // Output is "Hello World"


          You need to move this line:



          List<String> pickvalues = new List<String>();


          To here:



            if(String.valueOf(isPick) == 'PICKLIST'){
          List<String> pickvalues = new List<String>();


          This will give you a new object for each picklist.





          P.S. You can use the enum directly:



            if(isPick == Schema.DisplayType.PICKLIST){


          It's slightly longer but avoids the runtime conversion and possible typos.





          P.P.S. Don't forget about the MULTIPICKLIST and COMBOBOX varieties (if applicable).






          share|improve this answer





















          • yes.. Very silly of me to miss that.. Thank you..
            – Brav
            Dec 9 at 17:24










          • @Brav You're welcome! Always glad to help.
            – sfdcfox
            Dec 9 at 17:35
















          4














          You're initializing the pickvalues only once, so you're really just updating the same list over and over again. To demonstrate this clearly, try this code:



          String a = new String { 'Hello' };
          String b = a;
          b.add('World');
          System.debug(String.join(a,' ')); // Output is "Hello World"


          You need to move this line:



          List<String> pickvalues = new List<String>();


          To here:



            if(String.valueOf(isPick) == 'PICKLIST'){
          List<String> pickvalues = new List<String>();


          This will give you a new object for each picklist.





          P.S. You can use the enum directly:



            if(isPick == Schema.DisplayType.PICKLIST){


          It's slightly longer but avoids the runtime conversion and possible typos.





          P.P.S. Don't forget about the MULTIPICKLIST and COMBOBOX varieties (if applicable).






          share|improve this answer





















          • yes.. Very silly of me to miss that.. Thank you..
            – Brav
            Dec 9 at 17:24










          • @Brav You're welcome! Always glad to help.
            – sfdcfox
            Dec 9 at 17:35














          4












          4








          4






          You're initializing the pickvalues only once, so you're really just updating the same list over and over again. To demonstrate this clearly, try this code:



          String a = new String { 'Hello' };
          String b = a;
          b.add('World');
          System.debug(String.join(a,' ')); // Output is "Hello World"


          You need to move this line:



          List<String> pickvalues = new List<String>();


          To here:



            if(String.valueOf(isPick) == 'PICKLIST'){
          List<String> pickvalues = new List<String>();


          This will give you a new object for each picklist.





          P.S. You can use the enum directly:



            if(isPick == Schema.DisplayType.PICKLIST){


          It's slightly longer but avoids the runtime conversion and possible typos.





          P.P.S. Don't forget about the MULTIPICKLIST and COMBOBOX varieties (if applicable).






          share|improve this answer












          You're initializing the pickvalues only once, so you're really just updating the same list over and over again. To demonstrate this clearly, try this code:



          String a = new String { 'Hello' };
          String b = a;
          b.add('World');
          System.debug(String.join(a,' ')); // Output is "Hello World"


          You need to move this line:



          List<String> pickvalues = new List<String>();


          To here:



            if(String.valueOf(isPick) == 'PICKLIST'){
          List<String> pickvalues = new List<String>();


          This will give you a new object for each picklist.





          P.S. You can use the enum directly:



            if(isPick == Schema.DisplayType.PICKLIST){


          It's slightly longer but avoids the runtime conversion and possible typos.





          P.P.S. Don't forget about the MULTIPICKLIST and COMBOBOX varieties (if applicable).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 9 at 17:16









          sfdcfox

          246k11186422




          246k11186422












          • yes.. Very silly of me to miss that.. Thank you..
            – Brav
            Dec 9 at 17:24










          • @Brav You're welcome! Always glad to help.
            – sfdcfox
            Dec 9 at 17:35


















          • yes.. Very silly of me to miss that.. Thank you..
            – Brav
            Dec 9 at 17:24










          • @Brav You're welcome! Always glad to help.
            – sfdcfox
            Dec 9 at 17:35
















          yes.. Very silly of me to miss that.. Thank you..
          – Brav
          Dec 9 at 17:24




          yes.. Very silly of me to miss that.. Thank you..
          – Brav
          Dec 9 at 17:24












          @Brav You're welcome! Always glad to help.
          – sfdcfox
          Dec 9 at 17:35




          @Brav You're welcome! Always glad to help.
          – sfdcfox
          Dec 9 at 17:35


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Salesforce Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f241908%2fmapped-picklist-field-values-in-a-wrapper-class-in-incorrect-format%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Aardman Animations

          Are they similar matrix

          “minimization” problem in Euclidean space related to orthonormal basis