Hello again,
I hope you've checked out the part 1 of this post where we auto-create packages and shipments for your offline orders. This is part 2, and here we will see how we can automatically create packages and manual shipment orders for your online sales orders (or in other words, orders generated from Amazon, Etsy, Ebay or Shopify).
Setting up the custom function:
Open your Zoho Inventory organization.
Navigate to Settings using the gear icon from the top-right corner.
Select Automation. Click on the button adjacent to +New Workflow Rule.
Select Custom Functions from the drop-down.
Add a name for this workflow rule and make sure that there are no spaces between words.
Select Sales Orders against module.
Add a description if you need one.
Now, the workflow type is going to be Event Based.
The condition to set shall be, "When a Sales Order is created".
Now copy and paste this code fragment into the deluge pane:
salesorderID = salesorder.get("salesorder_id");
salesorderdate = salesorder.get("date").toDate();
organizationID = organization.get("organization_id");
authtoken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
//replace the value with your authtoken value
sal = invokeurl
[
type :GET
];
temp = sal.get("salesorder");
// info temp;
new = temp.get("sales_channel");
info new;
if(!new.equalsIgnoreCase("direct_sales"))
{
mapper = Map();
customerID = salesorder.get("customer_id").toString();
//uncomment the below two lines if you had not enabled auto package number generation
//package_number = salesorder.get("salesorder_number").replaceFirst("SO","PA");
//mapper.put("package_number",package_number);
mapper.put("customer_id",customerID);
mapper.put("date",salesorderdate);
lineItems = salesorder.get("line_items").toList();
newLineItems = List();
for each lineItem in lineItems
{
if(lineItem.get("name").isNull())
{
continue;
}
lineItemMap = Map();
solineitemID = lineItem.get("line_item_id");
lineItemMap.put("so_line_item_id",solineitemID);
quantity = lineItem.get("quanity");
lineItemMap.put("quantity",quantity);
newLineItems.add(lineItemMap);
}
mapper.put("line_items",newLineItems);
jsonString = Map();
jsonString.put("JSONString",mapper);
info jsonString;
response = invokeurl
[
type :POST
parameters:jsonString
];
info mapper;
info response.toMap().get("message");
pack=response.get("package");
packid=pack.get("package_id");
ship=Map();
ship.put("date",salesorderdate);
ship.put("delivery_method","dhl"); //REPLACE WITH YOUR SHIPPING CARRIER NAME
jsons=Map();
jsons.put("JSONString",ship);
resp = invokeurl
[
type: POST
parameters: jsons
];
info resp.toMap().get("message");
}
else
{
info "SO from direct sales";
}
Copy and replace your authtoken number inside the code.
You can change the carrier name/shipping medium by making changes to this line: ship.put("delivery_method","dhl"); //REPLACE WITH SHIPPING CARRIER NAME.
Note: If you have enabled auto-generation of package numbers for your organization, then you don't need these two lines:
//package_number = salesorder.get("salesorder_number").replaceFirst("SO","PA");
//mapper.put("package_number",package_number);
Henceforth, whenever an online sales order gets generated within Zoho Inventory, a package and a manual shipment record will be created for it.
Until next time.
Your everyday end user,
Ajay Aadhithya Chandrasekaran
Zoho Inventory