Python - 在NetSuite中创建/读取/更新/删除记录

时间:2018-06-14 15:12:54

标签: python soap wsdl netsuite

我想知道如何创建/读取/更新/删除记录及其标准/自定义字段,使用带有NS WSDL的python SOAP

1 个答案:

答案 0 :(得分:5)

这是一个脚本,提供了如何登录,然后在NetSuite中CRUD记录的示例。

from zeep import Client

 WSDL_URL = 'https://webservices.netsuite.com/wsdl/v2017_2_0/netsuite.wsdl' #WSDL we're using (don't change)
 NS_EMAIL= 'NSemail@email.com' #NS-Email
 NS_PASSWORD = 'NSPassword' #NS-Password
 NS_ROLE = 'NSroleId' #NS-Role Id
 NS_ACCOUNT = 'NSaccountId' #NS-Account Id
 NS_APPID = 'NS268FD3-8553-4464-AEEB-FB6BE2EE616E'  #NS App-ID

 def make_app_info(client): #make app info, used in login_client
     AppInfo = client.get_type('ns4:ApplicationInfo')
     app_info = AppInfo(applicationId = NS_APPID)
     return app_info

 def make_passport(client): #make passport, used in login_client
     RecordRef = client.get_type('ns0:RecordRef')
     Passport = client.get_type('ns0:Passport')
     role = RecordRef(internalId=NS_ROLE)
     return Passport(email=NS_EMAIL,
                 password=NS_PASSWORD,
                 account=NS_ACCOUNT,
                 role=role)

 def login_client(): #login
     client = Client(WSDL_URL)
     login = client.service.login(passport=make_passport(client), _soapheaders={'applicationInfo': make_app_info(client)}, )
     return client 

 def add_customer(): #add a customer, example
     client = login_client()
     RecordRef = client.get_type('ns0:RecordRef') #set RecordRef as a RecordReference
     StringCustomFieldRef = client.get_type('ns0:StringCustomFieldRef') #custom string field ref
     CustomFieldList = client.get_type('ns0:CustomFieldList') #translator for custom field list

     #customField acctName
     acctName = StringCustomFieldRef() #this custom field is a string
     acctName.internalID = '1569'
     acctName.scriptId = 'custentity_sf_account_name'
     acctName.value = 'Test data'

     #customField acctId
     acctID= StringCustomFieldRef()
     acctID.internalId= '1596'
     acctID.scriptId= 'custentity_account_id'
     acctID.value = 'More Test data'

     Customer = client.get_type('ns13:Customer') #make customer of type Customer
     customer = Customer( #set customer
         companyName='TEST',
         entityId='TEST (US LLC)',
         subsidiary = RecordRef(internalId='5', type='subsidiary'), #subsidiary is a RecordRef field, so you need to use RecordRef
         customFieldList = CustomFieldList([acctID,acctName]) #set custom Fields
     )
     response = client.service.add(customer) #make the call
     print(response)

 def get_customer(id): #fetch customer
     client = login_client()
     Record = client.get_type('ns0:RecordRef') #set Record = to RecordReference
     record = Record(internalId=id, type='customer') #change type for different type searches
     response = client.service.get(record)
     r = response.body.readResponse
     #print(r) #detailed view
     if r.status.isSuccess:
         print(r.record) #r.record.companyName #simple view

 def update_customer(): #update a customer
     client = login_client()
     Customer = client.get_type('ns13:Customer') #set Customer = to NS customer type
     customer = Customer(
         internalId = '451348', #must specify internalId
         companyName='Testing 123'
     )
     response = client.service.update(customer) #upsert can also be used
     print(response)

 def delete_customer(id): #delete
     client = login_client()
     Record = client.get_type('ns0:RecordRef') #set Record to RecordReference
     record = Record(internalId=id, type='customer')
     response = client.service.delete(record) #run command
     print(response)

 def add_order(): #add an order
     client = login_client()
     RecordRef = client.get_type('ns0:RecordRef')
     SelectCustomFieldRef = client.get_type('ns0:SelectCustomFieldRef')
     StringCustomFieldRef = client.get_type('ns0:StringCustomFieldRef')
     CustomFieldList = client.get_type('ns0:CustomFieldList')
     SalesOrderItemList = client.get_type('ns19:SalesOrderItemList')
     Order = client.get_type('ns19:SalesOrder')
     SalesOrderItem = client.get_type('ns19:SalesOrderItem')
     billtier = SelectCustomFieldRef() #using SelectCustomFieldRef since custom field is a select
     billtier.internalId = '308' #custField InternalId
     billtier.scriptId = 'custbody_bill_to_tier' #custField scriptId
     billtier.value ={
                     'internalId': '2', #option ID
                     'externalId': None,
                     'typeId': '12' #list Id
     }
     item1 = SalesOrderItem() #making a single lineitem
     item1.quantity = '1'
     item1.item = RecordRef(internalId='5816')
     item1.amount = 999
     order = Order(
         entity = RecordRef(internalId='451656', type='customer'),
         itemList= SalesOrderItemList([item1]), #Add the line item
         customFieldList = CustomFieldList([billtier]) #add the custom fields
     )
     response = client.service.add(order) #add the order
     print(response)

一些注释: