+ Start a Discussion
ManuDewanManuDewan 

Fieldsets

Are Fieldsets accessible in Apex or only in Visualforce? From the Release Notes and docs, it seems only the latter but will be great to know for sure as I could REALLY use them in my code for now. I'm basically trying to get the different set of fields on the layouts for different record types but there doesn't seem to be a way in Apex to get those so was hoping to use Fieldsets.

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

Field Sets are not currently available in Apex (though its on the roadmap). For your case, could you not simply iterate through all the fields on the Opportunity record (use a decribe call to find all Opp. fields dynamically) and then copy over all non-null fields to your 'mirror' object.

Depending on the Opp Rect Type, only certain fields could have been set by the user and so you would indirectly meet your req?

I'm probably oversimplyfying your reqs, but it's worth a shot :)

All Answers

BritishBoyinDCBritishBoyinDC

Only VF at this stage, but you could create a fieldset per Record Type, and then only render the fieldset for that RecordType?

ManuDewanManuDewan

Thanks! That's the issue. I'm not tryting to render them; I need to create another object record based on Opportunity and only copy over fields based on which record type it is.

forecast_is_cloudyforecast_is_cloudy

Field Sets are not currently available in Apex (though its on the roadmap). For your case, could you not simply iterate through all the fields on the Opportunity record (use a decribe call to find all Opp. fields dynamically) and then copy over all non-null fields to your 'mirror' object.

Depending on the Opp Rect Type, only certain fields could have been set by the user and so you would indirectly meet your req?

I'm probably oversimplyfying your reqs, but it's worth a shot :)

This was selected as the best answer
ManuDewanManuDewan
Thanks! That does sound it could work! I'll give it a try and if successful, post the code here. Thanks again -Manu
ManuDewanManuDewan

And here's the code for it in case anyone needs it for future. In my case, the field names were identical, but if you have a number of different fields, it'll be good to use custom settings for mapping the field names and then using describes as here to get the same.

 

 

public with sharing class ContractCreator {
	Map<String, String> oppRecordTypes = new Map<String, String>{};
	Map<String, String> contractRecordTypes = new Map<String, String>{};
	public ContractCreator(){
		//Create a map between the Record Type Name and Ids for retrieval later
		String[] objNames = new String[]{'Opportunity', 'Contract'};
		List<RecordType> rtypes = [Select Name, Id, sObjectType From RecordType where isActive = true AND sObjectType IN :objNames];
	    	for(RecordType rt: rtypes){
	    		if(rt.sObjectType == 'Opportunity')
        			oppRecordTypes.put(rt.Id, rt.Name);
    			else
    				contractRecordTypes.put(rt.Name, rt.Id);
	    	}
	}
		
	public void createContracts(Map<Id, Opportunity> opps){
		List<Contract> contractsToCreate = new List<Contract>();
		//Get all the common fields in the two objects
		Set<String> fieldsToPopulate = new Set<String>();
		Map<String, Schema.SObjectField> contractSchemaMap = Schema.SObjectType.Contract.fields.getMap();
		Set<String> oppFields = Schema.SObjectType.Opportunity.fields.getMap().keySet();
		for(String cField: contractSchemaMap.keySet()){
			for(String oField: oppFields){
		 		if(cField == oField){
		  			fieldsToPopulate.add(cField);
		 		}
			}
		}
		for(Opportunity opp: opps.values()){
			SObject contr = new Contract();
			for(String s: fieldsToPopulate){
				Schema.DescribeFieldResult contractField = contractSchemaMap.get(s).getDescribe();
				//Populate all the non-null fields if they are createable
				if(opp.get(s) != null && contractField.isCreateable()){
					System.debug('Populating..: ' + s +' with '+opp.get(s));
					contr.put(s, opp.get(s));
				}
			}
			Contract cnt = (Contract)contr;
			cnt.recordTypeId = contractRecordTypes.get(oppRecordTypes.get(opp.recordTypeId));
			cnt.Organisation_Name__c 	= opp.AccountId;
        		cnt.Opportunity__c 		= opp.Id;
        		cnt.Contract_Name__c 		= opp.Name;
	        	
			contractsToCreate.add(cnt);
		}
		insert contractsToCreate;
	}
}

 

forecast_is_cloudyforecast_is_cloudy

This is great. Thanks so much for sharing your code with the rest of the community!