4

We have created one vf page in custom object. From that, vf page we need to select an opportunity name from hardcoded picklist values and need to select the existing account where we need to create an opportunity, Also I have a input field in vf page. In this input field I need to populate the selected account field called salesperson. After selecting the existing account and picklist value I will get the selected account Salesperson field value in given input field. If I click create button then the opportunity will be created under the existing account. The opportunity owner should be the Salesperson the one which I selected from the vf page. As expected the opportunity has been created under the existing account but the opportunity owner was not the one which I selected. Steps I followed, 1.I have taken the salesperson value from Account using the below query.

public String Sales_pop1 {get;set;} Sales_pop1 =[select EXO_Sales_Rep__r.id, EXO_Sales_Rep_r.Name from Account where id=:siteObj.Account_c LIMIT 1].EXO_Sales_Rep__r.Name;

now Sales_pop1 variable will have the value of Salesperson name.

  1. I have written a code to create an opportunity in the existing account. There I have mapped the Sales_pop1 field to Opportunity Owner field. The code is below

    public void convertbutton1() {

    try {
    Opportunity opp = new Opportunity();

    opp.Site_Ref_No__c = siteObj.Site_Ref_No__c;
    
    opp.Name = siteObj.Name+'-'+string1;
    

    opp.AccountId=siteObj.Account__c; //Mapping Selected lookup account id to Opportunity AccountId opp.Owner=Sales_pop1; //Mapping Salesperson name to Opportunity owner

    opp.CloseDate=siteObj.Completion_Date__c;

     opp.StageName=stage1;
    
      if(stage1 == 'No Contact Made')
        opp.Probability= 0;
     else if(stage1 == 'Conatct Made')
        opp.Probability= 10;
     else if(stage1 == 'Taking Off')
        opp.Probability= 25;
     else if(stage1 == 'Quotation with Customer ')
        opp.Probability= 50;
     else if(stage1 == 'Negotiation/Review')
         opp.Probability= 75;
     else if(stage1 == 'Closed Won')
        opp.Probability= 100;
     else if(stage1 == 'Closed Lost')
        opp.Probability= 0;
    try 
     { 
     insert opp; 
    }
    

    I got an error while passing the salesperson value to Opportunity owner.

Error Error: Compile Error: Illegal assignment from String to SOBJECT:User at line 312 column 9

Please help me out in this issue. Thanks in Advance.

Regards, Uday.

1 Answer 1

1

The problem is that the Owner is an SObject of type User. The OwnerId is an Id type that points to the correct User record.

Do you really want to permanently change the Owner's name of an opportunity to the sales rep's name found on an Account? As in change the actual User record that is the owner of an opportunity?

If so, you'll need to first select the user record and then make the change to it directly by using the OwnerId field of the Opportunity.

User u = [SELECT FirstName, LastName FROM User WHERE Id = :opp.OwnerId];

Also, you can't change the Name field of a user record, but you can change the FirstName and/or the LastName of the User record.

1
  • Hi Adam, Thanks for the reply. i will try this one. Yes I need to change the Opportunity Owner name to the Sales rep's name which is given on an Account. As per my requirement, I have created a lookup field for selecting the user called Salesrep in an account. In vf page if I select the existing account the particular assigned salesrep has to be populated. Populated salesrep has to be the owner of the Opportunity when I click convert button from vf page. Please advice me.
    – Udaya
    Commented May 11, 2013 at 7:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .