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
John NeilanJohn Neilan 

Trigger Question

Hi Everyone,

This is probably a pretty easy answer, but I'm just drawig a blank.  The code snippet below is designed to clone an Opportunity.  The RecClone class is set up to pull in all createable fields for the clone, since the clone function that Apex offers only pulls those fields that you specifically name.  The clone works fine, but I want to overwrite some of the clone fields with other values.  How do I pull the values from Line 3 and replace them with the values from lines 4 - 10?
 
String soql = RecClone.getCreatableFieldsSOQL('Opportunity','Id =: OppId');
                    Opportunity opp = (Opportunity)Database.query(soql);
                    Opportunity opp2 = opp.clone(false, true);
                        opp2.CloseDate = opp.Renewal_Date_Next__c;
                        opp2.OwnerId = amh.Assigned_Account_Manager__c;
                        opp2.Term__c = 12;
                        opp2.Renewal__c = 'Yes';
                        opp2.Effective_Date__c = opp.Renewal_Date_Next__c;
                        opp2.Renewed_Opportunity__c = opp.Id;
                        Opp2.StageName = 'Call Scheduled';

                insert opp2;

 
Best Answer chosen by John Neilan
BalajiRanganathanBalajiRanganathan
Renewal_Date_Next__c check if this field is creatable. the getCreatableFieldsSOQL function  might not be returning this field in the SOQL.
You might have to get this field from Trigger.newmap (Opportunity opp2 = Trigger.newmap.get(opp.id))

All Answers

mjohnson-TICmjohnson-TIC
Is opp2 is not being inserted with your updated values? It should be unless something behind the scenes is formating your data.
Simon ReparSimon Repar
Hi,

it seems to me that code should be working fine... 
John NeilanJohn Neilan
Sorry, I forgot to post the error I'm getting (guess that would help :-))

execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Opportunity.Renewal_Date_Next__c: Class.ClassAccountManagerHandoffNotes.addNotes: line 4, column 1
mjohnson-TICmjohnson-TIC
Open up apex class 
ClassAccountManagerHandoffNotes
go to line 4, and add Renewel_Date_Next__c to the select statement against Opportunity.
John NeilanJohn Neilan
Thanks.  I didn't post my AccountManagerHandoffNotes class because it was somewhat larege and the error is just in the snippet above.  The error points to line 4 above, which does not hav a Select statement.  Below is my RecClone class that pulls the fields to clone, if that helps anyone!
 
//This class is used to clone all the creatable fields on objects for use with Apex cloning.

public with sharing class RecClone{ 
 
    // Returns a dynamic SOQL statement for the whole object, includes only creatable fields since we will be inserting a cloned result of this query
    public static string getCreatableFieldsSOQL(String objectName, String whereClause){
         
        String selects = '';
         
        if (whereClause == null || whereClause == ''){ return null; }
         
        // Get a map of field name and field token
        Map<String, Schema.SObjectField> fMap = Schema.getGlobalDescribe().get(objectName.toLowerCase()).getDescribe().Fields.getMap();
        list<string> selectFields = new list<string>();
         
        if (fMap != null){
            for (Schema.SObjectField ft : fMap.values()){ // loop through all field tokens (ft)
                Schema.DescribeFieldResult fd = ft.getDescribe(); // describe each field (fd)
                if (fd.isCreateable()){ // field is creatable
                    selectFields.add(fd.getName());
                }
            }
        }
         
        if (!selectFields.isEmpty()){
            for (string s:selectFields){
                selects += s + ',';
            }
            if (selects.endsWith(',')){selects = selects.substring(0,selects.lastIndexOf(','));}
             
        }
         
        return 'SELECT ' + selects + ' FROM ' + objectName + ' WHERE ' + whereClause;
         
    }
 
}

 
mjohnson-TICmjohnson-TIC
Can you include your entire trigger? It's not the RecClone class that's causing the error. It's a reference within a "
ClassAccountManagerHandoffNotes" apex class.
BalajiRanganathanBalajiRanganathan
Renewal_Date_Next__c check if this field is creatable. the getCreatableFieldsSOQL function  might not be returning this field in the SOQL.
You might have to get this field from Trigger.newmap (Opportunity opp2 = Trigger.newmap.get(opp.id))
This was selected as the best answer
John NeilanJohn Neilan
Thanks for the assistance.  I figured it out!  The field in question is a formula field.  The RecClone class was designed to pull in all "createable" fields (line 19 of the Class).  I changed it to pull in all "Accessible" fields (changed isCreateable to isAccessible) and it now clones my Opportunity and includes the values I specified as changes.  Thanks again for the assistance!!
John NeilanJohn Neilan
Balaji, yes that was it.  I change it to isAccessible.