Hello. I would like to generate document in DocGen with such data structure:
Opportunity -> CustomProducts -> CustomServices -> Vendors
So opportunity has many customProducts, and each customProduct has many custom services and so on.
I could do this in DocGen UI, but user wants to select vendor, and depends on the vendor we should have only customProducts and customServices related to particular vendor. This kind of filter I cannot do in DocGen UI.
So i decided to write apex code, which will do that filter and give me records by some field value on opportunity. I took example from here:
And, first of all, this example with mistake (there should be OpportunityLineItem, not Opportunity, as there is no connection between Product2 and Opportunity).
Secondly, it is very basic, there is no proper information, how to build such structure. There is no documentation i could find, for example, if i add object names to this method:
public Set<string> getChildRelationships(string objectName) { // Return a set of object names corresponding to the children for the objectName specified // In this example, there are no child objects. return new Set<string>(); }
How can i connect or use this (lets say i returned there Set of CustomProduct, CustomService strings) in method with SOQL query? I dont understand, how i should connect each related record to its parent in this method!
public Loop.ExternalData.QueryResultSet query(Loop.ExternalData.QueryRequestInfo requestInfo) { // Provide data for each object in requestInfo.GetObjectNames() // Assume that the Document Package is run from the Opportunity Id opportunityId = requestInfo.RecordId; // Declare the variable to be returned. Loop.ExternalData.QueryResultSet queryResultSet = new Loop.ExternalData.QueryResultSet(); // Loop through all objects requested. The QueryResultSet instance returned needs to contain a QueryResult instance for each object requested. for (string objectName : requestInfo.GetObjectNames()) { // Declare fields list for QueryResult instance List<string> fields = new List<string>(); // Declare query result to add to QueryResultSet instance Loop.ExternalData.QueryResult queryResult = new Loop.ExternalData.QueryResult(objectName, fields); // set up fields list and query to get data for QueryResult instance if (objectName == opportunityProductObjectName) { List<string> queryFields = new List<string>(); // Get list of fields from the Opportunity and Product objects Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Opportunity.Fields.getMap(); for (string fieldKey : fieldMap.keySet()) { fields.add(fieldKey); queryFields.add(fieldKey); } fieldMap = Schema.SObjectType.Product2.Fields.getMap(); for (string fieldKey : fieldMap.keySet()) { fields.add(productFieldPrefix + fieldKey); queryFields.add('Product2.' + fieldKey); } // Since we added fields to the list, update the QueryResult instance queryResult = new Loop.ExternalData.QueryResult(objectName, fields); string query = string.format( 'SELECT {0} FROM Opportunity WHERE Id = :opportunityId', new List<string>{ string.join(queryFields, ',') } ); // for each row of data returned by the query for (SObject record : Database.query(query)) { // Store the values (as strings) from the record in the same order of the fields defined in the QueryResult instance List<string> recordValues = new List<string>(); for (string field : queryFields) { recordValues.add(getFieldValue(record, field)); } // Add the values to the QueryResult instance rows queryResult.rows.add(recordValues); } } // Add the QueryResult instance to the QueryResultSet instance // This needs to be done for every object specified in requestInfo.GetObjectNames(), regardless of whether data is required for the request. queryResultSet.add(queryResult); } return queryResultSet; }
Can you please provide some code example in my situation?! Or maybe better solution.
Thanks.