4

I am using Djnago & Salesforce.

I establish connection between them through simple-salesforce

I have created a custom picklist on Contact Object of Salesforce.

I wont to fetch all the 20 values of my picklist & display in Django.

I am looking for SOQL to fetch values from Salesforce.

sf = Salesforce(instance_url='https://test.salesforce.com', session_id='')
sf1 = Salesforce(connection parameters)
sf3 = sf1.query("SELECT Color__c FROM Contact")

sf3 will give me values set for respective Contact records.

I want to fetch all the 20 values I have entered while creating Color__c on Contact object. In Apex we can do that, something like below

public class PicklistUtil { 
    public static List<Schema.PicklistEntry> getContactColor() { 
        return Contact.Color__c.getDescribe().getPicklistValues(); 
   } 
}

I am looking to do the same in Django. Can someone please help?

davejagoda
  • 2,420
  • 1
  • 20
  • 27
Jeevan Bodas
  • 148
  • 5
  • 13

2 Answers2

5

There might be a better way, but the following should work:

d = sf1.Contact.describe()
for f in d['fields']:
    if f['name'] == 'Color__c':
        break
picklist = f['picklistValues']

picklist should then be a list of OrderedDicts.

user2390182
  • 72,016
  • 6
  • 67
  • 89
  • 1
    @schwobaseggl Do you know how to update/insert picklist values through this API? – Matt W. Oct 31 '18 at 19:17
  • @MattW. did you find an answer to this? Im trying to do exactly that but it doesnt seem possible – Kevin Müller Apr 02 '19 at 13:07
  • @KevinMüller I haven't found an answer unfortunately. – Matt W. Apr 02 '19 at 13:19
  • @MattW. I managed to create a new picklist atleast. explained [here](https://stackoverflow.com/questions/55537360/updating-picklist-in-salesforce-using-python) Maybe you can find a way to edit a existing list using this – Kevin Müller Apr 10 '19 at 12:58
  • @MattW. Finally managed to update a picklist in Salesforce using Python. In case you still need to do that look my answer here --> https://stackoverflow.com/questions/55537360/updating-picklist-in-salesforce-using-python – Kevin Müller Apr 15 '19 at 09:00
  • @KevinMüller Ah thank you- that helps. One thing in particular I'm really trying to solve now is adding a chatter on an object, like account. Any idea how to do that? – Matt W. Apr 16 '19 at 13:14
  • @MattW. Sadly I never worked with Chatter so I dont think im able to help you :c – Kevin Müller Apr 17 '19 at 06:07
  • 1
    @KevinMüller bummer! oh well, thanks for your help on the picklist – Matt W. Apr 17 '19 at 14:43
  • Thanks this is the only thing that worked! From there I return a list of tuples i.e. (p['label'], p['value']) – Brendan Metcalfe Jan 20 '21 at 19:11
0

Picklist values can be obtained also directly in choices attribute of the field as a part of Model exported by

manage.py inspectdb --database=salesforce table_names...

with django-salesforce.

hynekcer
  • 14,942
  • 6
  • 61
  • 99