Class: SugarCRM::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/sugarcrm/connection/helper.rb,
lib/sugarcrm/connection/api/login.rb,
lib/sugarcrm/connection/api/logout.rb,
lib/sugarcrm/connection/connection.rb,
lib/sugarcrm/connection/api/get_entry.rb,
lib/sugarcrm/connection/api/set_entry.rb,
lib/sugarcrm/connection/api/get_entries.rb,
lib/sugarcrm/connection/api/get_user_id.rb,
lib/sugarcrm/connection/api/set_entries.rb,
lib/sugarcrm/connection/api/get_entry_list.rb,
lib/sugarcrm/connection/api/seamless_login.rb,
lib/sugarcrm/connection/api/get_server_info.rb,
lib/sugarcrm/connection/api/get_user_team_id.rb,
lib/sugarcrm/connection/api/search_by_module.rb,
lib/sugarcrm/connection/api/set_relationship.rb,
lib/sugarcrm/connection/api/get_entries_count.rb,
lib/sugarcrm/connection/api/get_module_fields.rb,
lib/sugarcrm/connection/api/get_relationships.rb,
lib/sugarcrm/connection/api/set_relationships.rb,
lib/sugarcrm/connection/api/get_report_entries.rb,
lib/sugarcrm/connection/api/set_campaign_merge.rb,
lib/sugarcrm/connection/api/get_note_attachment.rb,
lib/sugarcrm/connection/api/set_note_attachment.rb,
lib/sugarcrm/connection/api/get_available_modules.rb,
lib/sugarcrm/connection/api/get_document_revision.rb,
lib/sugarcrm/connection/api/set_document_revision.rb

Constant Summary collapse

URL =
"/service/v2/rest.php"
DONT_SHOW_DEBUG_FOR =

Set this to filter out debug output on a certain method (i.e. get_modules, or get_fields)

[]
RESPONSE_IS_NOT_JSON =
[:get_user_id, :get_user_team_id]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, user, pass, options = {}) ⇒ Connection

This is the singleton connection class.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sugarcrm/connection/connection.rb', line 20

def initialize(url, user, pass, options={})
  @options  = {
    :debug => false,
    :register_modules => true,
    :load_environment => true
  }.merge(options)
  @errors   = []
  @url      = URI.parse(url)
  @user     = user
  @pass     = pass
  @request  = ""
  @response = ""
  resolve_url
  login!
  self
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



13
14
15
# File 'lib/sugarcrm/connection/connection.rb', line 13

def connection
  @connection
end

#errorsObject

Returns the value of attribute errors.



17
18
19
# File 'lib/sugarcrm/connection/connection.rb', line 17

def errors
  @errors
end

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'lib/sugarcrm/connection/connection.rb', line 14

def options
  @options
end

#passObject (readonly)

Returns the value of attribute pass.



10
11
12
# File 'lib/sugarcrm/connection/connection.rb', line 10

def pass
  @pass
end

#requestObject

Returns the value of attribute request.



15
16
17
# File 'lib/sugarcrm/connection/connection.rb', line 15

def request
  @request
end

#responseObject

Returns the value of attribute response.



16
17
18
# File 'lib/sugarcrm/connection/connection.rb', line 16

def response
  @response
end

#sessionObject

Returns the value of attribute session.



11
12
13
# File 'lib/sugarcrm/connection/connection.rb', line 11

def session
  @session
end

#sugar_session_idObject

Returns the value of attribute sugar_session_id.



12
13
14
# File 'lib/sugarcrm/connection/connection.rb', line 12

def sugar_session_id
  @sugar_session_id
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/sugarcrm/connection/connection.rb', line 8

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



9
10
11
# File 'lib/sugarcrm/connection/connection.rb', line 9

def user
  @user
end

Instance Method Details

#b64_decode(file) ⇒ Object



46
47
48
# File 'lib/sugarcrm/connection/helper.rb', line 46

def b64_decode(file)
  Base64.decode64(file)
end

#b64_encode(file) ⇒ Object

We need to strip newlines from Base64 encoding for JSON validation purposes.



42
43
44
# File 'lib/sugarcrm/connection/helper.rb', line 42

def b64_encode(file)
  Base64.encode64(file).gsub(/\n/, '')
end

#class_for(module_name) ⇒ Object

Returns an instance of class for the provided module name



32
33
34
35
36
37
38
39
# File 'lib/sugarcrm/connection/helper.rb', line 32

def class_for(module_name)
  begin
    class_const = @session.namespace_const.const_get(module_name.classify)
    klass = class_const.new
  rescue NameError
    raise InvalidModule, "Module: #{module_name} is not registered"
  end
end

#connect!Object Also known as: reconnect!

Connect



62
63
64
65
66
67
68
69
# File 'lib/sugarcrm/connection/connection.rb', line 62

def connect!
  @connection = Net::HTTP.new(@url.host, @url.port)
  if @url.scheme == "https"
    @connection.use_ssl = true
    @connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  @connection.start
end

#connected?Boolean

Check to see if we are connected

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/sugarcrm/connection/connection.rb', line 55

def connected?
  return false unless @connection
  return false unless @connection.started?
  true
end

#debug=(debug) ⇒ Object



107
108
109
# File 'lib/sugarcrm/connection/connection.rb', line 107

def debug=(debug)
  options[:debug] = debug
end

#debug?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/sugarcrm/connection/connection.rb', line 111

def debug?
  options[:debug]
end

#get_available_modulesObject Also known as: get_modules

Retrieves the list of modules available to the current user logged into the system.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sugarcrm/connection/api/get_available_modules.rb', line 3

def get_available_modules
  login! unless logged_in?
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}"
    }
  EOF
  
  json.gsub!(/^\s{6}/,'')
  mods = send!(:get_available_modules, json)["modules"]
  modules = []
  mods.each do |mod|
    modules << Module.new(@session, mod)
  end
  modules
end

#get_document_revision(id) ⇒ Object

Downloads a particular revision of a document.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/sugarcrm/connection/api/get_document_revision.rb', line 3

def get_document_revision(id)
  login! unless logged_in?
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "id": "#{id}"
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  SugarCRM::Response.handle(send!(:get_document_revision, json), @session)
end

#get_entries(module_name, ids, opts = {}) ⇒ Object

Retrieve a list of SugarBeans by ID. This method will not work with the report module.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sugarcrm/connection/api/get_entries.rb', line 4

def get_entries(module_name, ids, opts={})
  login! unless logged_in?
  options = { 
    :fields => [], 
    :link_fields => [], 
  }.merge! opts

  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "module_name": "#{module_name}",
      "ids": #{ids.to_json},
      "select_fields": #{resolve_fields(module_name, options[:fields])},
      "link_name_to_fields_array": #{options[:link_fields].to_json}
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  SugarCRM::Response.handle(send!(:get_entries, json), @session)
end

#get_entries_count(module_name, query, opts = {}) ⇒ Object

Retrieves the specified number of records in a module.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sugarcrm/connection/api/get_entries_count.rb', line 3

def get_entries_count(module_name, query, opts={})
  login! unless logged_in?
  options = {
    :deleted => 0
  }.merge! opts

  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "module_name": "#{module_name}",
      "query": "#{query}",
      "deleted": #{options[:deleted]}
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  send!(:get_entries_count, json)
end

#get_entry(module_name, id, opts = {}) ⇒ Object

Retrieves a single SugarBean based on the ID.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sugarcrm/connection/api/get_entry.rb', line 3

def get_entry(module_name, id, opts={})
  login! unless logged_in?
  options = { 
    :fields => [], 
    :link_fields => [], 
  }.merge! opts
  
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "module_name": "#{module_name}",
      "id": "#{id}",
      "select_fields": #{resolve_fields(module_name, options[:fields])},
      "link_name_to_fields_array": #{options[:link_fields].to_json}
    }
  EOF
      
  json.gsub!(/^\s{6}/,'')
  SugarCRM::Response.handle(send!(:get_entry, json), @session)
end

#get_entry_list(module_name, query, opts = {}) ⇒ Object

Retrieve a list of SugarBeans. This is the primary method for getting a list of SugarBeans using the REST API.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sugarcrm/connection/api/get_entry_list.rb', line 4

def get_entry_list(module_name, query, opts={})
  login! unless logged_in?
  options = {
    :order_by => '', 
    :offset => '', 
    :fields => [], 
    :link_fields => [], 
    :limit => '', 
    :deleted => 0
  }.merge! opts

  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "module_name": "#{module_name}",
      "query": "#{query}",
      "order_by": "#{options[:order_by]}",
      "offset": "#{options[:offset]}",
      "select_fields": #{resolve_fields(module_name, options[:fields])},
      "link_name_to_fields_array": #{options[:link_fields].to_json},
      "max_results": "#{options[:limit]}",
      "deleted": #{options[:deleted]}
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  SugarCRM::Response.handle(send!(:get_entry_list, json), @session)
end

#get_module_fields(module_name) ⇒ Object Also known as: get_fields

Retrieves the vardef information of the specified bean.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/sugarcrm/connection/api/get_module_fields.rb', line 3

def get_module_fields(module_name)
  login! unless logged_in?  
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "module_name": "#{module_name}"
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  SugarCRM::Response.handle(send!(:get_module_fields, json), @session)
end

#get_note_attachment(id) ⇒ Object

Retrieves an attachment from a note.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/sugarcrm/connection/api/get_note_attachment.rb', line 3

def get_note_attachment(id)
  login! unless logged_in?
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "id": "#{id}"
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  send!(:get_note_attachment, json)["note_attachment"]
end

#get_relationships(module_name, id, related_to, opts = {}) ⇒ Object Also known as: get_relationship

Retrieves a collection of beans that are related to the specified bean and, optionally, returns relationship data



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sugarcrm/connection/api/get_relationships.rb', line 5

def get_relationships(module_name, id, related_to, opts={})
  login! unless logged_in?
  options = { 
    :query => '',
    :fields => [], 
    :link_fields => [], 
    :deleted => 0
  }.merge! opts  

  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "module_name": "#{module_name}",
      "module_id": "#{id}",
      "link_field_name": "#{related_to.downcase}",
      "related_module_query": "#{options[:query]}",
      "related_fields": #{resolve_related_fields(module_name, related_to)},
      "related_module_link_name_to_fields_array": #{options[:link_fields].to_json},
      "deleted": #{options[:deleted]}
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  SugarCRM::Response.new(send!(:get_relationships, json), @session, {:always_return_array => true}).to_obj
end

#get_report_entries(ids, opts = {}) ⇒ Object

Retrieves a list of report entries based on specified report IDs.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/sugarcrm/connection/api/get_report_entries.rb', line 3

def get_report_entries(ids, opts={})
  login! unless logged_in?
  options = {:select_fields => ''}.merge! opts

  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "ids": #{ids.to_json},
      "select_fields": "#{options[:select_fields].to_json}"
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  send!(:get_report_entries, json)
end

#get_server_infoObject

Returns server information such as version, flavor, and gmt_time.



3
4
5
6
# File 'lib/sugarcrm/connection/api/get_server_info.rb', line 3

def get_server_info
  login! unless logged_in?
  Response.handle(send!(:get_server_info, ""), @session)
end

#get_user_idObject

Returns the ID of the user who is logged into the current session.



3
4
5
6
7
8
9
10
11
12
# File 'lib/sugarcrm/connection/api/get_user_id.rb', line 3

def get_user_id
  login! unless logged_in?
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}"
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  send!(:get_user_id, json)
end

#get_user_team_idObject

Retrieves the ID of the default team of the user who is logged into the current session.



4
5
6
7
8
9
10
11
12
13
# File 'lib/sugarcrm/connection/api/get_user_team_id.rb', line 4

def get_user_team_id
  login! unless logged_in?
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}"
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  send!(:get_user_team_id, json)
end

#logged_in?Boolean

Check to see if we are logged in

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/sugarcrm/connection/connection.rb', line 38

def logged_in?
  connect! unless connected?
  @sugar_session_id ? true : false
end

#loginObject

Logs the user into the Sugar application.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/sugarcrm/connection/api/login.rb', line 3

def 
  connect! unless connected?    
  json = <<-EOF
    {
      "user_auth": {
        "user_name": "#{@user}",
         "password": "#{OpenSSL::Digest::MD5.new(@pass)}",
          "version": 2
      },
      "application": "sugarcrm_rubygem"
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  response = send!(:login, json)
end

#login!Object

Login



44
45
46
47
# File 'lib/sugarcrm/connection/connection.rb', line 44

def login!
  @sugar_session_id = ["id"]
  raise SugarCRM::LoginError, "Invalid Login" unless logged_in?
end

#logoutObject

Logs out of the Sugar user session.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/sugarcrm/connection/api/logout.rb', line 3

def logout
  login! unless logged_in?
  json = <<-EOF
    {
      "user_auth": {
        "session": "#{@sugar_session_id}"
      }
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  send!(:logout, json)
end

#resolve_fields(module_name, fields) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sugarcrm/connection/helper.rb', line 17

def resolve_fields(module_name, fields)
  # FIXME: This is to work around a bug in SugarCRM 6.0
  # where no fields are returned if no fields are specified
  if fields.length == 0
    mod = Module.find(module_name.classify, @session)
    if mod
      fields = mod.fields.keys
    else
      fields = ["id"]
    end
  end
  return fields.to_json
end

Attempts to return a list of fields for the target of the association. i.e. if we are associating Contact -> Account, using the “contacts” link field name - this will lookup the contacts association and try to determine the target object type (Contact). It will then pull the fields for that object and shove them in the related_fields portion of the get_relationship request.



7
8
9
10
11
12
13
14
15
# File 'lib/sugarcrm/connection/helper.rb', line 7

def resolve_related_fields(module_name, link_field)
  a = Association.new(class_for(module_name), link_field)
  if a.target
    fields = a.target.new.attributes.keys
  else
    fields = ["id"]
  end
  fields.to_json
end

#seamless_loginObject

Performs a seamless login during synchronization.



3
4
5
6
7
8
9
10
11
12
# File 'lib/sugarcrm/connection/api/seamless_login.rb', line 3

def 
  login! unless logged_in?
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}"
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  response = send!(:seamless_login, json)
end

#search_by_module(search_string, modules, opts = {}) ⇒ Object

Returns the ID, module name and fields for specified modules. Supported modules are Accounts, Bugs, Calls, Cases, Contacts, Leads, Opportunities, Projects, Project Tasks, and Quotes.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sugarcrm/connection/api/search_by_module.rb', line 5

def search_by_module(search_string, modules, opts={})
  login! unless logged_in?
  
  options = { 
    :offset => nil, 
    :limit => nil, 
  }.merge! opts
  
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "search_string": "#{search_string}",
      "modules": "#{modules}",
      "offset": #{options[:offset]},
      "max_results": #{options[:limit]}
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  send!(:search_by_module, json)
end

#send!(method, json, max_retry = 3) ⇒ Object Also known as: retry!

Send a request to the Sugar Instance



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sugarcrm/connection/connection.rb', line 73

def send!(method, json, max_retry=3)
  if max_retry == 0
    raise SugarCRM::RetryLimitExceeded, "SugarCRM::Connection Errors: \n#{@errors.reverse.join "\n\s\s"}"
  end
  @request  = SugarCRM::Request.new(@url, method, json, @options[:debug])
  # Send Ze Request
  begin
    if @request.length > 3900
      @response = @connection.post(@url.path, @request)
    else
      @response = @connection.get(@url.path.dup + "?" + @request.to_s)
    end
    return handle_response
  # Timeouts are usually a server side issue
  rescue Timeout::Error => error
    @errors << error
    send!(method, json, max_retry.pred)
  # Lower level errors requiring a reconnect
  rescue Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPIPE, EOFError => error
    @errors << error
    reconnect!
    send!(method, json, max_retry.pred)
  # Handle invalid sessions
  rescue SugarCRM::InvalidSession => error
    @errors << error
    old_session = @sugar_session_id.dup
    login!
    # Update the session id in the request that we want to retry.
    json.gsub!(old_session, @sugar_session_id)
    send!(method, json, max_retry.pred)
  end    
end

#set_campaign_merge(targets, campaign_id) ⇒ Object

Performs a mail merge for the specified campaign.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/sugarcrm/connection/api/set_campaign_merge.rb', line 3

def set_campaign_merge(targets, campaign_id)
  login! unless logged_in?
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "targets": #{targets.to_json},
      "campaign-id": "#{campaign_id}"
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  send!(:set_campaign_merge, json)
end

#set_document_revision(document_id, revision_number, opts = {}) ⇒ Object

Sets a new revision for a document.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sugarcrm/connection/api/set_document_revision.rb', line 3

def set_document_revision(document_id, revision_number, opts={})
  options = { 
    :file => '', 
    :file_name => '',
    :document_name => nil
  }.merge! opts
  
  # Raise an exception of we try to pass :file, but not :file_name
  if (!options[:file].empty? && options[:file_name].empty?)
    raise ArgumentException, ":file_name must be specified if :file is specified"
  end
  
  # If no document_name is given, use the file_name
  options[:document_name] ||= options[:file_name]
  
  login! unless logged_in?
  
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "document_revision": {
         "id": "#{document_id}",
         "document_name": "#{options[:document_name]}",
         "revision": "#{revision_number}",
         "filename": "#{options[:file_name]}",
         "file": "#{b64_encode(options[:file])}"
      }
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  send!(:set_document_revision, json)
end

#set_entries(module_name, name_value_lists) ⇒ Object

Creates or updates a list of SugarBeans.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/sugarcrm/connection/api/set_entries.rb', line 3

def set_entries(module_name, name_value_lists)
  login! unless logged_in?
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "module_name": "#{module_name}",
      "name_value_list": #{name_value_lists.to_json}
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  send!(:set_entries, json)
end

#set_entry(module_name, name_value_list) ⇒ Object

Creates or updates a single SugarBean.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/sugarcrm/connection/api/set_entry.rb', line 3

def set_entry(module_name, name_value_list)
  login! unless logged_in?
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "module_name": "#{module_name}",
      "name_value_list": #{name_value_list.to_json}
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  send!(:set_entry, json)
end

#set_note_attachment(id, filename, file, opts = {}) ⇒ Object

Creates or updates an attachment on a note



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sugarcrm/connection/api/set_note_attachment.rb', line 3

def set_note_attachment(id, filename, file, opts={})
  options = { 
    :module_id => '', 
    :module_name => '' 
  }.merge! opts
  
  login! unless logged_in?
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
         "note": {
            "id": "#{id}",
            "filename": "#{filename}",
            "file": "#{b64_encode(file)}",
            "related_module_id": "#{options[:module_id]}",
            "related_module_name": "#{options[:module_name]}" 
         }
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  send!(:set_note_attachment, json)
end

#set_relationship(module_name, module_id, link_field_name, related_ids, opts = {}) ⇒ Object

Sets a single relationship between two SugarBeans.

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sugarcrm/connection/api/set_relationship.rb', line 3

def set_relationship(module_name, module_id, link_field_name, related_ids, opts={})
  login! unless logged_in?
  options = { 
    :name_value_list => [], 
    :delete => 0, 
  }.merge! opts
  raise ArgumentError, "related_ids must be an Array" unless related_ids.class == Array
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "module_name": "#{module_name}",
      "module_id": "#{module_id}",
      "link_field_name": "#{link_field_name}",
      "related_ids": #{related_ids.to_json},
      "name_value_list": #{options[:name_value_list].to_json},
      "delete": #{options[:delete]}  
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  # TODO:  Add a handler for the response.  By default it returns a hash like:
  # {failed => 0, deleted => 0, created => 1}.  We should add this to the 
  # Response.handle method and return true/false depending on the outcome.
  send!(:set_relationship, json)
end

#set_relationships(module_names, module_ids, link_field_names, related_ids) ⇒ Object

Sets multiple relationships between two SugarBeans.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sugarcrm/connection/api/set_relationships.rb', line 3

def set_relationships(module_names, module_ids, link_field_names, related_ids)
  login! unless logged_in?
  
  [module_names, module_ids, link_field_names, related_ids].each do |arg|
    raise ArgumentError, "argument must be an Array" unless arg.class == Array
  end
  
  json = <<-EOF
    {
      "session": "#{@sugar_session_id}",
      "module_names": "#{module_names.to_json}",
      "module_ids": #{module_ids.to_json},
      "link_field_names": #{link_field_names.to_json},
      "related_ids": #{related_ids.to_json}   
    }
  EOF
  json.gsub!(/^\s{6}/,'')
  send!(:set_relationships, json)
end