Before I begin my question, I'd just like to give a brief background on what I am doing and what I am looking to achieve.

FYI: I am a newbie with this Salesforce stuff.

  1. I have connected my site with salesforce successfully via the Force.com PHP Toolkit.

  2. VIA the PHP toolkit I am able to successfully upsert a contact when a new user is registered on my site.

  3. I have created a custom object called "Courses".

  4. VIA the PHP toolkit I am able to successfully upsert a "course" into the custom object "courses".

  5. I am looking at creating a relationship between the 2 objects. The relationship will be contacts (students) assigned to (courses). A contact can be in many courses and a course can have many students.

  6. So after some research I have found a way to create this many-to-many relationship via creating a junction object which will serve to connect the 2 other objects via the "Master-Detail Relationship" fields.

  7. This is working fine as I want it. If I go into a contact I can see a section where I can assign a course to the contact.

  8. I need the last step to be done via the PHP Toolkit.

This is where I do not know what to do or how to write the function. Because it is a many-to-many relationship, Im guessing that the object I build in php has to contain an array of courses that I am assigning to the contact.

Is there a particular field I need to target and if so what are the params I need to pass to it?

    define("USERNAME", "XXXX@XXXXXXXX.com");
    define("PASSWORD", "XXXXXXXX");
    define("SOAP_CLIENT_BASEDIR", trailingslashit( ABSPATH ) . "../soapclient");
    require_once (SOAP_CLIENT_BASEDIR.'/SforceEnterpriseClient.php');
    require_once (SOAP_CLIENT_BASEDIR.'/SforceHeaderOptions.php');
    try {
      $mySforceConnection = new SforceEnterpriseClient();
      $mySforceConnection->login(USERNAME, PASSWORD.SECURITY_TOKEN);

      $sObject = new stdclass();
      $sObject->FirstName = $first_name;
      $sObject->LastName = $last_name;

  $createResponse = $mySforceConnection->upsert("Email", array($sObject), 'Contact');

    } catch (Exception $e) {
      echo $mySforceConnection->getLastRequest();
      echo $e->faultstring;

1 Answer 1


In Salesforce, for the junction record, let's call it Course_Registration__c.

For each course registration (Contact : Course), you will need to insert a Course_Registration__c with these fields filled in:

  • Course__c (points at the Course being registered for)
  • Contact__c (points at the Contact registered for the course)

Both Course_Registration__c.Course__c and Course_Registration__c.Contact__c are ID fields. The values to set will be the IDs returned after you insert the Course__c and Contact SObjects, respectively.

That is, the inserts to the two master SObjects need to happen first, you grab the IDs of these upon return from the insert call to the SFDC API, and you use them to populate a junction record via another insert (or bulk insert for all registrations per Contact)

  • Thank you so much. But I do have another question. Nov 25, 2017 at 9:46
  • The records in the junction objects aren't linking to the actual related course record that the contact has enrolled in. Instead the record is linking to an actual junction object record. Should it be linking to the actual course record?? Nov 25, 2017 at 9:48
  • I'm not sure I follow this question. The junction record has two parent lookup ID fields - one to Course, one to Contact. Once inserted, you will be able to traverse from Contact to all registered courses for that Contact; or from Course to all registered Contacts in that course
    – cropredy
    Nov 25, 2017 at 18:37
  • Ohhh.... I think I made a small mistake. I created the 2 fields as Master-Detail Relationship fields. So instead I need 2 Lookup Relationship fields. Is this correct? Nov 26, 2017 at 10:38
  • Master detail or lookup, doesn’t matter
    – cropredy
    Nov 26, 2017 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.