Using Starfish Scripting Class Properties & Methods in C: Difference between revisions

From Starfish ETL
Jump to navigation Jump to search
No edit summary
No edit summary
Line 32: Line 32:
return quoteID;
return quoteID;
}
}
</source>
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.
I had to include some using statements and a function in my .NET Global section.
<source lang="csharp">
using System.Text;
using System.Security.Cryptography;
public static byte[] GetHash(string inputString)
{
    HashAlgorithm algorithm = SHA256.Create();
    return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
}
</source>
<source lang="csharp">
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();
}
   
}
</source>
<source lang="csharp">
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());
}
}
</source>
</source>

Revision as of 16:02, 9 December 2019

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.

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 = 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;
	}


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.

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));
}
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();
	}
    
}


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());
	}
}