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
Brad007Brad007 

Help with apex code reference

 I have a custome object built  names Config_Object__c from which i need to access the values to my scheduler class.

 

Here is the code. please suggest if you have any ideas.

 

My Config_Object__C has fields 

1.HFSurveyCode__c="HF1Mrov'

 

2.Survey_Date__c

 

3.SOQL_query="Select o.Id, o.Account.Id, o.AccountId, o.Account.Survey_Opp_Id__c, o.Account.Survey_Dt__c,                                                        o.Account.Survey_Code__c, o.Account.Name 

                                      From Opportunity o 

                                      where o.StageName='Active' 

                                      AND o.Placmnt_Start_Date_Arrive__c < Last_N_days :30 

                                      AND o.Placmnt_Start_Date_Arrive__c = Last_N_days :60"

 

 

global class HF_Survey_OneMonth Implements Schedulable 
{
    
    global void execute(SchedulableContext sc)
    {
        sendSurvey();
    }
    
    
    
    public void SendSurvey()
    {
     //string query = Config_Object__c.SOQL_Query__c;
    ERROR here: Entities should be explictly specified in SOSL call in APEX.
 I want to reference my SOQL_query from custom object
         List<Opportunity> oppList =[ Config_Object__c.SOQL_Query__c];                               
                    
             List<Account> accList = new List<Account>();
             
                   for(Opportunity opp:oppList)
                     {
                        if(opp.AccountId!= null)
                        {
                          Account acc = opp.Account;
                          if(acc.Survey_Code__c != String.valueOf(Config_Object__c.HFSurveyCode__c))
                           {
                             string oppcode= opp.Id;
                             acc.Survey_Code__c = String.valueOf(Config_Object__c.HFSurveyCode__c);
                             acc.Survey_Dt__c = Config_Object__c.Survey_Date__c;
                             acc.Survey_Opp_Id__c = oppcode.subString(0,15);
                           }                  
                        
                       else if(acc.Survey_Opp_Id__c != opp.Id)
                          {
                             string oppcode= opp.Id;
                             acc.Survey_Code__c = String.valueOf(Config_Object__c.HFSurveyCode__c);
                             acc.Survey_Dt__c = Config_Object__c.Survey_Date__c;
                             acc.Survey_Opp_Id__c = oppcode.subString(0,15);
                          }
                         accList.add(acc); 
                        }
                     }
                update accList;          
              
     }
  }

 

 

 

Please suggest what should be done.

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

This is a wierd request, and potentially a bad thing to do.  But, that's your issue.  :-)

 

Your code isn't querying for the values of the Config_Object__c anywhere.   This is a custom object, and there can obviously be multiple records in that object, each with different stored queries and query criteria.  So, how are you going to determine which Config_Object__c record you want.  Again, your issue, I'll assume you want the most recently created one.

 

 

global class HF_Survey_OneMonth Implements Schedulable 
{
    
    global void execute(SchedulableContext sc)
    {
        sendSurvey();
    }
    
    
    public void SendSurvey()
    {
        // Get the most recently created config object.
        Config_Object__c config = [select HFSurveyCode__c, Survey_Date__c, SOQL_Query__c from Config_Object__c order by CreatedDate Desc limit 1];

        List<Opportunity> oppList = Database.query(config.SOQL_Query__c);                     
                    
             List<Account> accList = new List<Account>();
             
                   for(Opportunity opp:oppList)
                     {
                        if(opp.AccountId!= null)
                        {
                          Account acc = opp.Account;

// Why are you doing this IF statement?  You do the same things for both cases.
                          if(acc.Survey_Code__c != String.valueOf(config.HFSurveyCode__c))   // refer to the config object queried above.
                           {
                             string oppcode= opp.Id;
                             acc.Survey_Code__c = String.valueOf(config.HFSurveyCode__c);  // Isn't this redundant?
                             acc.Survey_Dt__c = config.Survey_Date__c;
                             acc.Survey_Opp_Id__c = oppcode.subString(0,15);  // Don't do the substring.  SF takes care of correctly handling Id's
                           }                  
                        
                       else if(acc.Survey_Opp_Id__c != opp.Id)
                          {
                             string oppcode= opp.Id;
                             acc.Survey_Code__c = String.valueOf(config.HFSurveyCode__c);
                             acc.Survey_Dt__c = config.Survey_Date__c;
                             acc.Survey_Opp_Id__c = oppcode.subString(0,15);
                          }
                         accList.add(acc); 
                        }
                     }
                update accList;          
              
     }
  }

 

 

However:

 

1. You should add a bunch of Try/Catch around several things here because the potential for bad queries is vast.

 

2. What happens to your code if the query stored in the Config object is, literally:   "Select Peanuts from classic comix from the 1950s".  Or, even better, "#T9834ugrewn3(*9r2", or even null.  So, if you're going to let users store queries, you've better be sure they are valid queries.  I'd think it's better to store the values that you want the user to be able to change and then build the query string yourself.  At least then you can use validation rules to check the values, and you'll always have the fields that you require.

 

Best, Steve. 

 

 

 

All Answers

cloudcodercloudcoder

Few things going on here:

 

1. You are going to have problems as strings within Apex are delimited with single quotes and the value in your custom field is also using them. try escaping single quotes in your query

 

2. Because you are dynamically creating the query string, you are using SOSL. SOSL rules are different and you should check out the docs on how to use them.

 

 

Brad007Brad007

I basically dont have quotes, I can remove them.

The requirement is i should be able to enter any query in the SOQL_query filed and my scheduler class should be able to pick it up to my opportunity object.

 

Can you help me how to reference the code here.

 

My Config_Object__C has fields 

1.HFSurveyCode__c="HF1Mrov'

 

2.(Date type)Survey_Date__c

 

3.(Text Area data type) SOQL_query=Select o.Id, o.Account.Id, o.AccountId, o.Account.Survey_Opp_Id__c, o.Account.Survey_Dt__c,                                                        o.Account.Survey_Code__c, o.Account.Name 

                                      From Opportunity o 

                                      where o.StageName='Active' 

                                      AND o.Placmnt_Start_Date_Arrive__c < Last_N_days :30 

                                      AND o.Placmnt_Start_Date_Arrive__c = Last_N_days :60

 

 

global class HF_Survey_OneMonth Implements Schedulable 
{
    
    global void execute(SchedulableContext sc)
    {
        sendSurvey();
    }
    
    
    
    public void SendSurvey()
    {
     //string query = Config_Object__c.SOQL_Query__c;
    ERROR here: Entities should be explictly specified in SOSL call in APEX.
 I want to reference my SOQL_query from custom object
         List<Opportunity> oppList =[ Config_Object__c.SOQL_Query__c];                               
                    
             List<Account> accList = new List<Account>();
             
                   for(Opportunity opp:oppList)
                     {
                        if(opp.AccountId!= null)
                        {
                          Account acc = opp.Account;
                          if(acc.Survey_Code__c != String.valueOf(Config_Object__c.HFSurveyCode__c))
                           {
                             string oppcode= opp.Id;
                             acc.Survey_Code__c = String.valueOf(Config_Object__c.HFSurveyCode__c);
                             acc.Survey_Dt__c = Config_Object__c.Survey_Date__c;
                             acc.Survey_Opp_Id__c = oppcode.subString(0,15);
                           }                  
                        
                       else if(acc.Survey_Opp_Id__c != opp.Id)
                          {
                             string oppcode= opp.Id;
                             acc.Survey_Code__c = String.valueOf(Config_Object__c.HFSurveyCode__c);
                             acc.Survey_Dt__c = Config_Object__c.Survey_Date__c;
                             acc.Survey_Opp_Id__c = oppcode.subString(0,15);
                          }
                         accList.add(acc); 
                        }
                     }
                update accList;          
              
     }
  }

 

SteveBowerSteveBower

This is a wierd request, and potentially a bad thing to do.  But, that's your issue.  :-)

 

Your code isn't querying for the values of the Config_Object__c anywhere.   This is a custom object, and there can obviously be multiple records in that object, each with different stored queries and query criteria.  So, how are you going to determine which Config_Object__c record you want.  Again, your issue, I'll assume you want the most recently created one.

 

 

global class HF_Survey_OneMonth Implements Schedulable 
{
    
    global void execute(SchedulableContext sc)
    {
        sendSurvey();
    }
    
    
    public void SendSurvey()
    {
        // Get the most recently created config object.
        Config_Object__c config = [select HFSurveyCode__c, Survey_Date__c, SOQL_Query__c from Config_Object__c order by CreatedDate Desc limit 1];

        List<Opportunity> oppList = Database.query(config.SOQL_Query__c);                     
                    
             List<Account> accList = new List<Account>();
             
                   for(Opportunity opp:oppList)
                     {
                        if(opp.AccountId!= null)
                        {
                          Account acc = opp.Account;

// Why are you doing this IF statement?  You do the same things for both cases.
                          if(acc.Survey_Code__c != String.valueOf(config.HFSurveyCode__c))   // refer to the config object queried above.
                           {
                             string oppcode= opp.Id;
                             acc.Survey_Code__c = String.valueOf(config.HFSurveyCode__c);  // Isn't this redundant?
                             acc.Survey_Dt__c = config.Survey_Date__c;
                             acc.Survey_Opp_Id__c = oppcode.subString(0,15);  // Don't do the substring.  SF takes care of correctly handling Id's
                           }                  
                        
                       else if(acc.Survey_Opp_Id__c != opp.Id)
                          {
                             string oppcode= opp.Id;
                             acc.Survey_Code__c = String.valueOf(config.HFSurveyCode__c);
                             acc.Survey_Dt__c = config.Survey_Date__c;
                             acc.Survey_Opp_Id__c = oppcode.subString(0,15);
                          }
                         accList.add(acc); 
                        }
                     }
                update accList;          
              
     }
  }

 

 

However:

 

1. You should add a bunch of Try/Catch around several things here because the potential for bad queries is vast.

 

2. What happens to your code if the query stored in the Config object is, literally:   "Select Peanuts from classic comix from the 1950s".  Or, even better, "#T9834ugrewn3(*9r2", or even null.  So, if you're going to let users store queries, you've better be sure they are valid queries.  I'd think it's better to store the values that you want the user to be able to change and then build the query string yourself.  At least then you can use validation rules to check the values, and you'll always have the fields that you require.

 

Best, Steve. 

 

 

 

This was selected as the best answer