Thursday 15 January 2015

sugar crm rest api example

REST api Example

source: http://urdhva-tech.blogspot.co.uk/2013/02/rest-api-example.html

At times developers find it so difficult to find simple and running example of REST API to perform CRUD operations on SugarCRM.. There are plenty of tutorials, blogs, developer guides explaining same thing. Still for an ease for ourselves and to the hunters of same, writing the same thing all over again. 

REST Example

This blog post explains how to login to SugarCRM instance with admin user.


<?php

// specify the REST web service to interact with

$url = '<SITE_URL_OF_SUGAR>/service/v2/rest.php';

// Open a curl session for making the call
$curl = curl_init($url);

// Tell curl to use HTTP POST

curl_setopt($curl, CURLOPT_POST, true);

// Tell curl not to return headers, but do return the response

curl_setopt($curl, CURLOPT_HEADER, false);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);



// Set the POST arguments to pass to the Sugar server

$parameters = array(

    'user_auth' => array(

        'user_name' => 'admin',

        'password' => md5('<ADMIN_PASSWORD>'),

        ),

    );

$json = json_encode($parameters);
$postArgs = array(
    'method' => 'login',
    'input_type' => 'JSON',
    'response_type' => 'JSON',
    'rest_data' => $json,
    );
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);

// Make the REST call, returning the result
$response = curl_exec($curl);
// Convert the result from JSON format to a PHP array
$result = json_decode($response);
if ( !is_object($result) ) {
    die("Error handling result.\n");
}
if ( !isset($result->id) ) {
    die("Error: {$result->name} - {$result->description}\n.");
}
// Echo out the session id
echo $result->id."<br />";

$session = $result->id;

Create a new record in Lead module
 As we have session id, we are logged-in!, we will use that session id to create a new record.
Here we used set_entry method for that we need three parameters

1) session : pass current session id
2) module : module name to create a record
3) name_value_list : name value list combination

Let's see that in below code snippets


$parameters = array(
    'session' => $session, //Session ID
    'module' => 'Leads',  //Module name
    'name_value_list' => array (
            array('name' => 'first_name', 'value' => 'David'),
            array('name' => 'last_name', 'value' => 'Boris'),
            array('name' => 'status', 'value' => 'New'),
            array('name' => 'lead_source', 'value' => 'Web Site')
        ),
    );
$json = json_encode($parameters);
$postArgs = 'method=set_entry&input_type=JSON&response_type=JSON&rest_data=' . $json;

curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);

// Make the REST call, returning the result
$response = curl_exec($curl);


// Convert the result from JSON format to a PHP array
$result = json_decode($response,true);

// Get the newly created record id
$recordId = $result['id'];


print "New Record Created with ID ".$recordId;


Read detail of top 5 leads having work phone, as an example.


// Let us fetch detail of a Lead

$fields_array = array('first_name','last_name','phone_work');

$parameters = array(

    'session' => $
session,                                 //Session ID
    'module_name' => 'Leads',                             //Module name
    'query' => " leads.phone_work IS NOT NULL ",   //Where condition without "where" keyword
    'order_by' => " leads.last_name ",                 //$order_by
    'offset'  => 0,                                               //offset
    'select_fields' => $fields_array,                      //select_fields
    'link_name_to_fields_array' => array(array()),//optional
    'max_results' => 5,                                        //max results                 
    'deleted' => 'false',                                        //deleted
);

$json = json_encode($parameters);

$postArgs = array(
    'method' => 'get_entry_list',
    'input_type' => 'JSON',
    'response_type' => 'JSON',
    'rest_data' => $json,
    );

curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);

$response = curl_exec($curl);

// Convert the result from JSON format to a PHP array

$result = json_decode($response);


print "<pre>";
print_r($result);
die;

Update a lead status "New" to "Assigned" for lead which we have just created. We can use set_entry method with specified record id to update a value.


$recordId = "<Your 36 character record id>"; 

$parameters = array(
    'session' => $session,
    'module' => 'Leads',
    'name_value_list' => array(
            array('name' => 'id', 'value' => $recordId),  //Record id to update
            array('name' => 'status', 'value' => 'Assigned'),
        ),
    );
$json = json_encode($parameters);
$postArgs = 'method=set_entry&input_type=JSON&response_type=JSON&rest_data=' . $json;

curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);

// Make the REST call, returning the result
$response = curl_exec($curl);
// Convert the result from JSON format to a PHP array
$result = json_decode($response,true);

// Get the newly created record id
$recordId = $result['id'];
print "Record Updated, Updated id ".$recordId;

Delete a record via web-service.  It is as simple as we update record with id. Here we need to provide id and deleted equal 1.


$recordId = "<Your 36 character record id>"; 

$parameters = array(
    'session' => $session,
    'module' => 'Leads',
    'name_value_list' => array(
            array('name' => 'id', 'value' => $recordId),
            array('name' => 'deleted', 'value' => '1')        //Deleted flag
        ),
    );
$json = json_encode($parameters);
$postArgs = 'method=set_entry&input_type=JSON&response_type=JSON&rest_data=' . $json;

curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);

// Make the REST call, returning the result
$response = curl_exec($curl);
// Convert the result from JSON format to a PHP array
$result = json_decode($response,true);

// Get the newly created record id
$recordId = $result['id'];

print "Record Deleted!, Deleted record id is ".$recordId;

Friday 12 December 2014

how to show errors in php - answers in an instant

error_reporting(E_ALL); ini_set('display_errors', 1); source: http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display

Saturday 7 December 2013

3 reasons people may be put off calling you about your business opportunity

Is an 08 number putting off potential customers daily?

Do you want more people calling you about your business opportunity?

We've discovered that having a UK local landline number can improve your chances of getting people to call you about your business opportunity. Here's why..

1. Most people have minutes included to land lines in their home or mobile package

2. Freephone (0800 or 0808) numbers are not free for mobiles

3. More people have mobiles than land lines

Get an 0113 Leeds number now – free trial for 1 week followed by £4.99 a month.
More effective than a 0800 and now includes free voicemail service which emails you the voicemail message.

Your exclusive offer code is
http://goo.gl/RHPWWq

Order your free trial now.

Other UK codes are available. Please contact mark@hopgood.eu for more details

Never miss a call - get a mobile friendly dedicated number with voicemail for £4.99 a month

Here's a way to get a dedicated local number for your business with voicemail for just £4.99 a month.
First week free - you can try it for a month with no further charges if you cancel in week 1.

As a comparison, BT's standard 
line rental is £15.45/mth (£15.99/mth from Jan 2014) 

Choose from the following UK number prefixes.

0113 Leeds
0114 Sheffield
0115 Nottingham
0116 Leicester
0117 Bristol
0118 Reading



Order your 1 week free trial of a local number with voicemail.
Local Number code 


You may cancel at any time. If you cancel within the 1 week trial period you will not be charged.
Charge after 1 week is £4.99 per month for your local number line rental and voicemail service.
Voicemails will be sent via email to an email account of your choice.


Wednesday 17 October 2012

Register for the MonaVie weekly webcast

MonaVie is a range of healthy natural products, MonaVie is a business and an opportunity.
With the MonaVie opportunity more people make more money, more quickly!

Join us in our weekly webcast, watch the video then enter your details for reminders.

MonaVie Executive Elite Distributors with Executive Distributors Mark and Sue Hopgood

Register for the MonaVie weekly webcast which includes a 5 star opportunity presentation and motivational videos.


Register for the MonaVie weekly webcastClaim your place on the weekly MonaVie Webcast. We will also send you details of our next meeting and regular motivational messages, free of charge.
Your first name
Telephone number
Email address
Best time to call