Using Starfish Scripting Class Properties & Methods in C

From Starfish ETL
Revision as of 07:52, 20 June 2020 by Jkuehlthau (talk | contribs) (Undo revision 5766 by Jkuehlthau (talk))
Jump to navigation Jump to search

See available Starfish Scripting Class Variables, Properties and Methods. To use these variables, properties and methods, you must append "Starfish." to the beginning of variable, property or method AND you must use the exact capitalization as found in this wiki.

Generic Example

Example using the PreviewMode Property and GetSetting and SaveSetting Methods:

if (!Starfish.PreviewMode)
	{
		//Set the last run datetime for this user.
		Starfish.SaveSetting("Meetings-LRD-"+gUserID, DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss+00:00"));
		
		//Check to see if there are more users to process and if so, restart job.
		string uIDs = Starfish.GetSetting("UserIDs");
		//This Setting is set in the first Job and will restrict runs to only the Admin user.
		bool b = Starfish.GetSetting("AdminUserOnlyTesting_True_OR_False") == "True";
		if (b) {
			uIDs = "ADMIN";
		}
		if (runCnt < uIDs.Split(';').Length-1)
		{
			runCnt++;
			Starfish.GotoJob("e8566f56-69cd-4d65-8835-351de24795e3");
			Starfish.LogMessage("Count: "+ runCnt.ToString());
		} else {
			runCnt = 0;
			Starfish.GotoNextRow();
		}

		string quoteID = "";
		//Sometimes a Starfish Origin Connector will return NULL for an field's Object.  If you try to run .ToString() on the NULL object, it will error.  If that happens, you'll have to confirm the object exists before attempting to convert it to a string.
		if (Starfish.OriginData["OPPORTUNITYID"] != null)
		{
			quoteID = Starfish.XRefRead("SLXQuoteIDs",Starfish.OriginData["OPPORTUNITYID"].ToString());
			if (String.IsNullOrEmpty(quoteID))
			{
				quoteID = Starfish.SmartLookup("Quotes","id","[{\"foreignid_c\":\""+Starfish.OriginData["OPPORTUNITYID"].ToString()+"\"}]").ToString();
			}
		}
		return quoteID;
	}

Hashing Example

Here are a couple of scripts where I used C# Hashing code to save the hash of a records data so I could skip over the record later if the data was the same. I could not use only the date_modified field because I was reading and writing the same record.

Global Code

I had to include some using statements and a function in my .NET Global section:

using System.Text;
using System.Security.Cryptography;

public static byte[] GetHash(string inputString)
{
    HashAlgorithm algorithm = SHA256.Create();
    return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
}

Repeat Each Row Before Operation

void CSharpProcedure()
{
	string source = Starfish.OriginData["CONTACTID"].ToString()+Starfish.OriginData["ACCOUNTID"].ToString()+Starfish.OriginData["CONTACT_TEAM_SET"].ToString()+Starfish.OriginData["ACCOUNT_TEAM_SET"].ToString()+Starfish.OriginData["ACCOUNT_TEAM_ID"];
    StringBuilder sb = new StringBuilder();
    foreach (byte b in GetHash(source))
	{
        sb.Append(b.ToString("X2"));
	}
	Starfish.LogMessage("newval: "+sb.ToString());
	
	string oldVal = "";
	oldVal = Starfish.XRefRead("hashTeamsToContacts",Starfish.OriginData["CONTACTID"].ToString());
	Starfish.LogMessage("oldval: "+oldVal);
	if(sb.ToString() == oldVal){
		if (!Starfish.PreviewMode)
		{
			Starfish.SaveSetting("AccountTeamsToContactsLDM",Starfish.OriginData["CONTACT_DATE_MODIFIED"].ToString());
		}
		Starfish.GotoNextRow();
	}
    
}


Repeat Each Row After Operation

void CSharpProcedure()
{
	if (!Starfish.PreviewMode)
	{
		Starfish.SaveSetting("AccountTeamsToContactsLDM",Starfish.OriginData["CONTACT_DATE_MODIFIED"].ToString());
		
		//Save the hash of the origin so we can not run the record again if it does not need to be run.
		string source = "";
		source = Starfish.OriginData["CONTACTID"].ToString()+Starfish.OriginData["ACCOUNTID"].ToString()+Starfish.OriginData["CONTACT_TEAM_SET"].ToString()+Starfish.OriginData["ACCOUNT_TEAM_SET"].ToString()+Starfish.OriginData["ACCOUNT_TEAM_ID"].ToString();
		StringBuilder sb = new StringBuilder();
		foreach (byte b in GetHash(source))
		{
			sb.Append(b.ToString("X2"));
		}
		Starfish.LogMessage("newval: "+sb.ToString());
		Starfish.XRefWrite("hashTeamsToContacts",Starfish.OriginData["CONTACTID"].ToString(),sb.ToString());
	}
}

Processing only records modified after the last record processed based on date_modified

Origin Filter

Use a variable like this in your origin query: @@VAR:LastModifiedDateTime@@
Example:

[{"$and":[{"modified_user_id":{"$not_equals":"53c55c2f-6304-496c-8512-1dfea42fbd87"}},{"send_to_azure_c":"1"},{"date_modified":{"$gte":"@@VAR:LastModifiedDateTime@@"}}]}]


Before Operation to retrieve last date modified or supply one if none found

object ScriptedVariable()
{
    string strDte = "";
    //Retrieve the stored datetime value.
	strDte = Starfish.GetSetting("SugarBugsToDevOps").ToString();
	//If no datetime value, create a value equal to one month ago.  This way we don't end up erroring if a value is required and we don't end up processing all records in a table, if that is not desired.
	if (strDte == "")
	{
	    strDte = DateTime.Today.AddMonths(-1).ToString();
	}
	//Convert the datetime format of the string to a format that is expected.
	DateTime dte = Convert.ToDateTime(strDte);
	strDte = dte.ToString("yyyy-MM-ddTHH:mm:ss");
	return strDte;
}

After Operation to save the date modified of the last processed record

void CSharpProcedure()
{
    if (!Starfish.PreviewMode)
	{
		//Set the last run datetime for this user.
		Starfish.SaveSetting("SugarBugsToDevOps", Starfish.OriginData["DATE_MODIFIED"].ToString());
	}
}