Home > Software engineering >  How do I upload a pdf in Notes & Files section of a specific Opportunity in salesforce?
How do I upload a pdf in Notes & Files section of a specific Opportunity in salesforce?

Time:10-06

I am using python salesforce client, simple_salesforce to create Opportunities in salesforce. I am trying to upload pdf in Notes & Files section of a specific Opportunity object but unable to find any example or source, will appreciate help.

Here's how I am creating opportunity:

from simple_salesforce import Salesforce
sf = Salesforce(username=username, password=security_token)
sf.Opportunity.create({'Name':organization_name,'Amount':priceTotal, "Profitability__c": netMargin, "proposal_id__c": proposal_id'), "StageName": "Qualification", "CloseDate": str(date.today())})

CodePudding user response:

I found the solution:

from simple_salesforce import Salesforce
sf = Salesforce(username=username, password=security_token)

try:
    # retrieving object id
    opportunity_id = opportunity.get('records')[0].get('Id')
    
    file_name = path_of_file
    # uploading pdf
    fields = {'title' : f"{datetime.today()}",'PathOnClient' : f"{file_name}.pdf",'VersionData' : encoded_string.decode('ascii')}
    content_version_id = sf.ContentVersion.create(fields)
   
    query = f"SELECT ContentDocumentId FROM ContentVersion WHERE Id= '{content_version_id['id']}'"
    cdId = sf.query(query)
   
    tr = sf.ContentDocumentLink.create({"ContentDocumentId": cdId['records'][0]['ContentDocumentId'],"LinkedEntityId": opportunity_id,"Visibility": "AllUsers"})
    
    print("pdf uploaded")
except Exception as e:
    print(e)
  • Related