Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Create/Read/Update/Delete records in NetSuite

I was wondering how to Create/Read/Update/Delete records and their standard/custom fields, using python SOAP with NS WSDL

like image 727
bogus Avatar asked Jan 28 '23 22:01

bogus


1 Answers

Here's a script that gives an example on how to login, and then to CRUD the records in NetSuite.

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= '[email protected]' #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)

Some Notes:

  • In cases where it says: RecordRef = client.get_type('ns0:RecordRef') the ns0 refers to the namespace .xsd file that is in the import section on the wsdl.

  • A list of all NetSuite objects and their fields can be found here: http://www.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2016_1/schema/other/salesorderitemlist.html?mode=package

  • Details about field references to use when handling different types of custom fields: https://system.na1.netsuite.com/app/help/helpcenter.nl?fid=section_n3458179.html

like image 129
bogus Avatar answered Jan 30 '23 13:01

bogus