Zoho FSM | Create Requests in Zoho FSM from Zoho CRM Deals

Create Requests in Zoho FSM from Zoho CRM Deals

Usecase: Using a custom button, push Deals in Zoho CRM as Requests to Zoho FSM.
 
Step 1: Create a connection for Zoho FSM in Zoho CRM
Step 2: Create a custom button
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 Default Services, choose 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 custom button

Create a custom button in the Deals module to push a CRM Deal as a Request in FSM.
 
To create a custom button:
  1. Navigate to Setup > Customization > Modules and Fields and click the Deals module.



  2. Select the Buttons tab and click Create New Button.



  3. In the Create Custom Button page, do the following and click Save:
    1. Button Name: CreateRequestInFSM
    2. Define action: Function
    3. Select Page: In Record
    4. Select Position: Details
    5. Select Layout(s): Standard
    6. Configured Function: Click Choose and create a custom function.


Step 3: Create a custom function 

  1. In the Configure Function popup, select Write your own.



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



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



  4. In the Edit Arguments popup, do the following:
    1. Enter the argument name as deal_id.
    2. For the argument value, type # and select Deals - Deal Id and click Done.



  5. Click Save.



  6. Add the script. Click Save, then click Assosciate.

    string button.CreateRequestInFSM(String deal_id)
    {
    dealResp = zoho.crm.getRecordById("Deals",deal_id);
    Tax_Id = "5391129000000473001";
    // replace the TAX ID from the Books
    Tax_name = "us";
    // replace the TAX Name from the Books
    tax_percentage = 23;
    // replace the TAX Percentage from the Books
    tax = {"Taxable":true,"Tax_Percentage":tax_percentage,"Tax_Name":Tax_name,"Tax_Id":Tax_Id};
    erroravailable = false;
    errormessage = "";
    //info dealResp;
    if(dealResp != null)
    {
    Summary = dealResp.get("Deal_Name");
    Account = dealResp.get("Account_Name");
    AccountId = "";
    //info Account;
    if(Account != null && Account != "" && Account.containKey("id"))
    {
    AccountId = Account.get("id");
    Company_name = Account.get("name");
    //info AccountId;
    Account_resp = zoho.crm.getRecordById("Accounts",AccountId).toMap();
    Account_map = Account_resp.toMap();
    //info Account_map;
    Contact_Email = Account_map.get("Email");
    bl_address = Account_map.get("Billing_Street");
    bl_city = Account_map.get("Billing_City");
    bl_country = Account_map.get("Billing_Country");
    bl_state = Account_map.get("Billing_State");
    bl_zip = Account_map.get("Billing_Code");
    sh_address = Account_map.get("Shipping_Street");
    sh_city = Account_map.get("Shipping_City");
    sh_country = Account_map.get("Shipping_Country");
    sh_state = Account_map.get("Shipping_State");
    sh_zip = Account_map.get("Shipping_Code");
    }
    if(Company_name.toString() != "" && Company_name != null)
    {
    //info response;
    response = zoho.fsm.searchRecordsByField("Companies","ZCRM_Id",AccountId,Map(),"fsmconnection");
    info response;
    if(response.isNull() || response.isEmpty())
    {
    response = zoho.fsm.searchRecordsByField("Companies","Company_Name",Company_name,Map(),"fsmconnection");
    }
    //info response;
    if(response.toString() != "" && response != null)
    {
    list_item = response.get("data").get(0);
    serviceAdId = "";
    billingAdId = "";
    //info list_item;
    fsmCust = list_item.get("Company_Name");
    if(fsmCust.equalsIgnoreCase(Company_name))
    {
    fsm_Company_id = list_item.get("id");
    //info fsm_customer_id;
    Billing_Address = list_item.get("Billing_Address");
    Service_Address = list_item.get("Service_Address");
    if(Service_Address != null)
    {
    serviceAdId = Service_Address.get("id");
    }
    if(Billing_Address != null)
    {
    billingAdId = Billing_Address.get("id");
    }
    if(serviceAdId == "" && billingAdId != "")
    {
    serviceAdId = billingAdId;
    }
    else if(billingAdId == "" && serviceAdId != "")
    {
    billingAdId = serviceAdId;
    }
    else if(serviceAdId == "" && billingAdId == "")
    {
    Service_Address_Map = Map();
    Service_Address_Map.put("Address_Name",Company_name + " Shipping Address");
    Service_Address_Map.put("Street_1",sh_address);
    Service_Address_Map.put("City",sh_city);
    Service_Address_Map.put("State",sh_state);
    Service_Address_Map.put("Zip_Code",sh_zip);
    Service_Address_Map.put("Country",sh_country);
    Billing_Address_Map = Map();
    Billing_Address_Map.put("Address_Name",Company_name + " Billing Address");
    Billing_Address_Map.put("Street_1",bl_address);
    Billing_Address_Map.put("City",bl_city);
    Billing_Address_Map.put("State",bl_state);
    Billing_Address_Map.put("Zip_Code",bl_zip);
    Billing_Address_Map.put("Country",bl_country);
    response = zoho.fsm.updateRecord("Companies",fsm_Company_id,{"Service_Address":Service_Address_Map,"Billing_Address":Billing_Address_Map});
    con_response = zoho.fsm.getRecordById("Companies",fsm_Company_id).toMap();
    sample = con_response.get("data").get(0);
    if(sample.get("Service_Address") != null)
    {
    serviceAdId = sample.get("Service_Address").get("id");
    }
    if(sample.get("Billing_Address") != null)
    {
    billingAdId = sample.get("Billing_Address").get("id");
    }
    }
    }
    }
    else
    {
    Phone = Account_resp.get("Phone");
    createMap = Map();
    createMap.put("Company_Name",Company_name);
    createMap.put("Phone",Phone);
    createMap.put("Tax",tax);
    Service_Address_Map = Map();
    Service_Address_Map.put("Address_Name",Company_name + " Shipping Address");
    Service_Address_Map.put("Street_1",sh_address);
    Service_Address_Map.put("City",sh_city);
    Service_Address_Map.put("State",sh_state);
    Service_Address_Map.put("Zip_Code",sh_zip);
    Service_Address_Map.put("Country",sh_country);
    Billing_Address_Map = Map();
    Billing_Address_Map.put("Address_Name",Company_name + " Billing Address");
    Billing_Address_Map.put("Street_1",bl_address);
    Billing_Address_Map.put("City",bl_city);
    Billing_Address_Map.put("State",bl_state);
    Billing_Address_Map.put("Zip_Code",bl_zip);
    Billing_Address_Map.put("Country",bl_country);
    createMap.put("Service_Address",Service_Address_Map);
    createMap.put("Billing_Address",Billing_Address_Map);
    cr_resp = zoho.fsm.createRecord("Companies",createMap);
    fsm_Company_id = cr_resp.get("data").toMap().get("Companies").toMap().get("id");
    response = zoho.fsm.getRecordById("Companies",fsm_Company_id);
    //info response;
    billingAdId = response.get("data").toMap().get("Billing_Address").toMap().get("id");
    serviceAdId = response.get("data").toMap().get("Service_Address").toMap().get("id");
    }
    }
    Contact = dealResp.get("Contact_Name");
    //info Contact;
    Contact_Email = "";
    if(Contact != null && Contact != "" && Contact.containKey("id"))
    {
    Contact_id = Contact.get("id");
    Contact_name = Contact.get("name");
    //info AccountId;
    customer_resp = zoho.crm.getRecordById("Contacts",Contact_id).toMap();
    customer_map = customer_resp.toMap();
    Contact_Email = customer_map.get("Email");
    bl_address = customer_map.get("Billing_Street");
    bl_city = customer_map.get("Billing_City");
    bl_country = customer_map.get("Billing_Country");
    bl_state = customer_map.get("Billing_State");
    bl_zip = customer_map.get("Billing_Code");
    sh_address = customer_map.get("Shipping_Street");
    sh_city = customer_map.get("Shipping_City");
    sh_country = customer_map.get("Shipping_Country");
    sh_state = customer_map.get("Shipping_State");
    sh_zip = customer_map.get("Shipping_Code");
    //info customer_map;
    if(!Contact_id.isNull() && !Contact_id.isEmpty())
    {
    serviceAdId = "";
    billingAdId = "";
    response = zoho.fsm.searchRecordsByField("Contacts","ZCRM_Id",Contact_id,Map(),"fsmconnection");
    info response;
    if(response.isNull() || response.isEmpty())
    {
    if(!Contact_Email.isNull() && !Contact_Email.isEmpty())
    {
    response = zoho.fsm.searchRecordsByField("Contacts","Email",Contact_Email,Map(),"fsmconnection");
    }
    }
    //info response;
    if(response.toString() != "" && response != null)
    {
    list_item = response.get("data").get(0);
    serviceAdId = "";
    billingAdId = "";
    //info list_item;
    fsm_customer_id = list_item.get("id");
    Billing_Address = list_item.get("Billing_Address");
    Service_Address = list_item.get("Service_Address");
    if(Service_Address != null)
    {
    serviceAdId = Service_Address.get("id");
    }
    if(Billing_Address != null)
    {
    billingAdId = Billing_Address.get("id");
    }
    if(serviceAdId == "" && billingAdId != "")
    {
    serviceAdId = billingAdId;
    }
    else if(billingAdId == "" && serviceAdId != "")
    {
    billingAdId = serviceAdId;
    }
    else if(serviceAdId == "" && billingAdId == "")
    {
    Service_Address_Map = Map();
    Service_Address_Map.put("Address_Name",Contact_name + " Shipping Address");
    Service_Address_Map.put("Street_1",sh_address);
    Service_Address_Map.put("City",sh_city);
    Service_Address_Map.put("State",sh_state);
    Service_Address_Map.put("Zip_Code",sh_zip);
    Service_Address_Map.put("Country",sh_country);
    Billing_Address_Map = Map();
    Billing_Address_Map.put("Address_Name",Contact_name + " Billing Address");
    Billing_Address_Map.put("Street_1",bl_address);
    Billing_Address_Map.put("City",bl_city);
    Billing_Address_Map.put("State",bl_state);
    Billing_Address_Map.put("Zip_Code",bl_zip);
    Billing_Address_Map.put("Country",bl_country);
    response = zoho.fsm.updateRecord("Contacts",fsm_customer_id,{"Service_Address":Service_Address_Map,"Billing_Address":Billing_Address_Map});
    //info response;
    con_response = zoho.fsm.getRecordById("Contacts",fsm_customer_id).toMap();
    sample = con_response.get("data").get(0);
    if(sample.get("Service_Address") != null)
    {
    serviceAdId = sample.get("Service_Address").get("id");
    }
    if(sample.get("Billing_Address") != null)
    {
    billingAdId = sample.get("Billing_Address").get("id");
    }
    }
    }
    else
    {
    Phone = customer_resp.get("Phone");
    createMap = Map();
    createMap.put("Last_Name",Contact_name);
    createMap.put("Email",Contact_Email);
    createMap.put("Phone",Phone);
    createMap.put("Tax",tax);
    Service_Address_Map = Map();
    Service_Address_Map.put("Address_Name",Contact_name + " Shipping Address");
    Service_Address_Map.put("Street_1",sh_address);
    Service_Address_Map.put("City",sh_city);
    Service_Address_Map.put("State",sh_state);
    Service_Address_Map.put("Zip_Code",sh_zip);
    Service_Address_Map.put("Country",sh_country);
    Billing_Address_Map = Map();
    Billing_Address_Map.put("Address_Name",Contact_name + " Billing Address");
    Billing_Address_Map.put("Street_1",bl_address);
    Billing_Address_Map.put("City",bl_city);
    Billing_Address_Map.put("State",bl_state);
    Billing_Address_Map.put("Zip_Code",bl_zip);
    Billing_Address_Map.put("Country",bl_country);
    createMap.put("Service_Address",Service_Address_Map);
    createMap.put("Billing_Address",Billing_Address_Map);
    cr_resp = zoho.fsm.createRecord("Contacts",createMap);
    //info cr_resp;
    fsm_customer_id = cr_resp.get("data").toMap().get("Contacts").toMap().get("id");
    response = zoho.fsm.getRecordById("Contacts",fsm_customer_id);
    //info response;
    billingAdId = response.get("data").toMap().get("Billing_Address").toMap().get("id");
    serviceAdId = response.get("data").toMap().get("Service_Address").toMap().get("id");
    }
    terrResp = zoho.fsm.getRecords("Territories");
    terrId = terrResp.get("data").toMap().get("id");
    createMap = Map();
    createMap.put("Summary",Summary);
    createMap.put("Company",fsm_Company_id);
    createMap.put("Contact",fsm_customer_id);
    createMap.put("Service_Address",{"id":serviceAdId});
    createMap.put("Billing_Address",{"id":billingAdId});
    createMap.put("Territory",terrId);
    info createMap;
    }
    }
    else
    {
    erroravailable = true;
    errormessage = "Contacts is Mandatory for workorder creation";
    }
    }
    if(erroravailable == false)
    {
    create_resp = zoho.fsm.createRecord("Requests",createMap);
    reqId = create_resp.get("data").toMap().get("Requests").toMap().get("id");
    api_url = "https://fsm.zoho.com/fsm/v1/Requests/" + reqId + "/actions/process_sync?sync_service=zohocrm";
    // Update the link to the based on the DC.
    // Define the body
    request_body = Map();
    data_list = List();
    data_item = Map();
    data_item.put("integ_id",deal_id);
    data_item.put("sync_process","link");
    data_item.put("integ_module","Deals");
    data_list.add(data_item);
    request_body.put("data",data_list);
    // Convert the body to JSON string
    request_body_json = request_body.toString();
    link_res = invokeurl
    [
    url :api_url
    type :POST
    parameters:request_body_json
    connection:"fsmconnection"
    ];
    }
    else
    {
    info errormessage;
    /* sendmail
        [
        from: zoho.adminuserid
        to: zoho.adminuserid
        subject: "Request is not created"
        message: errormessage
       
        ] */
    }
    return "Function executed successfully";
    }

Testing the use case

Click the CreateRequestInFSM button in a deals record.
Ensure the contact in the deal is already present in Zoho FSM and also that the has a service address and billing address. Use this custom function to push the contact from CRM to FSM.


 
A new Request will be created in Zoho FSM. The link to the deal will be available in the request.



The requests created for the deal will be listed under the Zoho FSM related list.