1

I have a Cakephp3 project, and in one of the controllers I'm making an array of things to pass to Salesforce. They all work fine except for this field "Account" in the Opportunities ("Opportunity") table which is of type lookup(Account).

Opportunity Fields

The field is supposed to have an Account name (string). I've tried passing the name of the Account that's supposed to be there, I've tried passing the id of the account (0014000000U2mSh, for example) but every time I get the error:

"[message] => The external foreign key reference does not reference a valid entity: Account [statusCode] => INVALID_FIELD"

I've been looking this up all day but I haven't found an answer that fixes this yet. Could anyone please tell me how exactly to pass information on to this field? For all the other fields, it was simply a matter of saying

$fields['Name'] = $name;
$fields['CloseDate'] = date();
//$fields['Account'] = ???; gives error
etc...
$entries = array($fields);
$table = 'Opportunity';
$this->Carts->writeSalesForce($entries, $table);

The writeSalesForce function is as below:

public function writeSalesForce($entries, $table) {
        $objects = array();
        foreach($entries as $fields) {
            $sObject = new \stdclass();
            $sObject->fields = $fields;
            $sObject->type = $table;
            $objects[] = $sObject;
        }
        $createResponse = $this->sfc->create($objects);
        return $createResponse;
    }
2
  • Can you try AccountId ?
    – kurunve
    May 25, 2017 at 0:51
  • Thanks @kuruvne, that's what worked. Gave errors at first, still not sure why - literally commented it out and then uncommented it again later and this time it worked, so maybe some weird cache problem :| Cheers.
    – Maria
    May 26, 2017 at 1:27

1 Answer 1

3

The actual field's API Name is AccountId. I know the UI says Account but with pretty much any standard lookup, you actually append Id to the end of the name. There are dozens of examples of this behavior, such as Account.OpportunityId, Contact.AccountId, User.ContactId, etc. If you were to look at the opportunity describe, you would see this value.

Opportunity Describe

When you use Account, you need an instance of the parent object, not an Id value. One example of how this is done is: Set Relationship Via Name Pointing Field? I'm not sure if CakePHP supports this behavior, though.

5
  • Hi Adrian, thanks for your response. I believe I've tried adding AccountId instead....(ie $fields['AccountId'] = '0014000000U2mSh'; - is that what you're saying?) But I got the same error. I'll try again to make sure. Otherwise I'm not sure how to get an instance of the parent object...is this info in Salesforce? Unfortunately I inherited this project with all the fields preexisting and I've never worked on Salesforce before so it's a bit of a mess - as far as I can tell when I click hierarchy on the accounts I need I don't see anything useful (am I way off track here?) Thanks!
    – Maria
    May 25, 2017 at 17:14
  • @Maria Don't specify both. You need to specify just AccountId. I recommend you completely avoid name pointing fields, it's going to have a steeper learning curve.
    – Adrian Larson
    May 25, 2017 at 17:44
  • Sorry I don't follow your previous comment. I've tried adding $fields['AccountId'] = '0014000000U2mSh'; and $fields['Opportunity.AccountId']... (and many variances) - none work. Can you please tell me more specifically how I need to change this logic to make it work with this lookup(Account) field? Sorry fot the noob question, but after 2 days of this it's pretty frustrating.
    – Maria
    May 25, 2017 at 23:14
  • Well, I commented that line out, then went out to clear my head, came back a few hours later and uncommented it and ran it again for the n-th time. Apparently while I was gone the stars aligned, the hemisphere opened and a single ray of light came down upon my computer screen and guided Santa's little helpers to come and poop magical dust on my code or something because now it magically works, after two days of it not. Thanks for your time Adrian!
    – Maria
    May 26, 2017 at 1:21
  • Well that is one of the more interesting comments I have ever read! I'm glad you got it solved.
    – Adrian Larson
    May 26, 2017 at 1:28

You must log in to answer this question.

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