Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retrieve method fails if only one record found #32

Closed
hynekcer opened this issue Jun 18, 2016 · 4 comments
Closed

retrieve method fails if only one record found #32

hynekcer opened this issue Jun 18, 2016 · 4 comments

Comments

@hynekcer
Copy link
Contributor

hynekcer commented Jun 18, 2016

I use demo.py for tests. Your original current source fails on an empty Salesforce instance or if no Account "New Account" has been present before running the demo.

Traceback (most recent call last):
  File "demo.py", line 235, in <module>
    demo.retrieve()
  File "demo.py", line 154, in retrieve
    print str(acc[beatbox._tSObjectNS.Id]) + " : " + str(acc[beatbox._tSObjectNS.Name])
  File "/home/.../Beatbox/xmltramp.py", line 143, in __getitem__
    raise KeyError, n
KeyError: ('urn:sobject.partner.soap.sforce.com', 'Id')

It is because beatbox.Client.retrive returns directly one xmltramp.Element instead of a list of length 1.

The backward compatible solution is to modify only the demo.py so that it is tested if the result is a list or an Element :

     def retrieve(self):
         print("\nretrieve")
         accounts = svc.retrieve("id, name", "Account", self.__theIds)
+        if not beatbox.islst(accounts):
+            accounts = [accounts]
         for acc in accounts:

... but this is not a demo of advisable clean code.
A more natural solution would be to return a list of elements by beatbox.Client.retrieve if the ids parameter is a list, otherwise to return an element if an Id is used directly without list.

         def retrieve(self, fields, sObjectType, ids):^M
                 return RetrieveRequest(self.__serverUrl, self.sessionId, fields, sObjectType, ids
-                                       ).post(self.__conn)
+                                       ).post(self.__conn, islst(ids))

The best solution is probably to add a retrive method to IterClient class and also to write an example with IterClient to the documentation because that class is very useful. It will be both natural and backward compatible. What do you mean?

@superfell
Copy link
Owner

Perhaps I'm missing understanding. but seems like demo.py should just make sure that __theIds has at least 2 records in it, then everything will behave as expected.

Ideally the behavior of retrieve should return an array if the id list was an array, but i think its too late to change that unless we add retrieve2 or something.

Yes, adding retrieve to IterClient is a good idea.

@hynekcer
Copy link
Contributor Author

There is one Account created, updated, deleted, undeleted by demo and finally there is exactly by one Account more that at the beginning. In order to pass the script there must be finally at least two records with the name "New Account" that is not usual even in a live SF database. Yes, I was surprised that it failed always only for the first time in a new sandbox and I was anxious if I hadn't made a bug..

hynekcer added a commit to hynekcer/Beatbox that referenced this issue Jul 2, 2016
@hynekcer
Copy link
Contributor Author

hynekcer commented Jul 2, 2016

I fixed also the demo that it can continue on a plain sandbox without custom fields. Only a warning is reported.

@hynekcer
Copy link
Contributor Author

hynekcer commented Jul 6, 2016

I'm thinking about a forward compatibility switch, that would be in a backward compatibility position by default. Another example: if the result has an element <size>1</size> in partner wsdl namespace, then the result should be a list. The type of result should be predictable by the type of request. I don't want to do anything for it until anything really important is found for a backward/forward compatibility switch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants