PHP - Many To Many Relationship

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

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("SECURITY_TOKEN", "XXXXXXXXXXXXXXXXX");
    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->createConnection(SOAP_CLIENT_BASEDIR.'/enterprise.wsdl.xml');
      $mySforceConnection->login(USERNAME, PASSWORD.SECURITY_TOKEN);

      $sObject = new stdclass();
      $sObject->FirstName = $first_name;
      $sObject->LastName = $last_name;
      // INSERT SOME FIELD HERE TO ATTACH AN ARRAY OF COURSES THE USER IS ATTACHED TO

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

    } catch (Exception $e) {
      echo $mySforceConnection->getLastRequest();
      echo $e->faultstring;
    }
profilepic.png
manpreet 2 years ago

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__cwith 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)


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.