+ Start a Discussion
mgodseymgodsey 

Cloning Opp Team Members with their Access Level

I am using a standard controller extension to custom clone Opportunities. It will allow Opportunities to be cloned with their Opportunity Teams (among other objects). However, I'm having trouble getting the access level for the team member to clone as well. From looking at the community boards I know that I need to insert an OpportunityShare record as well, but that hasn't helped. Even if the team member had Read/Write access on the original Opp, on the clone they just have Read access.

 

Can anyone take a peek at my code below and let me know where I've gone wrong? Thanks!!

 

public class OpportunityCloneExtension{
     
    //Variable to hold current record
    Opportunity currentRecord;
     
    //Standard constructor method
    public OpportunityCloneExtension(ApexPages.StandardController controller){
        currentRecord = (Opportunity)controller.getRecord();
    } 
     
    public PageReference cloneRecord(){
        //Variable to hold the new record
        Opportunity newRecord;
        Savepoint sp = Database.setSavepoint();
         
        try{
            //------Cloning the Opportunity-----------
            //get all the creatable fields from the object so they can be cloned
            String oppSOQL = CSUtils.getCreatableFieldsSOQL('Opportunity',   'id=\''+currentRecord.id+'\'');
             
            //query for the current Opportunity and clone it, replacing a few field values first
            currentRecord = (Opportunity)Database.query(oppSOQL);
            newRecord = currentRecord.clone(false);
            newRecord.ApprovalStatus__c = Status.NotSubmitted;
            newRecord.Type = 'Existing Client: Campaign Renewal';
            newRecord.SalesPlanner__c = null;
            newRecord.RequestType__c = null;
            newRecord.DeliverableDueDate__c = null;
            newRecord.PORequired__c = null;
            newRecord.PONumber__c = null;
            newRecord.PrePay__c = null;
            newRecord.Campaign_Event__c = null;
            newRecord.ExistingConversionVolume__c = null;
             
            newRecord.ClonedFrom__c = currentRecord.id;
             
            insert newRecord;
          
            //--------Cloning the Opp Team Test--------
            List<OpportunityTeamMember> relatedTeamMembers = new List<OpportunityTeamMEmber>();
            for(OpportunityTeamMember tm : [SELECT OpportunityAccessLevel, OpportunityId, UserId, TeamMemberRole FROM OpportunityTeamMember WHERE OpportunityId =: CurrentRecord.id]){
                OpportunityTeamMember otm = tm.clone(false);
                otm.OpportunityID = newRecord.Id;
                relatedTeamMembers.add(otm);
            } 
            
            insert relatedTeamMembers;   
            
            List<OpportunityShare> relatedOppShare = new List<OpportunityShare>();
            for(OpportunityShare os : [SELECT OpportunityAccessLevel,OpportunityId,RowCause, UserOrGroupID FROM OpportunityShare WHERE OpportunityID =:CurrentRecord.id AND RowCause = 'Sales Team']){
                OpportunityShare nOS = os.clone(false);
                nOS.OpportunityID = newRecord.id;
                relatedOppShare.add(nOS);
                
            }
            
            insert relatedOppShare;    
           
         
        } catch(Exception e){
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
        }
         
        return new PageReference('/'+newRecord.id);
    }     
}

 

Best Answer chosen by Admin (Salesforce Developers) 
mgodseymgodsey
This post solved the issue, for anyone that is looking at this post later.

http://blog.jeffdouglas.com/2011/03/17/opportunityaccesslevel-not-writable/