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
Wendy SadehWendy Sadeh 

Setting Opportunity Owner - Getting Insufficient Privileges

In my partner community I have some custom code that will create an opportunity and will set up some defaults.  If a partner creates an opportunity I want to set the Opportunity Owner to an internal salesperson.  

For instance:  opportunity.OwnerID = '005j000000CAPu5';

When I do this I get Insufficient Privileges error.  Not sure what lever to pull.
Mahesh DMahesh D
Hi Wendy,

Can you provide the custom code which you have.

Also check the Permissions for the User Profile on Opportunity object, make sure it has atleast Create / Edit permissions.

Regards,
Mahesh
Mahesh DMahesh D
Please copy the code using above panel (Add a code sample < >).
Wendy SadehWendy Sadeh
public without sharing class CustomOpportunityController
{

 public String  opportunityName {get; set;}
 public String  ppProjectNumber {get; set;}
 public String  accountName {get; set;}
 public String  companyName {get; set;}
 public String  mainContact {get; set;}
 public String  pp_project_number {get; set;}
 
 public CustomOpportunityController () {}
 
 public PageReference createOpportunity() 
 {
    Opportunity opportunity = new Opportunity();
        
    try 
    {
      system.debug('Oppurtunity Name : ' + opportunityName);
      system.debug('Pace Project Number : ' + ppProjectNumber);
      system.debug('Account Name : ' + accountName);
      
      Id UserId = UserInfo.getUserId();
      Id companyId;
      Id contactId;
      Id opportunityId;
      String BaseURL;
      String refUrl;
      
      String Email;
      String Phone;
      String Street_Address;
      String City;             
      String State;
      String ZipCode;      
      
      system.debug('Current logged in User ID : ' + UserId);
    
      if(UserId != null)
      {
        
          List<User> userlst = [
            SELECT Name, Account.Id, Account.Name, Contact.Id,
                   Contact.Email, Contact.Phone,  
                   Account.Street_Address__c, Account.City__c,
                   Account.State__c, Account.zip_code__c
            FROM User
            WHERE Id = :UserId
            LIMIT 1
            ];
            
          User user; 
          
          if ( userlst.size() > 0 ) 
          {
              user = userlst[0];
              companyName = user.Account.Name; 
              companyId   = user.Account.Id; 
              contactId   = user.Contact.Id;
               
              mainContact = user.Name; 
              Email       = user.Contact.Email;         
              Phone       = user.Contact.Phone;
              Street_Address =  user.Account.Street_Address__c;
              City        = user.Account.City__c; 
              State       = user.Account.State__c; 
              ZipCode     = user.Account.zip_code__c;
              
              system.debug('Users - company name : ' +   companyName);   
              system.debug('Users - main contact : ' +   mainContact);    
              system.debug('Users - companyId    : ' +   companyId);   
              system.debug('Users - contactId    : ' +   contactId); 
                      
          }  
          else
          {
              ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'User Company Name not found'));
          }
      }
      
      Account account; 
      List<Account> accountlst = [
            SELECT Id, Name
            FROM Account
            WHERE Name = :accountName
            LIMIT 1
            ];
      
      if ( accountlst.size() > 0 ) 
      {
          account = accountlst[0];
           system.debug('Successfully account retrieved ' + account.Name + ' with Id:' + account.Id);
      }
      else
      {
          account = new Account();
          account.Name = accountName;
          insert account;
          system.debug('Successfully account created ' + account.Name + ' with Id:' + account.Id);
          
          accountlst = [
            SELECT Id
            FROM Account
            WHERE Name = :accountName
            LIMIT 1
            ];
          
          if ( accountlst.size() > 0 ) 
          {
              account = accountlst[0];
          }
      }        
      opportunity.accountId = account.id; 
      opportunity.Name    = opportunityName;
      opportunity.StageName = 'Pre-Qualified Lead';
      opportunity.Loan_Term__c = 20;
      opportunity.PACEPartner_Project_Number__c = ppProjectNumber;     
      opportunity.Initial_Entry_Date__c =  system.today();
      opportunity.CloseDate   = system.today().addDays(90);
      opportunity.LeadSource = 'Channel Partner';
      opportunity.Channel_Partner_Entity__c = companyId;
      opportunity.Channel_Partner_Main_Contact__c = contactId;
      opportunity.Validated_by_CF__c = false;
      opportunity.Sub_Lead_Source__c = companyName;
      opportunity.PACEPartner_Mailing_City__c = City;
      opportunity.PACEPartner_Email__c = Email;
      opportunity.PACEPartner_Name__c = contactId;
      opportunity.PACEPartner_Phone__c  = Phone;
      opportunity.PACEPartner_Mailing_State__c = State;
      opportunity.PACEPartner_Mailing_Street_Address__c = Street_Address;  
      opportunity.PACEPartner_Mailing_Zip_Code__c   = ZipCode; 
      /* Renee Pifer is default transaction manager */
      opportunity.Transaction_Manager__c = '005j000000CAYj4AAH';

        /* Set up the default Opportunity Owner */

        /* Solar City,Solar Engine - Brandon is Owner*/
        if (opportunity.Channel_Partner_Entity__c == '001j000000YSGyd' || opportunity.Channel_Partner_Entity__c == '001j000000khX8m')  {
            opportunity.OwnerID =  '005j000000CAPu5';
        } /* View Glass, Minerva - Chris Robbins is owner*/
        else if (opportunity.Channel_Partner_Entity__c == '001j000000Sd0sR' || opportunity.Channel_Partner_Entity__c == '001j000000khGar') {
            opportunity.OwnerID = '005j000000BcK6J';
        } /* Pace Houston - Joshua is the owner */
        else if (opportunity.Channel_Partner_Entity__c == '001j000000WDrWj') {
            opportunity.OwnerID = '005j000000BcK6x';
        } /* Chris Robbins is default owner */
        else {
            opportunity.OwnerID = '005j000000BcK6J';
        }



                                
      system.debug('Just before opportunity insertion ');
      insert opportunity;
      opportunityId = opportunity.Id;
      system.debug('Just after opportunity insertion ');
      BaseURL = ApexPages.currentPage().getHeaders().get('Host');
      system.debug('Just after getting BaseURL ' + BaseURL);
      system.debug('Just after getting Opportunity ID : ' + opportunityId);
      refUrl = '/'+ opportunityId ;
      system.debug('Just after getting refUrl : ' + refUrl);
     
      system.debug('------------ opportunity  -----------'+ opportunity);
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Opportunity created successfully')); 
   
      PageReference ref = new PageReference(refUrl);    
      ref.setRedirect(true);
      return ref;
  } 
  catch (DMLException e) 
  {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error while creating Opportunity'));
      return null;
  } 
  finally 
  {
  }
    
    return null;
 }

 
Wendy SadehWendy Sadeh

The Insufficeint Priveledge  is because once I change the owner I cannot redirect back to the opportunity.   I need to create a sharing rule for the Partner.