Zoho FSM | Create a Work Order in FSM from a CRM Sales Order

Create a Work Order in FSM from a CRM Sales Order

Use case: Create a work order in Zoho FSM whenever a sales order is created in Zoho CRM.
 
Follow the steps below to implement this use case:
Step 1: Create a connection for Zoho FSM in Zoho CRM
Step 2: Create a workflow rule
Step 3: Create a custom function

Step 1:  Create a connection for Zoho FSM in Zoho CRM  

To create a connection for Zoho FSM that can be used in Zoho CRM for invoking the Zoho FSM APIs, do the following:
  1. Log in to Zoho CRM.
  2. Navigate to Setup > Developer Hub > Connections and click Create Connection.



  3. Under the Default Services tab, search for and click Zoho FSM.



  4. Do the following and click Create And Connect:
    1. Connection Name: FSMConnection
    2. Select the Scopes ZohoFSM.modules.all



  5. Click Connect in the authentication page.



  6. Click Accept in the authorization page.

In the custom function, where FSM APIs are used via the invokeURL task, use this Connection Link Name.
 

Step 2: Create a workflow rule  

Create a workflow rule in Zoho CRM to create a work order in Zoho FSM whenever a sales order is created in Zoho CRM.
  1. Navigate to Setup > Automation > Workflow Rules and click Create Rule.



  2. Enter the following details, then click Next:
    1. Module: Sales Orders
    2. Rule Name: CreateWOInFSM



  3. Select the rule trigger as Create from Record action and click Next.



  4. Select the rule criteria as All Sales Orders. Click Next.



  5. Click +Action and select Function.



  6. In the Configure Function popup, click Write your own.
    Create a custom function.



  7. Click Save.

Step 3: Create a custom function  

  1. In the Create New Function popup, enter the following and click Create.
    1. Function Name: CreateWOInFSM
    2. Display Name: CreateWOInFSM



  2. In the Deluge Script Editor, click Edit Arguments.



  3. In the Edit Arguments popup, do the following:
    1. Enter the argument name as salesorderId.
    2. For the argument value, type # and select Sales Orders - Sales Order Id and click Done.



  4. Click Save.



  5. Add the below script and click Save.

    salesOrderresp = zoho.crm.getRecordById("Sales_Orders",salesorderId);
    if(salesOrderresp != null)
    {
    Summary = salesOrderresp.get("Subject");
    Account_Name = salesOrderresp.get("Account_Name").toMap().get("name");
    Encoded_Account_Name = zoho.encryption.urlEncode(Account_Name);
    response = invokeurl
    [
    type :GET
    connection:"fsmconnection"
    ];
    if(response != "")
    {
    response_data = response.get("data").toMap();
    FSM_Account_id = response_data.get("id");
    Billing_Address_resp = response_data.get("Billing_Address");
    if(Billing_Address_resp != null)
    {
    Billing_Address_resp = Billing_Address_resp.toMap();
    bill_add_map = Map();
    bill_add_map.put("id",Billing_Address_resp.get("id"));
    }
    Service_Address_resp = response_data.get("Service_Address");
    if(Service_Address_resp != null)
    {
    Service_Address_resp = Service_Address_resp.toMap();
    ship_add_map = Map();
    ship_add_map.put("id",Service_Address_resp.get("id"));
    }
    }
    else
    {
    Account_Id = salesOrderresp.get("Account_Name").toMap().get("id");
    account_resp = zoho.crm.getRecordById("Accounts",Account_Id);
    mobile = account_resp.get("Fax");
    phone = account_resp.get("Phone");
    website = account_resp.get("Website");
    sh_street = account_resp.get("Shipping_Street");
    sh_city = account_resp.get("Shipping_City");
    sh_state = account_resp.get("Shipping_State");
    sh_code = account_resp.get("Shipping_Code");
    sh_country = account_resp.get("Shipping_Country");
    bi_street = account_resp.get("Billing_Street");
    bi_city = account_resp.get("Billing_City");
    bi_state = account_resp.get("Billing_State");
    bi_code = account_resp.get("Billing_Code");
    bi_country = account_resp.get("Billing_Country");
    createMap = Map();
    createMap.put("Company_Name",Account_Name);
    createMap.put("Mobile",mobile);
    createMap.put("Phone",phone);
    createMap.put("Website",website);
    Service_Address_Map = Map();
    Service_Address_Map.put("Address_Name",Account_Name + "Shipping Address");
    Service_Address_Map.put("Street_1",sh_street);
    Service_Address_Map.put("City",sh_city);
    Service_Address_Map.put("State",sh_state);
    Service_Address_Map.put("Zip_Code",sh_code);
    Service_Address_Map.put("Country",sh_country);
    Billing_Address_Map = Map();
    Billing_Address_Map.put("Address_Name",Account_Name + "Billing Address");
    Billing_Address_Map.put("Street_1",bi_street);
    Billing_Address_Map.put("City",bi_city);
    Billing_Address_Map.put("State",bi_state);
    Billing_Address_Map.put("Zip_Code",bi_code);
    Billing_Address_Map.put("Country",bi_country);
    createMap.put("Service_Address",Service_Address_Map);
    createMap.put("Billing_Address",Billing_Address_Map);
    responze = zoho.fsm.createRecord("Companies",createMap);
    FSM_Account_id = responze.get("data").toMap().get("Companies").toMap().get("id");
    temp_company_resp = zoho.fsm.getRecordById("Companies",FSM_Account_id);
    response_data_x = temp_company_resp.get("data").toMap();
    Billing_Address_resp = response_data_x.get("Billing_Address");
    if(Billing_Address_resp != null)
    {
    Billing_Address_resp = Billing_Address_resp.toMap();
    bill_add_map = Map();
    bill_add_map.put("id",Billing_Address_resp.get("id"));
    }
    Service_Address_resp = response_data_x.get("Service_Address");
    if(Service_Address_resp != null)
    {
    Service_Address_resp = Service_Address_resp.toMap();
    ship_add_map = Map();
    ship_add_map.put("id",Service_Address_resp.get("id"));
    }
    }
    Contact_Id = salesOrderresp.get("Contact_Name").toMap().get("id");
    Contact_Resp = zoho.crm.getRecordById("Contacts",Contact_Id);
    Contact_Email = Contact_Resp.get("Email");
    responze = invokeurl
    [
    type :GET
    connection:"fsmconnection"
    ];
    if(responze.toString() != "")
    {
    responze = responze.toMap();
    FSM_Contact_id = responze.get("data").toMap().get("id");
    }
    else
    {
    Salutation = Contact_Resp.get("Salutation");
    First_Name = Contact_Resp.get("First_Name");
    Last_Name = Contact_Resp.get("Last_Name");
    Phone = Contact_Resp.get("Phone");
    Mobile = Contact_Resp.get("Mobile");
    bi_Street = Contact_Resp.get("Mailing_Street");
    bi_city = Contact_Resp.get("Mailing_City");
    bi_state = Contact_Resp.get("Mailing_State");
    bi_code = Contact_Resp.get("Mailing_Zip");
    bi_country = Contact_Resp.get("Mailing_Country");
    sh_street = Contact_Resp.get("Other_Street");
    sh_city = Contact_Resp.get("Other_City");
    sh_state = Contact_Resp.get("Other_State");
    sh_code = Contact_Resp.get("Other_Zip");
    sh_country = Contact_Resp.get("Other_Country");
    createMap = Map();
    createMap.put("Salutation",Salutation);
    createMap.put("First_Name",First_Name);
    createMap.put("Last_Name",Last_Name);
    createMap.put("Email",Contact_Email);
    createMap.put("Phone",Phone);
    createMap.put("Mobile",Mobile);
    createMap.put("Company",FSM_Account_id);
    Service_Address_Map = Map();
    Service_Address_Map.put("Address_Name",First_Name + " " + Last_Name + " Shipping Address");
    Service_Address_Map.put("Street_1",sh_street);
    Service_Address_Map.put("City",sh_city);
    Service_Address_Map.put("State",sh_state);
    Service_Address_Map.put("Zip_Code",sh_code);
    Service_Address_Map.put("Country",sh_country);
    Billing_Address_Map = Map();
    Billing_Address_Map.put("Address_Name",First_Name + " " + Last_Name + " Billing Address");
    Billing_Address_Map.put("Street_1",bi_Street);
    Billing_Address_Map.put("City",bi_city);
    Billing_Address_Map.put("State",bi_state);
    Billing_Address_Map.put("Zip_Code",bi_code);
    Billing_Address_Map.put("Country",bi_country);
    createMap.put("Service_Address",Service_Address_Map);
    createMap.put("Billing_Address",Billing_Address_Map);
    cr_resp = zoho.fsm.createRecord("Contacts",createMap);
    FSM_Contact_id = cr_resp.get("data").toMap().get("Contacts").toMap().get("id");
    }
    i = 1;
    servive_line_list = list();
    product_List = salesOrderresp.get("Product_Details").toList();
    for each  each_product in product_List
    {
    temp_each_product = each_product.toMap();
    quantity = temp_each_product.get("quantity");
    unit_price = temp_each_product.get("unit_price");
    product_Name = temp_each_product.get("product").toMap().get("name");
    Encoded_product_Name = zoho.encryption.urlEncode(product_Name);
    rezponse = invokeurl
    [
    type :GET
    connection:"fsmconnection"
    ];
    if(rezponse != "")
    {
    FSM_Product_id = rezponse.get("data").toMap().get("id");
    }
    else
    {
    product_id = each_product.toMap().get("product").toMap().get("id");
    product_resp = zoho.crm.getRecordById("Products",product_id);
    Unit = product_resp.get("Usage_Unit");
    Unit_Price = product_resp.get("Unit_Price");
    Type = product_resp.get("Product_Category");
    Description = product_resp.get("Description");
    createMap = Map();
    createMap.put("Name",product_Name);
    createMap.put("Description",Description);
    createMap.put("Unit",Unit);
    createMap.put("Unit_Price",Unit_Price);
    createMap.put("Type",Type);
    resp = zoho.fsm.createRecord("Service_And_Parts",createMap);
    FSM_Product_id = resp.get("data").toMap().get("Service_And_Parts").toMap().get("id");
    }
    servive_line_resp = Map();
    servive_line_resp.put("Service",FSM_Product_id);
    servive_line_resp.put("Quantity",quantity);
    servive_line_resp.put("List_Price",unit_price);
    servive_line_resp.put("Sequence",i);
    servive_line_list.add(servive_line_resp);
    i = i + 1;
    }
    }

    terrResp = zoho.fsm.getRecords("Territories");
    terrId = terrResp.get("data").toMap().get("id");

    createMap = Map();
    createMap.put("Summary",Summary);
    createMap.put("Priority","Medium");
    createMap.put("Type","Service");
    createMap.put("Contact",FSM_Contact_id);
    createMap.put("Company",FSM_Account_id);
    createMap.put("Service_Address",ship_add_map);
    createMap.put("Billing_Address",bill_add_map);
    createMap.put("Service_Line_Items",servive_line_list);
    createMap.put("Territory",terrId);
    create_resp = zoho.fsm.createRecord("Work_Orders",createMap);
    info create_resp;

Testing the use case

Create a Sales Order.
Ensure the contact and account in the deal is already present in FSM. Use this custom function to push the contact and account from CRM to FSM.


 
A new Work Order will be created in FSM.