+ Start a Discussion
dlCamelotdlCamelot 

Transform String Return to Row Return

I'm trying to create a wrapper for my row return but I somehow need 2 classes to accomplish this.  I've only successfully written one of the classes which returns a string.  How do I take this new query - and make it into a wrapper?

New dnbData2 query:
dnbData2 = [SELECT companyID, Name, DunsNumber,City,State,Country,Zip FROM DatacloudDandBCompany WHERE Name IN :  nameFragments LIMIT 50];

Old Class that only returns a string:
Public class Foobar{

        @AuraEnabled 
    public static List<String> getDnBMatches(String companyName){
        List<String> dnbFinalMatchList = new List<String>();  
        List<String> dnbCompleteMatches = new List<String>(); 
        List<String> dnbPartialMatches = new List<String>();
        List<String> nameFragments = new List<String>();
        List<DatacloudDandBcompany> dnbData2 = new List<DatacloudDandBcompany>();
        Integer emptyList = 0;
        Map<id,DatacloudDandBcompany> dnbData = new Map<id,DatacloudDandBcompany>();
   
        if (String.isBlank(companyName)){
                system.debug('DC - No Company Name');
                return null;
        }else{
                for (String fragment : companyName.split(' ')){
                     nameFragments.add(fragment);
                     system.debug('DC - Fragments - ' + nameFragments);
                     } 
                     
               dnbData2 = [SELECT companyId, Name FROM DatacloudDandBCompany WHERE Name IN :('Deloitte','Consulting') LIMIT 50];
               system.debug(dnbData2);

                for (DatacloudDandBCompany c: dnbData2 ){
                    if (companyName == c.name){
                        dnbCompleteMatches.add(c.companyid);
                        system.debug('DC - dnbComplete Matches Loop');
                    }
                    dnbPartialMatches.add(c.companyid); 
                    system.debug('DC - dnbPartial Matches Loop');
                }

                if (dnbCompleteMatches.size() > emptyList) {
                    system.debug('DC - dnbComplete Matches Loop' + dnbCompleteMatches);
                    return dnbCompleteMatches;
                    

                }
                else {
                    return dnbPartialMatches;
                    

                }
         
        }
    }    
}
Please note, I have to keep the data manipulation in the new version (i.e. string fragment work, complete and partial results additions, etc.)
 
Daniel WuDaniel Wu
May you use an inner class as the method return?
jigarshahjigarshah
You can use an inner class that acts as a wrapper for the DatacloudDandBCompany records that are returned using the new query that you have highlighted.

I am assuming that dnbData2 variable is a list that holds a records of type DatacloudDandBCompany. Sample code to create an populate a wrapper class is as below. You can then use the dataCloudWrapperList to perform any further processing as required.
public class Class2{

	//Property
	public List<DatacloudWrapper> dataCloudWrapperList {get; set;}
	
	//Default Constructor
	public Class2(){
		this.dataCloudWrapperList = new List<DatacloudWrapper>();
		this.buildWrapperList();
	}
	
	//Method to populate the wrapper from query
	public void buildWrapperList(){
	
		//Populate a List of wrapper classes
		for(DatacloudDandBCompany companyRec :[SELECT companyID, 
													  Name, 
													  DunsNumber,
													  City,
													  State,
													  Country,
													  Zip 
											   FROM DatacloudDandBCompany 
											   WHERE Name IN :nameFragments 
											   LIMIT 50]){

            //Populate a list of wrapper to be used for further processing
			dataCloudWrapperList.add(
				new DatacloudWrapper(
					companyRec.companyID, 
					companyRec.Name, 
					companyRec.DunsNumber,
					companyRec.City,
					companyRec.State,
					companyRec.Country,
					companyRec.Zip 
				)
			);
		}//for ends
	}
	
	//Inner Class or a wrapper class
	public class DatacloudWrapper{
	
		//Default Constructor for wrapper
		public DatacloudWrapper(String pCompanyId, 
								String pName,
								String pDunsNumber,
								String pCity,
								String pState,
								String pCountry,
								String pState,
								String pZipCode){
								
			this.companyId = pCompanyId;
			this.name = pName;
			this.dunsNumber = pDunsNumber;
			this.city = pCity;
			this.State = pState;
			this.country = pCountry;
			this.zipCode = pZipCode;
		}
		
		//Properties that correspond to the data value of each column in your query
		public String companyId {get; set;}
		public String name {get; set;}
		public String dunsNumber {get; set;}
		public String city {get; set;}
		public String state {get; set;}
		public String country {get; set;}
		public String zipCode {get; set;}
		
	}//Inner class ends

}//Class2 ends

Additionally, you can read about using inner classes using the following link - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining.htm

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.
dlCamelotdlCamelot
Doesn't this create a wrapper for all the potential matches?  On my page, I'll be returning only ones that logic that's being applied atop the Soql set.
jigarshahjigarshah
I am not quite sure as to which set you need to populate into the wrapper. Do you need to populate the dnbCompleteMatches List as a wrapper class? In any case, just follow the steps specified below and the same code should work for you.

1. Copy the following within your Foobar class
     - Inner class - DatacloudWrapper
     - Property - dataCloudWrapperList
     - Method to populate the wrapper - buildWrapperList()
2. The code within the buildWrapperList() method, remains the same. The only thing that you would need to do is replace the SOQL query with an appropriate List that has the processed or filtered data to be populated into a list of wrapper.

In case you are unable to pass the wrapper to your UI or client side code on the page, you could use the JSON.serialize() method in Apex to convert the wrapper into a string and pass it back to the UI. Refer the following link to understand more about JSON.serialize() - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm.

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.
dlCamelotdlCamelot
Still not grasping how/where to manipulate the data within the wrapper.  Also, current code is throwing an error regarding this.buildWrapperList(); .  See code below:
Public class Class2{

    Public Class2(){
        this.dataCloudWrapperList = new List<DatacloudWrapper>();
        this.buildWrapperList();
    }
    

    @AuraEnabled 
    public List<DatacloudWrapper> dataCloudWrapperList {get;set;}


    @AuraEnabled 
    public void buildWrapperList(String companyName){
    List<String> nameFragments = new List<String>();

        if (String.isnotBlank(companyName)){
            for (String fragment : companyName.split(' ')){
                     nameFragments.add(fragment);
                     system.debug('DC - Fragments - ' + nameFragments);
                } 


            if(nameFragments != null) {
        //Populate a List of wrapper classes
                for(DatacloudDandBCompany companyRec :[SELECT companyID, 
                                                      Name, 
                                                      DunsNumber,
                                                      City,
                                                      State,
                                                      Country,
                                                      Zip 
                                               FROM DatacloudDandBCompany 
                                               WHERE Name IN :nameFragments 
                                               LIMIT 50]){

            //Populate a list of wrapper to be used for further processing
                                                dataCloudWrapperList.add(
                                                    new DatacloudWrapper(
                                                        companyRec.companyID, 
                                                        companyRec.Name, 
                                                        companyRec.DunsNumber,
                                                        companyRec.City,
                                                        companyRec.State,
                                                        companyRec.Country,
                                                        companyRec.Zip 
                                                    )           
                                                );
                }  //for ends
                return null;
        }
        return null;
    }
    return null;
   }
    
    //Inner Class or a wrapper class
    public class DatacloudWrapper{
    
        //Default Constructor for wrapper
        public DatacloudWrapper(String pCompanyId, 
                                String pName,
                                String pDunsNumber,
                                String pCity,
                                String pState,
                                String pCountry,
                                String pZipCode){
                                
            this.companyId = pCompanyId;
            this.name = pName;
            this.dunsNumber = pDunsNumber;
            this.city = pCity;
            this.State = pState;
            this.country = pCountry;
            this.zipCode = pZipCode;
        }
        
        //Properties that correspond to the data value of each column in your query
        public String companyId {get; set;}
        public String name {get; set;}
        public String dunsNumber {get; set;}
        public String city {get; set;}
        public String state {get; set;}
        public String country {get; set;}
        public String zipCode {get; set;}
        
    }//Inner class ends
}


 
jigarshahjigarshah
The error is with the way in which the buildWrapperList() method is being invoke within the constructor. The buildWrapperList() method defnition expects a paramter of String type i.e. the companyName which you are not passing.

Hence, modify line # 5 within the above code to pass the Company Name or an empty string as '' and that should fix the issue for you. The modified code for line # 5 is as below. You can replace 'Sample Company Name' with a value that is more relevant in your case.
 
public class Class2{

    public Class2(){
        this.dataCloudWrapperList = new List<DatacloudWrapper>();
        this.buildWrapperList('Sample Company Name');
    }
    
    @AuraEnabled 
    public List<DatacloudWrapper> dataCloudWrapperList {get;set;}


    @AuraEnabled 
    public List<String> buildWrapperList(String companyName){
    
		List<String> nameFragments = new List<String>();
		List<String> dnbCompleteMatches = new List<String>();
        List<String> dnbPartialMatches = new List<String>();

        if (String.isnotBlank(companyName)){
		
            for (String fragment : companyName.split(' ')){
				nameFragments.add(fragment);
				system.debug('DC - Fragments - ' + nameFragments);
            } 

            if(nameFragments != null) {
			
				//Populate a List of wrapper classes
                for(DatacloudDandBCompany companyRec :[SELECT companyID, 
                                                      Name, 
                                                      DunsNumber,
                                                      City,
                                                      State,
                                                      Country,
                                                      Zip 
                                               FROM DatacloudDandBCompany 
                                               WHERE Name IN :nameFragments 
                                               LIMIT 50]){

					//Populate a list of wrapper to be used for further processing
					dataCloudWrapperList.add(
						new DatacloudWrapper(
							companyRec.companyID, 
							companyRec.Name, 
							companyRec.DunsNumber,
							companyRec.City,
							companyRec.State,
							companyRec.Country,
							companyRec.Zip 
						));

				    //CHANGE - Logic for identifying partial and complete matching Company Ids
					if (companyName == companyRec.Name){
                        dnbCompleteMatches.add(companyRec.companyID);
                        system.debug('DC - dnbComplete Matches Loop');
                    }
                    dnbPartialMatches.add(companyRec.companyID); 
                    system.debug('DC - dnbPartial Matches Loop');
					
                } //for ends       
        }
    }
    
	//CHANGE - Return the partial and complete matching lists here
	if (!dnbCompleteMatches.isEmpty()) {
		system.debug('DC - dnbComplete Matches Loop' + dnbCompleteMatches);
		return dnbCompleteMatches;
	}
	else {
		return dnbPartialMatches;
	}
	
   }
    
    //Inner Class or a wrapper class
    public class DatacloudWrapper{
    
        //Default Constructor for wrapper
        public DatacloudWrapper(String pCompanyId, 
                                String pName,
                                String pDunsNumber,
                                String pCity,
                                String pState,
                                String pCountry,
                                String pZipCode){
                                
            this.companyId = pCompanyId;
            this.name = pName;
            this.dunsNumber = pDunsNumber;
            this.city = pCity;
            this.State = pState;
            this.country = pCountry;
            this.zipCode = pZipCode;
        }
        
        //Properties that correspond to the data value of each column in your query
        public String companyId {get; set;}
        public String name {get; set;}
        public String dunsNumber {get; set;}
        public String city {get; set;}
        public String state {get; set;}
        public String country {get; set;}
        public String zipCode {get; set;}
        
    }//Inner class ends
}

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.