2

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<String, Object> 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

    Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(json_response);

    Map<String, Object> basic_profile = (Map<String, Object>)m.get('BasicProfile');

    # THIS LINE
    objdlt.BasicProfile__c = getBasicProfile(basic_profile);
    insert objdlt; 

    pagereference ref = new pagereference(redirect_page);
    ref.setredirect(true);
    return ref;
}

1 Answer 1

5
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?

0

You must log in to answer this question.

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