0

I am using Database.query to query Opportunity records in apex class.

list<Opportunity> opplist = Database.query('select Name,createdDate, Billing_Account__c,New_Type__c From Opportunity where id =: oppId');

We are creating an App for our clients production environment. And they all have the following fields already present in there production environment. If i will also create the following fields in my developer org then this will cause the duplicate because the client environment already has the fields and after installing the package there will 2 fields for the same purpose which is not a good practice. So that's why i am developing the dynamic query such that when i install the package my code refer to there production fields dynamically. I have written the dynamic query but not able to cover through test class.

  • 3
    your question is not clear what you are trying to do. you are saying that you are creating a managed package it means you want to put your package in app exchange correct? if it is not you are trying to migrate your dev apex class to your PROD org, if that is case it is not good idea to create fields directly in PROD. you should add those fields in your dev environment. another thing I am unable to understand why you will get get duplicate field error? Please update your question with specific details. – Himanshu May 4 '16 at 7:25
  • We are creating an App for our clients production environment. And they all have the following fields already present in there production environment. If i will also create the following fields in my developer org then this will cause the duplicate because the client environment already has the fields and after installing the package there will 2 fields for the same purpose which is not a good practice. So that's why i am developing the dynamic query such that when i install the package my code refer to there production fields dynamically – RaviKant May 4 '16 at 7:33
1

You can use Schema.getGlobalDescribe to get all fields of any object and prepare your soql statement as shown below

String obj = 'Opportunity';

Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
Schema.SObjectType sobjType = gd.get(obj); 
Schema.DescribeSObjectResult describeResult = sobjType.getDescribe(); 
Map<String,Schema.SObjectField> fieldsMap = describeResult.fields.getMap(); 

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

for(Schema.SObjectField sfield : fieldsMap.values())
{
    schema.describefieldresult dfield = sfield.getDescribe();
    //Create list of fields
    fields.add(dfield.getname());
}


List<Opportunity> opplist = database.query('select ' + String.join(fields,',') + 
' from Opportunity');
  • This works in live code. But when we are calling this from test class how will it cover the dynamic query field values. Because after dynamic query we testing the values like this :- if(null != opplist[0].get('New_Con__c') && opplist[0].get('Product_Cat__c') != 'ProductNew'){ ....... How the New_Con__c field will get the value from the test class because it is not present in our org. – RaviKant May 4 '16 at 9:48
0

if code coverage is important for you then you can use Test.isRunningTest and create the required data in the class itself before the condition check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.