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



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

下面是一个脚本,它给出了一个关于如何登录的示例,然后在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)

一些注意事项:

  • 如果它说:RecordRef = client.get_type('ns0:RecordRef')ns0是指 WSDL 上的"导入"部分中的命名空间 .xsd 文件。

  • 所有NetSuite对象及其字段的列表可以在这里找到:http://www.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2016_1/schema/other/salesorderitemlist.html?mode=package

  • 有关处理不同类型的自定义字段时要使用的字段引用的详细信息: https://system.na1.netsuite.com/app/help/helpcenter.nl?fid=section_n3458179.html

相关内容

  • 没有找到相关文章

最新更新