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 Not Firing

Hello,

I created the trigger below to fire when a custom object is created to create a clone of the Opportunity related to the custom object.  I added a system.debug line on line 19, and it shows that the variable "amh2.Opportunity__r.Managed_Account_Transfer__c" is set to FALSE.  However, this checkbox field on the Opportunity is set to TRUE.  Does anyone know how to fix this?  Thanks.

public class ClassAccountManagerHandoffNotes {
    
    public void autoCreateRenewal1(Account_Manager_Handoff__c[] newAMH){

        Map<String,String> oppMap = new Map<String,String>();
        Set<Id> parentOpp = new Set<Id>();
    

        FOR (Account_Manager_Handoff__c amh1 : newAMH) {
            parentOpp.add(amh1.Opportunity__c);

        Map<Id,Opportunity> oppMap2 = new Map<Id,Opportunity> ([SELECT Id
                                                                FROM Opportunity
                                                                WHERE Id IN :parentOpp]);
                                                                
        FOR(Account_Manager_Handoff__c amh2 : newAMH){ 
        

        system.debug('********AMH'+amh2.Assigned_Account_Manager__c+' '+amh2.Opportunity__r.Managed_Transfer__c);
        IF(oppMap2.containskey(amh2.Opportunity__c) && amh2.Assigned_Account_Manager__c != NULL && amh2.Opportunity__r.Managed_Transfer__c == TRUE){
  
            Opportunity renewalOpp = amh2.Opportunity__r.clone(false);

                Opportunity clonedObj = renewalOpp.clone(false,true);
                          
                clonedObj.Name = amh2.Opportunity__r.Acct_Name__c + ' - Renewal (' +amh2.Opportunity__r.Renewal_Date_Year__c + ')';
                clonedObj.OwnerId = amh2.Assigned_Account_Manager__r.Id;
                clonedObj.StageName   = 'Active Discussions';
                clonedObj.CloseDate   = amh2.Opportunity__r.Renewal_Date_Next__c;
                clonedObj.Amount = amh2.Opportunity__r.Amount;
                clonedObj.Effective_Date__c  = amh2.Opportunity__r.Renewal_Date_Next__c;
                clonedObj.Renewal__c = 'Yes';
                clonedObj.Renewed_Opportunity__c = amh2.Opportunity__r.Id;
                clonedObj.Probability = 5;
    
    insert clonedObj;
        }
    }
}
}
}


Best Answer chosen by John Neilan
Pavan DavePavan Dave
You need to have all the fields in your SOQL before using it in your code.
Here you have missed querying Opportunity__c & Assigned_Account_Manager__c from Account_Manager_Handoff__c
Add these 2 fields as well in your SOQL at Line 19

All Answers

Pavan DavePavan Dave
You are using Opportunity__r, this may be the probelm.
You may refer: 
https://developer.salesforce.com/forums/ForumsMain?id=906F000000090YSIAY
John NeilanJohn Neilan
Thanks.  I tried using that but now I get an error:

execution of AfterInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account_Manager_Handoff__c.Assigned_Account_Manager__c: Class.ClassAccountManagerHandoffNotes.autoCreateRenewal1: line 26, column 1

What did I do wrong?  Thanks,

public class ClassAccountManagerHandoffNotes {


    public void autoCreateRenewal1(Account_Manager_Handoff__c[] newAMH){

        Map<String,String> oppMap = new Map<String,String>();
        Set<Id> parentOpp = new Set<Id>();
    

        FOR (Account_Manager_Handoff__c amh1 : newAMH) {
            parentOpp.add(amh1.Opportunity__c);

        Map<Id,Opportunity> oppMap2 = new Map<Id,Opportunity> ([SELECT Id
                                                                FROM Opportunity
                                                                WHERE Id IN :parentOpp]);
                                                                

        set<Id> amhIds = trigger.newMap.keyset();
        List<Account_Manager_Handoff__c> amhOppData = [SELECT Opportunity__r.Managed_Transfer__c
                                                        FROM Account_Manager_Handoff__c
                                                        WHERE Id IN :amhIds] ;
       FOR(Account_Manager_Handoff__c amh2 : amhOppData){ 
        

        system.debug('********AMH'+amh2.Assigned_Account_Manager__c+' '+amh2.Opportunity__r.Managed_Transfer__c);
        IF(oppMap2.containskey(amh2.Opportunity__c) && amh2.Assigned_Account_Manager__c != NULL && amh2.Opportunity__r.Managed_Transfer__c == TRUE){
  
            Opportunity renewalOpp = amh2.Opportunity__r.clone(false);

                Opportunity clonedObj = renewalOpp.clone(false,true);
                          
                clonedObj.Name = amh2.Opportunity__r.Acct_Name__c + ' - Renewal (' +amh2.Opportunity__r.Renewal_Date_Year__c + ')';
                clonedObj.OwnerId = amh2.Assigned_Account_Manager__r.Id;
                clonedObj.StageName   = 'Active Discussions';
                clonedObj.CloseDate   = amh2.Opportunity__r.Renewal_Date_Next__c;
                clonedObj.Amount = amh2.Opportunity__r.Amount;
                clonedObj.Effective_Date__c  = amh2.Opportunity__r.Renewal_Date_Next__c;
                clonedObj.Renewal__c = 'Yes';
                clonedObj.Renewed_Opportunity__c = amh2.Opportunity__r.Id;
                clonedObj.Probability = 5;
    
    insert clonedObj;
        }
    }
}
}
}


Pavan DavePavan Dave
You need to have all the fields in your SOQL before using it in your code.
Here you have missed querying Opportunity__c & Assigned_Account_Manager__c from Account_Manager_Handoff__c
Add these 2 fields as well in your SOQL at Line 19
This was selected as the best answer
John NeilanJohn Neilan
Thanks!  That got rid of my error and the trigger now executes, but it does not clone the Opportunity as it should.  Ugh.  I guess I need to debug some more!
AshlekhAshlekh
Hi

Below code will help you 
public class ClassAccountManagerHandoffNotes {


    public void autoCreateRenewal1(Account_Manager_Handoff__c[] newAMH){

        Map<String,String> oppMap = new Map<String,String>();
        Set<Id> parentOpp = new Set<Id>();
		
		FOR (Account_Manager_Handoff__c amh1 : newAMH) {
			if(amh1.Opportunity__c != null)
				parentOpp.add(amh1.Opportunity__c);
		}
        
		Map<Id,Opportunity> oppMap2 = new Map<Id,Opportunity> ([SELECT Id,Managed_Transfer__c,Acct_Name__c,Renewal_Date_Year__c
                                                               ,Renewal_Date_Next__c,Amount FROM Opportunity
                                                                WHERE Id IN :parentOpp]);
        List<Opportunity> newOpportunityList = new List<Opportunity>();
		FOR(Account_Manager_Handoff__c amh2 : amhOppData)
		{ 
			system.debug('********AMH'+amh2.Assigned_Account_Manager__c+' '+amh2.Opportunity__r.Managed_Transfer__c);
			IF(oppMap2.containskey(amh2.Opportunity__c) && amh2.Assigned_Account_Manager__c != NULL && oppMap2.get(amh2.Opportunity__c).Managed_Transfer__c == TRUE)
			{
				Opportunity f = oppMap2.get(amh2.Opportunity__c)
				Opportunity renewalOpp = f.clone(false);
				Opportunity clonedObj = renewalOpp.clone(false,true);
                clonedObj.Name = f.Acct_Name__c + ' - Renewal (' +f.Renewal_Date_Year__c + ')';
                clonedObj.OwnerId = amh2.Assigned_Account_Manager__c;
                clonedObj.StageName   = 'Active Discussions';
                clonedObj.CloseDate   = f.Renewal_Date_Next__c;
                clonedObj.Amount = f.Amount;
                clonedObj.Effective_Date__c  = f.Renewal_Date_Next__c;
                clonedObj.Renewal__c = 'Yes';
                clonedObj.Renewed_Opportunity__c = amh2.Opportunity__r.Id;
                clonedObj.Probability = 5;
				newOpportunityList.add(clonedObj);
        }
	}
	
	if(newOpportunityList.size()>0)
	insert clonedObj;
}
}

John NeilanJohn Neilan
Thanks Ashlekh,

That code works as well to get rid of any errors, however, the trigger is still not cloning the Opportunity.  Any idea why that may be happening?