Jump to content


Photo

Assign a contact to an opportunity (API)

api opportunity contact

  • Please log in to reply
2 replies to this topic

#1 rramirezz

rramirezz

    Member

  • Members
  • PipPip
  • 10 posts

Posted 21 October 2014 - 05:01 PM

how could i assign a contact to an opportunity when creating an opportunity via api?



#2 Ivica

Ivica

    Advanced Member

  • Moderators
  • 565 posts

Posted 22 October 2014 - 02:23 PM

Yes It is possible via API, please check ApRestContactTest::testCreateWithRelations() and testUpdateWithRelations. This example show how to create contact with relation to opportunity and what you need to do opposite from this example.

public function testCreateWithRelations()
        {
            $super = User::getByUsername('super');
            Yii::app()->user->userModel = $super;

            $authenticationData = $this->login();
            $headers = array(
                'Accept: application/json',
                'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
                'ZURMO_TOKEN: ' . $authenticationData['token'],
                'ZURMO_API_REQUEST_TYPE: REST',
            );

            $opportunity = OpportunityTestHelper::createOpportunityByNameForOwner('My Opportunity', $super);

            $data['firstName']           = "Freddy";
            $data['lastName']            = "Smith";
            $data['state']['id']         = ContactsUtil::getStartingState()->id;

            $data['modelRelations'] = array(
                'opportunities' => array(
                    array(
                        'action' => 'add',
                        'modelId' => $opportunity->id,
                        'modelClassName' => 'Opportunity'
                    ),
                ),
            );
            $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));

            $response = json_decode($response, true);
            $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
            $this->assertEquals($data['firstName'], $response['data']['firstName']);
            $this->assertEquals($data['lastName'], $response['data']['lastName']);

            RedBeanModel::forgetAll();
            $contact = Contact::getById($response['data']['id']);
            $this->assertEquals(1, count($contact->opportunities));
            $this->assertEquals($opportunity->id, $contact->opportunities[0]->id);

            $opportunity = Opportunity::getById($opportunity->id);
            $this->assertEquals(1, count($opportunity->contacts));
            $this->assertEquals($contact->id, $opportunity->contacts[0]->id);
        }

        /**
        * @depends testCreateWithRelations
        */
        public function testUpdateWithRelations()
        {
            $super = User::getByUsername('super');
            Yii::app()->user->userModel = $super;

            $authenticationData = $this->login();
            $headers = array(
                'Accept: application/json',
                'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
                'ZURMO_TOKEN: ' . $authenticationData['token'],
                'ZURMO_API_REQUEST_TYPE: REST',
            );

            $contact  = ContactTestHelper::createContactByNameForOwner('Simon', $super);
            $opportunity = OpportunityTestHelper::createOpportunityByNameForOwner('My Opportunity', $super);
            $compareData  = $this->getModelToApiDataUtilData($contact);
            $contact->forget();

            $data['modelRelations'] = array(
                'opportunities' => array(
                    array(
                        'action' => 'add',
                        'modelId' => $opportunity->id,
                        'modelClassName' => 'Opportunity'
                    ),
                ),
            );
            $data['firstName'] = 'Fred';

            $response = $this->createApiCallWithRelativeUrl('update/' . $compareData['id'], 'PUT', $headers, array('data' => $data));
            $response = json_decode($response, true);
            unset($response['data']['modifiedDateTime']);
            unset($compareData['modifiedDateTime']);
            $compareData['firstName'] = 'Fred';
            $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
            $this->assertEquals($compareData, $response['data']);

            RedBeanModel::forgetAll();
            $contact = Contact::getById($compareData['id']);
            $this->assertEquals(1, count($contact->opportunities));
            $this->assertEquals($opportunity->id, $contact->opportunities[0]->id);

            $opportunity = Opportunity::getById($opportunity->id);
            $this->assertEquals(1, count($opportunity->contacts));
            $this->assertEquals($contact->id, $opportunity->contacts[0]->id);

            // Now test remove relations
            $data['modelRelations'] = array(
                'opportunities' => array(
                    array(
                        'action' => 'remove',
                        'modelId' => $opportunity->id,
                        'modelClassName' => 'Opportunity'
                    ),
                ),
            );

            $response = $this->createApiCallWithRelativeUrl('update/' . $compareData['id'], 'PUT', $headers, array('data' => $data));
            $response = json_decode($response, true);
            $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
            RedBeanModel::forgetAll();
            $contact = Contact::getById($compareData['id']);
            $this->assertEquals(0, count($contact->opportunities));
            $opportunity = Opportunity::getById($opportunity->id);
            $this->assertEquals(0, count($opportunity->contacts));
        }


#3 rramirezz

rramirezz

    Member

  • Members
  • PipPip
  • 10 posts

Posted 22 October 2014 - 10:41 PM

Thank you so much Ivica.

 

You've been very helpful and have saved me a lot of work and time







Also tagged with one or more of these keywords: api, opportunity, contact

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users