I am building an app which reads information from an external API and then populates data in my App for further processing. I have two kinds of custom objects for this. First kind (e.g., BasicProfile) stores all the retrieved information. And second kind (e.g., Summary) stores the derived information. Summary also needs to have a lookup for BasicProfile and this is working fine in Salesforce UI. But when I try to populate this lookup field using Apex code, it is giving me error "Illegal assignment from BasicProfile__c to Id". Please let me know how can I populate the lookup field using Apex. Following is my existing code in Apex. (If I remove the lookup part, everything else is working fine, both the objects are inserted separately.)
public BasicProfile__c getBasicProfile(Map basic_profile) < BasicProfile__c obj = new BasicProfile__c(); obj.Zip__c = (String)basic_profile.get('Zip'); insert obj; return obj; >public PageReference doInsert() < Summary__c objdlt = new Summary__c(); objdlt.Name__c = nameVal; # Get json response from external API Mapm = (Map)JSON.deserializeUntyped(json_response); Map basic_profile = (Map)m.get('BasicProfile'); # THIS LINE objdlt.BasicProfile__c = getBasicProfile(basic_profile); insert objdlt; pagereference ref = new pagereference(redirect_page); ref.setredirect(true); return ref; >
asked Dec 17, 2018 at 19:16
padmanabh pande padmanabh pande
121 2 2 bronze badges
objdlt.BasicProfile__c = getBasicProfile(basic_profile).Id;
getBasicProfile(basic_profile) is returning the actual BasicProfile__c object, when you really just need its Id. So, access it via Id .
It makes more sense when you look at it this way:
BasicProfile__c profile = getBasicProfile(basic_profile); objdlt.BasicProfile__c = profile.Id; // not profile, a reference to an object, not an id
Maybe also rename getBasicProfile to createBasicProfile ?