function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Richa_LearningRicha_Learning 

Apex Class not returning query data

Hi,

I am calling my VF page using button. This is my controller. When i click on the button , i want those fields to be copied(pre-populated)  that are not in Cloneaccount__c (Custom settings).
But when i click on button it copying every field.  Could you please help to copy only those fields that are not in Cloneaccount_c (custom settings)
 
public class myClonedRecordEditPageController {
 
    private final Account o;
    private Account objNewAccount;
    Map<String, Schema.SObjectField> mapaccfields;
    set<String> setExfield = new set<String>();
      private string qstr = '';
    public myClonedRecordEditPageController(ApexPages.StandardController stdController) {
        this.o = (Account)stdController.getRecord();
    }
     
  
    public PageReference autoRun() {
 
        String AccountID = ApexPages.currentPage().getParameters().get('id');
 
        if (AccountID == null) {
          
            return null;
        }
        
        if(AccountID != null)
        { 
                mapaccfields = Schema.SObjectType.Account.fields.getMap() ;
                 List<Cloneaccount__c > Exfield = Cloneaccount__c.getall().values();
                 
            for( Cloneaccount__c excludedField: Exfield){
                setExfield.add(excludedField.Name.toLowerAccount());
            }
          
            for(String s: mapaccfields.keyset()){
                if(!setExfield.contains(s)){
                    if(qstr == ''){
                        qstr += s;
                    }else{
                        qstr += ',' + s;
                    }
                }
            }
 objnewAccount = Database.Query('Select ' + qstr + ' From Account where id= \'' + String.escapeSingleQuotes(AccountID) + '\''); 
     system.debug('hello'+ objnewAccount);
       PageReference pageRef = new PageReference('/' + AccountID+'/e');
      pageRef.setRedirect(true);
      return pageRef;
 
    }
 return null;
}
}

Thanks so much!
Pankaj_GanwaniPankaj_Ganwani
Hi Richa,

Just try to debug set and map keyset and check if field present in both the collections are of same case i.e. either upper or lower.
Richa_LearningRicha_Learning
Yes, both are in lowercase
Pankaj_GanwaniPankaj_Ganwani
Hi,

I got your problem. In  page reference method you are passing the Id of the existing account instead(not objnewaccount), which is why you are being redirected to the standard edit page of the Account. 
Richa_LearningRicha_Learning
Hey - i tried this before but when i click on the button , i get an error: "URL No Longer Exists​"
objnewAccount = Database.Query('Select ' + qstr + ' From Account where id= \'' + String.escapeSingleQuotes(AccountID) + '\''); 
     system.debug('hello'+ objnewAccount);
       PageReference pageRef = new PageReference('/' + objnewAccount+'/e');
      pageRef.setRedirect(true);
      return pageRef;
 
    }
Pankaj_GanwaniPankaj_Ganwani
Hi,

This error is coming because your record is not saved yet and you are trying to redirect to the edit page of the record. I believe you want to clone the record. To do so, you will either have to create vf page or use the concept of url hacking.
Richa_LearningRicha_Learning
Yes, i am creating a custom button that should pre-populate few fields in edit mode. I cant use url hacking because i need to pre=populate  rich text area fields also and that is not supported by url hacking.
Richa_LearningRicha_Learning
Also, even if i don't redirect to edit page - still all fiedls are copied when i use AccountID in page reference and when i use objnewaccount then "URL no longer exists"
Pankaj_GanwaniPankaj_Ganwani
All fields are populated because you are using the existing Record Id in URL. For instance if you go to your salesforce org and type https://yourinstance/recordId/e, this will redirect you to the edit page.
Richa_LearningRicha_Learning

Pankaj - as you suggested i did create a VF page and updated my class. I am using custom settings (Cloneaccounts__c ) to inlcude field API name to so they shoud not be cloned. there are few fields where api name is more than 40 characters and i am not able to save them. I created a field "Fieldname" on custom settings so i can save fields api name in it. But how do i modify this controller to read the 

value of CloneAccounts__C field "Fieldname"


Please help

public with sharing class CloneaccController {

    public account objaccount {get;set;}        
    public string accountID;                        
   
    private account objNewaccount;
    private string queryString = '';
    public string strPrevCurrency {get;set;}
    Map<String, Schema.SObjectField> mapaccountFields;
    set<String> setexfields = new set<String>();

    public CloneaccController(ApexPages.StandardController controller) 
    {
      
     
     
          

        accountID = ApexPages.currentPage().getParameters().get('id');
        
        
        if(accountID != null)
        { 
                mapaccountFields = Schema.SObjectType.account.fields.getMap() ;
                 List<Cloneaccounts__c > lstexfields = Cloneaccounts__c.getall().values();
                 
            for( Cloneaccounts__c exfield: lstexfields){
                setexfields.add(exfield.Name.toLoweraccount());
            }
          
            for(String s: mapaccountFields.keyset()){
                if(!setexfields.contains(s)){
                    if(queryString == ''){
                        queryString += s;
                    }else{
                        queryString += ',' + s;
                    }
                }
            }
                
                
                objnewaccount = Database.Query('Select ' + queryString + ' From account where id= \'' + String.escapeSingleQuotes(accountID) + '\'');   
                  
       objaccount = objNewaccount.clone(false,true,false,false);
             
        } 
 
         }
        
         public PageReference save()
    {
         insert objaccount;
        return new PageReference('/'+objaccount.id);
        
    }  
           
    }
Pankaj_GanwaniPankaj_Ganwani
Hi Richa,

Bind objNewAccount variable with the page instead of objaccount and on click of save button perform dml on objNewAccount variable.