+ Start a Discussion
QingsongQingsong 

How to get field Id by Apex Class

I know how to use apex to get Sobject prefix Id:
Schema.DescribeSObjectResult d = Schema.SObjectType.Reference_Opportunity__c ;
system.debug('======DescribeSObjectResult'+d.getKeyPrefix());

I have code:
<apex:commandButton value="Create new Reference Opportunity" onClick="window.open('https://cs4.salesforce.com/a0a/e?CF00NP0000000aoFc={!PXRB__c.Name}&CF00NP0000000aoFc_lkid={!PXRB__c.Id}&saveURL=%2F{!PXRB__c.Id}&retURL=%2F{!PXRB__c.Id}');"/>

this link will open an new screen for create new record under Reference_Opportunity__c, because this is child object of PXRB__C, so the default value will be put into PXRB_No__c field.

the "a0a" is the prefix of Sobject Reference_Opportunity__c
the "CF00NP0000000aoFc" is ID of the PXRB_No__c field in Sobject Reference_Opportunity__c

When we deploy into production, the SObject as well as the field Id will be different, so my question is how to get PXRB_No__c Id from production by using apex code.

Thanks.

Qingsong

kiranmutturukiranmutturu

if your sandbox is a full sandbox then there is no problem... by using the static id there....

QingsongQingsong

we have full sandbox but this is not works for me because we had new fields and object created in sandbox after we refresh from production, when we deploy the new fields and object from sandbox to production, the Id was changed.

 

I had workwround -- but it need to be update on every sandbox refresh

public string getRefOppsId(){
		   	Schema.DescribeSObjectResult d = Schema.SObjectType.Reference_Opportunity__c;
			system.debug('======DescribeSObjectResult'+d.getKeyPrefix());
			if (d.getKeyPrefix()=='a0a'){
				//Sandbox Id
				return 'a0Q';
			}
			else {
				//Production Id
				Schema.DescribeSObjectResult d1 = Schema.SObjectType.PXRB_Opportunities__c;
				return d1.getKeyPrefix();
			}
	   }

	   public string getRefOppsFieldId(){
		   	Schema.DescribeSObjectResult d = Schema.SObjectType.Reference_Opportunity__c;
			system.debug('======DescribeSObjectResult'+d.getKeyPrefix());
			if (d.getKeyPrefix()=='a0a'){
				//Sandbox Id
				return 'CF00N500000025aBy';
			}
			else {
				//Production Id
				return 'CF00N500000025aBy';
			}
	   }

 

<apex:commandButton style="align:center;" value="Create new SFDC Opportunity" onClick="window.open('/{!RefOppsId}/e?{!RefOppsFieldId}={!PXRB__c.Name}&{!RefOppsFieldId}_lkid={!PXRB__c.Id}&saveURL=%2F{!PXRB__c.Id}&retURL=%2F{!PXRB__c.Id}');"/>

 this will not required manual change the ID when deploy the changes from sandbox to production, only need update the code whenever sandbox get refreshed.

QingsongQingsong

thank you for the idea, this is basically i did in apex class with the static id, determinate the difference between sandbox and production.

 

Qingsong

kiranmutturukiranmutturu

then y can't u create a simple vf page to open a pop up for this scenario...

bob_buzzardbob_buzzard

We tend to use custom settings rather than static values for this - as they can be maintained in production without having to deploy code changes.