-
Notifications
You must be signed in to change notification settings - Fork 36
Manipulating Entries
Get entry list is used to get a list of entries from the sugarcrm database. The function itself can be found here and it looks like this:
def get_entry_list(self, module_name, query ="", order_by ="", offset = 0, select_fields = [], link_name_to_fields_array = []):
We have already discussed how modules work and you will put that into the module_name parameter. We will be using Accounts for these examples. Now if you just put in a module name and leave everything else blank, as such:
entry_list = session.get_entry_list('Accounts',"","","",[],[])
You will get a really long list of every entry in the Accounts module. So it's important to specify a bit with what you are looking for. Let's use a specific industry type to focus our search a bit. We will use the query field to do this.
entry_list = session.get_entry_list('Accounts', "accounts.industry = 'retail'", "","",[],[])
or
entry_list = session.get_entry_list('Accounts', "accounts.billing_address_state = 'CA'","","",[],[])
Adding queries like this will help limit your search a bit, but it will still come back with way more information then we need. So we need to be a little bit more specific with our searches. Now we will add information to the select fields parameter.
entry_list = session.get_entry_list('Accounts', "accounts.industry = 'retail'", "","",['id','sic_code'][])
You get much shorter output and closer to what we want, which looks like this:
##Get Entry Count
We can find out how many entries exist in a specific query. The get_entries_count() function can be found here The function call looks like this:
get_entries_count(self, module_name, query = "", deleted = False):
So you can see that it uses the same parameters as in the get entry list call. So we will use the same parameters as above to see how many entries are in the query that we gave get_entry_list().
entries_count = session.get_entries_count('Accounts', "accounts.industry = 'retail'")
Which will give us the following output:
So this specific query, given to get_entry_list(), gives us the result of how many field entries exist there.
##Set Entry
The set entry function is used to add new entries to the sugarCRM database. The code for set entry can be found here and the function definition looks like this:
set_entries(self,module_name, name_value_lists)
There are two things that are important with this function call. The module name and the name value lists. We have already gone into modules pretty thoroughly, so the new item to look at is name value lists. This is an array of values that we store in the database. We make a call to a separate function, to easily create a new name value list, which you can find here. Now normally you would have to deal with a lot of redundancy when creating name value lists, but this function helps to eliminate that. So let's go ahead and create a new name value list.
first_nvl = sugarcrm.toNameValueList({'name':'Freds Printing','billing_address_state':'OH','industry':'Retail'})
Now we have a new name value lists with the name "Freds Printing" and of industry type Retail. Now we take that first_nvl and put that into the set entries function like this:
first_bean = session.set_entry('Accounts', first_nvl)
Again, we are using the accounts module. This function returns a sugarbean id from the newly created function call and we will use this in a later example in this page. If you output the data for the sugarbean you will get something similar to this:
##Get Entry
The get entry function is used to call a single entry in the sugarcrm database. This is where that sugarbean id will come in handy. The get entry function can be found here and the function call looks like this:
get_entry(self, module_name, id, select_fields = [], link_name_to_fields_array = []):
Now again, we will ignore the link name to fields parameter as we will be explaining relationships in a separate page. We have also already explained how the select fields parameter works and isn't important for this example. For now we will focus on the module name and ids. Now we take that sugar bean id, first bean, and pass that into the get_entry function. It should look something like this:
first_list = get_entry('Accounts', first_bean, "","")
The output from this function will store everything into a new list, stored in the first_list variable. Now we take that and make a call to our NameValueList function, which you can find here. So let's make a call to our NameValueList function which will look like this:
print sugarcrm.fromNameValueList(first_list['entry_list'][0]['name_value_list'])
Now when we print this list, we will get all of the entries and fields that are in our newly created entry. The output looks like this:
##Get Entries
So let's say we want to get multiple entries at once. The code for get_entries can be found here and the function call looks like this:
get_entries(self, module_name, ids = [], select_fields = [], link_name_to_fields_array = []):
The important thing to note here is the id parameter is now an array. This is so we can take multiple beans and pass them to the get entries function and everything together in one list. Let's start off by creating another entry.
second_nvl = sugarcrm.toNameValueList({'name':'Smooth Gravel Software','billing_address_state':'OH','industry':'Retail'})
second_bean = session.set_entry('Accounts', second_nvl)
We now have our second name value list and a second bean id. Now we all put both of the bean ids into an array, like this:
beans = [first_bean,second_bean]
Now we will make a call to get entries and store the list of the two entries.
combined_list = session.get_entries('Accounts', beans)
Now combined list will contain all of the entries given by the array of ids. Now if we want to view the contents of everything in the list, we will make a function call like this:
for x in total_list['entry_list']
print sugarcrm.fromNameValueList(x['name_value_list'])
print '\n'
Which will give us the following output:
So both lists items are output. The top of the example shows the results of the two ids and you can double check with the picture to show that the ids exist in the list.