I am using the python salesforce toolkit for SFDC integration with a home grown application. We have nightly routines to read and write to SFDC. Based on a condition, I need to set the date value to current date or reset it to blank. I can set the date value, no issues. But I am unable to set the value to blank.
data = {}
if condition==True:
data['Termination_Date__c'] = '2017-05-12'
else:
data['Termination_Date__c'] = ??
I've tried to use '', 'null' and None while trying to blank out the value but nothing works so far. I am sure it's an easy solution but just can't find a way out. Any help would be appreciated.
Salesforce reads NULL values as None. try:
data['Termination_Date__c'] = None
Although, when I tried:
sf.Contact.update(data[0],{'Email' : Email1, 'MailingStreet' : ''})
It set my MailingStreet to nothing at all so '' seemed to have worked for me.
I found a solution to the question I posted. It's true that you can set '' to string fields to update them in SFDC. But certain fields like Date won't accept an empty string because of the validation rules in SFDC. A python None doesn't work either. But the toolkit has a way to handle such situation. Here's how you can blank/null the field in SFDC:
data = {}
if condition==True:
data['Termination_Date__c'] = '2017-05-12'
else:
data['fieldsToNull'] = ['Termination_Date__c']
Related
I am using python 3. I use database to lookup the country of IP, if the IP is not in the database, return None or AAA. After all process, show the return result (either a country name or None or AAA ) in a new column on the data frame, Specifically, I have three columns that are date time and IP in the data frame, I would like to add a new column "code1" that shows the country name or None of this IP.
I add error exception in the code, however, it doesn't work. The code and error message are as below. Can anyone help?
The following is the report when I add "from geoip2.errors import AddressNotFoundError" to my code. I guess it means when there is address not found in the database, it return nothing rather a "none" (see, there is a empty square brackets), therefore, when I use df['code1']=code1, it reports the number of value is not equal to the number of index. (I am not sure).
In fact,my aim is to add a column to my original data frame that report the country of each ip or none if it is not in the database. Is there any other way to do so instead of " df['code1']=code1 "? Any help will be appreciated. Thanks
Exceptions are just like any other object in Python. They have to be defined somewhere. In this case, AddressNotFoundError is not defined so the interpreter has no idea what it means.
It has to be imported:
from geoip2.errors import AddressNotFoundError
or
import geoip2.errors.AddressNotFoundError
See relevant part of geoip2 documentation.
You can just leave out the AddressNotFoundError and instead do
try:
response1 = reader.country(row1)
code1.append(response1.country.iso_code)
except:
response1 = None
but if you really want to just except the AddressNotFoundError then at the top do
import geoip.errors.AddressNotFoundError
I am not well versed at python at all. I was asked to review someone else's python script that uses search ldap entries. Btw - I can't reach out to original developer for some reason and before it is deployed + tested visual code checking is required. With that constraints in mind, allow me to proceed.
import ldap3
from ldap3 import Server,Connection, ALL
conn = Connection(....)
conn.search(....)
for entry in conn.entries:
if (len(entry['cn']) > 0):
....
name = entry['name']
if name:
user_name = str(name)
else:
user_name = "Bob"
First question is len(entry['cn']) > 0 I like to interpret it as checking the length of characters of returned cn value from ldap e.g. cn=bob,ou=people,ou=foocomany. I am pretty sure entry['cn'] is NOT string type but I don't know what data type it represents. Can you tell me what type it is?
My 2nd + 3rd questions are not directly related to the original question, but plz bear with me asking for them with grace.
My 2nd question is, if that assumption is correct, entry['cn'] should be converted to string type like str(entry['cn']). Then check its length?
My 3rd question is on if stmt. I like to interpret it as if name is not null or if name is not None in pythonic way. Did I interpret it correctly? If so I should replace it as if not (name is None) would work? I googled on it to get that stmt.
Given the context and code provided, it looks like this snippet is using the ldap3 library.
From the relevant documentation, conn.entries should be a list of Entry objects.
This means that entry['cn'] should be returning an Attribute. Doing a bit of source diving, this appears to just be a fancy list with writable flags. len(entry['cn']) > 0 ends up calling this method, which just returns the number of values that attribute has. It being greater than 0 just ensuring that the cn is, in fact, set.
May I know what is wrong with my below code ? I would like to query all where date_occ is between '2015-01-10T12:00:00' and '2015-12-31T24:00:00'
response = requests.get('https://data.lacity.org/api/id/7fvc-faax.json?$select=*&$where = date_occ between 2015-01-10T12:00:00 and 2015-12-131T24:00:00')
I get the following error:
Unrecognized arguments [$where ]
I realise the following doesn't work as well
response = requests.get('https://data.lacity.org/api/id/7fvc-faax.json?$select=*&vict_age >20')
data = response.json()
data = json_normalize(data)
data = pd.DataFrame(data)
But this works:
response = requests.get('https://data.lacity.org/api/id/7fvc-faax.json?$select=*&vict_sex=M')
what am I missing here?
There are a few questions and answers posed in this one. Starting with your second query first; where you want to look at age above 20 years-old. Looking at the metadata (click the down arrow), the victim age is not numeric and is a text string. Thus, you won't be able to use operators like greater than, less than, etc. However, you can look for "equal to". The query below will work:
https://data.lacity.org/resource/7fvc-faax.json?$where=vict_age = '20'
Note: I've dropped the $select and am just using $where for simpler display.
Your third example works since you've set it to query a text field. If you want LA to change it to a numeric, click the "Contact Dataset Owner" under the ellipsis button.
Your first question on dates has a few changes. First, your single quotation marks were not aligned and some were missing. Second, the latter date is 2015-12-131T24:00:00, which has an invalid day. Finally, the data on the portal does not have a timestamp, so you only need the year-month-day. This will work:
https://data.lacity.org/resource/7fvc-faax.json?$where=date_occ between '2015-01-10' and '2015-12-13'
Finally, I would recommend that you use the URL structure, https://data.lacity.org/resource/7fvc-faax.json? instead of /api/id/. The former is the proper URL structure for Socrata-based APIs.
I'm new to LDAP. So I don't really know all my terms and fully understand all the terms yet. However, I'm working on an existing system and all the set up is done. I'm just adding a method to it.
I'm trying to write a method in Python using LDAP query. I've played around on LDAP Browser and can see that my query is correct. However, I'm not sure how to put it in a python method to return a list. The method needs to return a list of all the users' username. So far I have:
def getUsersInGroup(self, group):
searchQuery= //for privacy Im not going to share this
searchAttribute=["username"]
results = self.ldap.search_s(self.ldap_root, ldap.SCOP_SUBTREE,
searchQuery, searchAttribute)
I'm unsure how to go from here. I don't fully understand what the search_s method returns. I read online that its better to use search_s over search method because the while loop can be avoided. Could you please provide and example of where I can go from here. Thanks.
You need to perform a LDAP search something like:
# Find all Groups user is a member of:
import ldap
l = ldap.initialize("ldap://my_host")
l.simple_bind_s("[my_dn]", "[my_pass]")
myfilter = "(member=(CN=UserName,CN=Users,DC=EXAMPLE,DC=COM))"
# for all groups including Nested Groups (Only Microsoft Active Directory)
# (member:1.2.840.113556.1.4.1941:=CN=UserName,CN=Users,DC=EXAMPLE,DC=COM)
ldap_result = l.search("[BASE_DN]", ldap.SCOPE_SUBTREE, myfilter, None)
res_type, data = l.result(ldap_result, 0)
print(data)
You need to use the full dn of the user.
I have these code written in Python using SQLAlchemy.
dailyCashFlow = models.DailyCashFlow(day=day, cash_start_of_day = cash_start_of_day, cash_end_of_day = cash_end_of_day)
branch.dailyCashFlow.append(dailyCashFlow)
db.session.commit()
result['result'] = True
result['message'] = string.join(['Daily cashflow saved successfully with ID=#'], `dailyCashFlow.id`)
The relationship is roughly : company->branches->dailyCashflow.
I want to get the id of the newly inserted dailyCashFlow.id. I think I can easily refer back to dailyCashFlow object to get the id, but it's not. It use lazy='dynamic' set up in the model. Is that the reason? And, how can I easily get the id of the newly inserted data?
Do your mean is got id by inserted data?
result = tb_conn.insert().execute(data)
id = result.inserted_primary_key[0]
I pasted my revelation to this questio
"You're right zzzeeek! That is also my understanding, if you inspect closely this code, string.join(['Daily cashflow saved successfully with ID=#'], dailyCashFlow.id), you will see that the ] is introduce too early. It should be like this : string.join(['Daily cashflow saved successfully with ID=#', dailyCashFlow.id)]. Well, I guess now I get a good experience why unit test is a must.. :) - If you want moving your reply to question section, I would gladly accept it as the accepted answer. "
So visitor to this page will know what did I do wrong ;)