Solution: Deluge script to determine the default Layout for a given User to be used when creating a new record

Solution: Deluge script to determine the default Layout for a given User to be used when creating a new record

My use case:  Create a new Invoice, using fields in the Potentials module in Zoho CRM - I want the layout to be set to the default layout of the user who is creating the invoice.

Problem - when creating a new record using a Deluge function, the layout for the new record does not default to the given user's default layout.  If you are using different layouts for different users/ profiles, then this can result in issues with users ending up seeing the record they have created in the incorrect layout.

Process:
  1. Get user and their profile
  2. Get layouts and determine the default layout for the given profile
Sample solution code (in this case, using to create an Invoice in Zoho CRM):
//  ------------ Get loginuser name and id and then get the default Layout for that user's Profile ------------
//  (user creating invoice will be the owner of the invoice)
u_resp = zoho.crm.invokeConnector("crm.getusers",{"type":"ActiveUsers"});
u_respMap = u_resp.get("response").toMap();
u_respList = u_respMap.get("users").toJSONList();
//info respList;
for each  u_respVar in u_respList
{
email = u_respVar.get("email");
if(zoho.loginuserid == email)
{
user_id = u_respVar.get("id");
user_profile_name = u_respVar.get("profile").getJSON("name");
info "user_profile_name :  " + user_profile_name;
}
}
user_name = zoho.loginuser;
layouts_resp = invokeurl
[
type :GET
connection:"InsertYourZohoConnectionHere"
];
layoutslist = layouts_resp.get("layouts").toJSONList();
for each  layout_item in layoutslist
{
profileslist = layout_item.get("profiles").toJSONList();
for each  profile_item in profileslist
{
if(user_profile_name == profile_item.get("name") && profile_item.get("default") == true)
{
layout_id = layout_item.get("id");
layout_name = layout_item.get("name");
info profile_item.get("name") + ", default = " + profile_item.get("default");
info "Using Layout : " + layout_name + ", ID : " + layout_id;
}
}
}
//  ------------------------------------------------------------------

Notes:
  1. Your Zoho CRM connection will need to include Zoho.CRM.Settings.Layouts
  2. You can now use the layout name and id in the data map used to create the record, eg
paramap = Map();

map_layout = Map();
map_layout.put("name",layout_name);
map_layout.put("id",layout_id);
paramap.put("Layout",map_layout);