Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

Javascript event flow issue

$
0
0

Hi

Can anyone help with the following JavaScript?  This script populates a table like report in a text file.  The report must get account data as well as all the contacts data.

The part for the Account data works fine, however the part to get all Contacts details have some issues.  It seems like the JavaScript code continues to run even though the 'RetreiveMultipleRecords' is not finished.

/**
*Account to Prokon Grid
*=======================
*Date:18/06/18
*Desc:
*Script to convert Account details with related contact details to a Prokon Grid.
*The Prokon Grid will be synced with ProBase
*/

var strProkonGrid;  //Var to hold the string data for the prokon grid
var grid_data = new Array();   // Data array
var grid_header = new Array(); // Column header array
var i;
/**
*retreive account information and populate it to strProkonGrid
*/
function retrieveAndPopulateAccount(AccountId) 
{	
	SDK.JQuery.retrieveRecord(
     AccountId,
     "Account",
     null, null,
     function (account) {
		/**
		*add table for basic account info
		*Client No, Company, Branch, Country
		*/
		i = 0;
		grid_data[i] = new Array( 'Client No', 
								account.AccountNumber
								);
		i++;
		grid_data[i] = new Array('Company',
								account.Name
								);
		i++;
		grid_data[i] = new Array('Branch',
								account.new_branch
								);
		i++;
		grid_data[i] = new Array('Country',
								account.new_CompanyCountry
								);
		strProkonGrid = array_to_grid(null, grid_data);
		/**
		*add table for agreement terms
		*/
		i = 0;
		grid_data = []; 
		grid_data[i] = new Array( 'Agreement', 
								account.newagreementterms
								);
		i++
		grid_data[i] = new Array( 'Terms',
									' ' //Fill blank
								);
		strProkonGrid += array_to_grid(null, grid_data);

		/**
		*add table for address
		*/
		i = 0;
		grid_data = []; 
		grid_header = Array('Postal Address','Physical Address','Accounts Address');
		
		grid_data[i] = new Array(account.Address2_Line1,
								account.Address1_Line1,
								account.new_Accounts1
								);
		i++;
		grid_data[i] = new Array(account.Address2_Line2,
								account.Address1_Line2,
								account.new_Accounts2
								);
		i++;
		grid_data[i] = new Array(account.Address2_Line3,
								account.Address1_Line3,
								account.new_Accounts3
								);
		i++;
		grid_data[i] = new Array(account.Address2_City,
								account.Address1_City,
								account.new_Accounts4
								);
		i++;
		grid_data[i] = new Array(account.Address2_StateOrProvince,
								account.Address1_StateOrProvince,
								account.new_Accounts5
								);
		i++;
		grid_data[i] = new Array(account.Address2_PostalCode,
								account.Address1_PostalCode,
								''
								);
		i++;
		grid_data[i] = new Array(account.Address2_Country,
								account.Address1_Country,
								''
								);
		strProkonGrid += array_to_grid(grid_header, grid_data);
		/**
		*add table for all contacts of the account
		*/
		i = 0;
		grid_data = []; 
		grid_header = Array('Contact / Vat no (1st line)',
							'Telephone',
							'Fax',
							'Cell',
							'E-mail',
							'Code');
		/**
		*First line is account data
		*/
		grid_data[i] = new Array(account.prkn_VATnumber,
								account.Telephone1,
								account.Fax,
								'',
								account.EMailAddress1,
								''
								);
		 i++;
		/**
		*query all contact that belongs to the account
		*/	
		SDK.JQuery.retrieveMultipleRecords("Contact","$select=*&$filter=ParentCustomerId/Id eq guid'" + AccountId +"'",
		function (results) {
		var firstResult = results[0];
		if (firstResult != null) 
		{
		for (var k = 0; k < results.length; k++) 
		{
			var ContactDetails = results[k];			
			grid_data[i] = new Array(ContactDetails.FullName,
									ContactDetails.Telephone1, 
									ContactDetails.Fax,
									ContactDetails.Telephone3,
									ContactDetails.EMailaddress1,
									ContactDetails.new_naraccounts
									);
			i++;
		}
	  }
	 },
	 errorHandler,
	 function () {
		//on complete handler	
		 }
	   );
	strProkonGrid += array_to_grid(grid_header, grid_data);
	WriteToFile();	
     },
     errorHandler
   );
}

function WriteToFile()
{
	var fso = new ActiveXObject("Scripting.FileSystemObject");
	var a = fso.CreateTextFile("R:\\Christo\\testfile.txt", true);
	a.WriteLine(strProkonGrid);
	a.Close();
}



function errorHandler(error) {
 alert(error.message);
}


Please vote if you find my post useful. Add me on Skype @ christo.skype1


Viewing all articles
Browse latest Browse all 8223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>