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
RajanRajan 

Illegal assignment from String to Account

Hi Friends,
Can any one please help for this error?.
I have to design a custom button for converting records in opportunity.
I have a table in which I am displaying data from Account, contact and 3 custom objects like attached pic. Whenever I'll select any record from this table using checkbox and click on that button then it should create opportunity. User-added image
I'm unable to get data of related object records like Account name, Contact name.
My code is as below:
******************
 global static string ConvertToOpportunity(Integer dataId){    
                
        Recommendation__c rec = [Select name, Account__r.Id, Account__r.Name, Recommendation_Contact__r.Name,Recommendation_Contact__r.Contact_YTD_Sales__c, Feedback__c,                                       Account__r.AccountNumber ,Recommendation_Contact__r.Title , Product_Service__c, Type__c, Opportunity_Score__c, Next_Best_Action__c, Product_Recommendation__c, Primary_SKU__c,  Recom_External_Id__c from Recommendation__c where Recom_External_Id__c =:dataId];
        system.debug('Recommendation:' +rec);
        
        if(rec != null){
        Opportunity opp = new Opportunity();
            opp.Name = rec.name;
            opp.Account_ID__c = rec.Account__r.Id;
            //opp.Account = rec.Account__r.Name;
            //opp.Account = String.valueOf(rec.Account__r.Name);
            // opp.Contact__c = rec.Recommendation_Contact__r.Name;
            opp.Recommendation_Name__c = rec.Name;
            opp.Opportunity_Lead_Score__c = rec.Opportunity_Score__c;
            opp.Contact_YTD_Sales__c = rec.Recommendation_Contact__r.Contact_YTD_Sales__c;
            opp.Conversation_Type__c = rec.Type__c;
            opp.Product_Recommendation__c = rec.Product_Recommendation__c;
            opp.Next_Best_Action__c = rec.Next_Best_Action__c;
            opp.CloseDate = system.today();
            opp.StageName = 'Pre-Qualified';
            
            system.debug('Opportunity:' +opp);
        insert opp;
        }
}|
Best Answer chosen by Rajan
RajanRajan
I've solved the issue. Thank you guyz for your respond.

All Answers

Soundar Rajan PonpandiSoundar Rajan Ponpandi
Hi rajan,

Please Use Below Code
 
global static string ConvertToOpportunity(Integer dataId){    
                
        Recommendation__c rec = [Select name, Account__r.Id, Account__r.Name, Recommendation_Contact__r.Name,Recommendation_Contact__r.Contact_YTD_Sales__c, Feedback__c,                                       Account__r.AccountNumber ,Recommendation_Contact__r.Title , Product_Service__c, Type__c, Opportunity_Score__c, Next_Best_Action__c, Product_Recommendation__c, Primary_SKU__c,  Recom_External_Id__c from Recommendation__c where Recom_External_Id__c =:dataId];
        system.debug('Recommendation:' +rec);
        
        if(rec != null){
        Opportunity opp = new Opportunity();
            opp.Name = rec.name;
            opp.AccountID = rec.Account__c;
            //opp.Account = rec.Account__r.Name;
            //opp.Account = String.valueOf(rec.Account__r.Name);
            // opp.Contact__c = rec.Recommendation_Contact__r.Name;
            opp.Recommendation_Name__c = rec.Name;
            opp.Opportunity_Lead_Score__c = rec.Opportunity_Score__c;
            opp.Contact_YTD_Sales__c = rec.Recommendation_Contact__r.Contact_YTD_Sales__c;
            opp.Conversation_Type__c = rec.Type__c;
            opp.Product_Recommendation__c = rec.Product_Recommendation__c;
            opp.Next_Best_Action__c = rec.Next_Best_Action__c;
            opp.CloseDate = system.today();
            opp.StageName = 'Pre-Qualified';
            
            system.debug('Opportunity:' +opp);
        insert opp;
        }
}|


Regards,

Soundar.
sourajking@gmail.com
 
RajanRajan
Thanks Soundar for reply,
I'm using the same code but I also have to insert Account name and contact name which is a related object. Apart from these 2 fields I am able to insert records. Can you please help me to insert related object records for below fields?

opp.Account = rec.Account__r.Name; ----                 error is ''Illegal assignment from String to Account''
opp.Contact__c = rec.Recommendation_Contact__r.Name;        -- error is ''System.StringException: Invalid id: Serdar Sahin'' here Serdar Sahin is contact name of the record
Shiva RajendranShiva Rajendran

Hi Rajan,

i hope  Opp.Account is a lookup field ,so you can insert only Account refernce there.
The error is self explainatory Illegal assignment from String to Account. opp.Account requires account reference while rec.Account__r.Name is a string.
If you need account name to be present on the opportunity  ,create a formula field or string field in opportunity which takes value from the account reference present in the opportunity.

The same applies for opp.Contact__c

Let me know if you further help.

Thanks and Regards,
Shiva RV

RajanRajan
Hello Shiva,
Opp.Account is lookup field in Oppotunity object with account. How to give account reference ?
What will be changes in code and what is the proceasure?
I've no idea about this because I have never worked with this type of situation. Thanks in advance
Shiva RajendranShiva Rajendran
Hi Rajan,

Not sure of Opp.Account field in opportunity but as @Soundar Raj mentioned in the previous comment ,there is a field called opp.AccountId which is a lookup or Reference Field.Just check if the field type of Account in opportunity object.Is it custom object and what is its type?
 

Can you share your opportunity object structure.
Go to any opportunity Record.
select view fields in the sidebar on that.You will see all the fields present in the opportunity.
Share the screenshot here to help you better.

Thanks and Regards,
Shiva RV
RajanRajan
I've solved the issue. Thank you guyz for your respond.
This was selected as the best answer