Last active
November 14, 2019 08:44
-
-
Save anil826/8ca4b7db45ac5c3642327c3711d3daf0 to your computer and use it in GitHub Desktop.
new
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
// for browsers := get local db data and write on a file as a backup and download that on the devise | |
var db = Lawnchair( { name:'QFlocalStorage', adapter: localStorage.getItem("f_db_adapter") }, function( store ) {}); | |
//Event listener | |
window.addEventListener('online', function(event){ | |
var draft_entry = []; | |
var collection_keys = []; | |
var updated_data = []; | |
//Get all the entries id from local db | |
db.keys(function(keys) { | |
//Collect ids | |
collection_keys = _.filter(keys, function(key) { | |
return (new RegExp('forms-')).test(key); | |
}); | |
//Get records from local db | |
db.get(collection_keys, function(records) { | |
//process data | |
send_entry_DFG(0, records); | |
}) | |
}); | |
// Send entry to DFG | |
function send_entry_DFG(counter, records){ | |
//Get the template name | |
var template_name = ( records[counter] && records[counter].value ) ? JSON.parse(records[counter].value).name : '' | |
//Loop for all entry | |
if ( counter < records.length ) { | |
//loop on draft only | |
if (records[counter] && records[counter].value && JSON.parse(records[counter].value).draft && template_name == "DFG Integration [Russia]" ) { | |
//Draft entry | |
var entry_key = records[counter].key; | |
var current_entry = JSON.parse(records[counter].value); | |
var page_components = current_entry.pages[0].components; | |
//Get first and last name | |
var first_name = _.where(page_components , {component_id: "ccee-a073-3d63"})[0].value | |
var last_name = _.where(page_components , {component_id: "2e65-9fb3-0133"})[0].value | |
//Get contact ID for update the record. | |
var contact_id = _.where(page_components , {component_id: "d255-2bb2-06a1"})[0].value | |
//Sample user data | |
var user_data = [ | |
{ | |
"typeId":"319013", // This is required fiedl in DFG Need to check maybe it will be a AccountID | |
"externalId": contact_id, // This one is option I guess we are able to make request without this one. | |
"data":[ | |
{ | |
"value": first_name, | |
"fieldId":"319014" | |
}, | |
{ | |
"value": last_name, | |
"fieldId":"319015" | |
} | |
] | |
} | |
] | |
//Make request on DFG server | |
$.ajax({ | |
url: 'https://test.dfg152.ru:8443/dfg-rest/entity/save', | |
cors: true, | |
headers: { | |
'x-auth-token': user_token, | |
'x-organization': 'Syngenta', | |
'content-type': 'application/json' | |
}, | |
data: JSON.stringify(user_data), | |
method:'POST', | |
success: function(response){ | |
//Checks for temp_id or DFG id. | |
var temp_id = response[0] && response[0].id ? response[0].id : null; | |
//Error message if record id not available. | |
if ( temp_id == null ) return alert('Error in saving data'); | |
//Set the temp id on form field. | |
current_entry.dirty = true; | |
current_entry.draft = false; | |
//Update the tempID | |
_.where(page_components , {component_id: "c127-209a-45c4"})[0].value = temp_id; | |
//Save the record on Salesforce | |
db.save({key: entry_key , value :JSON.stringify(current_entry)},function(result){ | |
console.log(result); | |
}) | |
// process next draft | |
send_entry_DFG(counter +1, records ); | |
} | |
}); | |
} else { | |
//Next data | |
send_entry_DFG(counter +1, records ); | |
} | |
} else { | |
//On done reload location. | |
window.location.reload(); | |
} | |
} | |
}); | |
//-------------------Login------------- | |
// Generic function to make an AJAX call | |
var DFGLogin = function(username , password) { | |
// Return the $.ajax promise | |
return $.ajax({ | |
url: 'https://test.dfg152.ru:8443/dfg-rest/authenticate', | |
cors: true, | |
headers: { | |
'X-Auth-Username': username, | |
'X-Auth-Password': password | |
}, | |
method:'POST', | |
success: function(data){ | |
//Store token | |
user_token = data.token; | |
} | |
}) | |
} | |
//Init token to null on first page load. | |
if (!user_token) var user_token = null; | |
//Get token from login API | |
if (user_token == null) DFGLogin('API.USER', 'qwer1234'); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment