Field Validation Function - How can I run on a Deal CREATE as well as an UPDATE?
greetings.. I am running some address validation logic using an onChange() validation on the field.
When it runs during an edit/update Zoho CRM sends the Deal ID as part of the payload, so everything works.
When it runs during a new/create operation, there is not yet a record created.. so I am unable to parse the data field and populate the fields at this time.
How can I best accomplish this?
Code provided below for your reference:
- //
- crm_record = crmAPIRequest.toMap().get("record");
- if(crm_record.containKey("id"))
- {
- existing_record = true;
- job_id = crm_record.get("id").toLong();
- }
- else
- {
- existing_record = false;
- }
- //
- url_encoded_address = zoho.encryption.urlEncode(crm_record.get("full_home_address"));
- //
- // Retrieve the API key for LocationIQ out of the "CRM Variables"
- api_key = zoho.crm.getOrgVariable("locationiq_api_token");
- //
- // Run the validation using LocationIQ
- location_response = invokeurl
- [
- url :"https://us1.locationiq.com/v1/search?key=" + api_key + "&q=" + url_encoded_address + "&format=json&addressdetails=1&limit=1&countrycodes=us&normalizeaddress=1"
- type :GET
- detailed:true
- ];
- //
- class = location_response.get("responseText").get(0).get("class");
- place = location_response.get("responseText").get(0).get("place");
- //
- return_value = Map();
- //
- if(isNull(class))
- {
- return_value.put('status','error');
- return_value.put('message','Provided address does not validate');
- }
- else
- {
- if(existing_record)
- {
- address_components = location_response.get("responseText").get(0).get("address");
- //
- new_full_address = address_components.get("house_number") + " " + address_components.get("road") + ", " + address_components.get("city") + ", " + address_components.get("state") + " " + address_components.get("postcode");
- //
- updated_address_fields = Map();
- updated_address_fields.put("full_home_address",new_full_address);
- updated_address_fields.put("Description",new_full_address);
- updated_address_fields.put("address_house_number",address_components.get("house_number"));
- updated_address_fields.put("address_street",address_components.get("road"));
- updated_address_fields.put("address_city",address_components.get("city"));
- updated_address_fields.put("address_state",address_components.get("state"));
- updated_address_fields.put("address_zip",address_components.get("postcode"));
- updated_address_fields.put("Address_Validated",1);
- //
- update = zoho.crm.updateRecord("Deals",job_id,updated_address_fields);
- //
- }
- return_value.put('status','success');
- }
- //
- return return_value;
Thank you for any assistance you can provide.
- Joel