Examples of using Starfish Class Functions In Javascript

From Starfish ETL
Jump to navigation Jump to search

This code was used to lookup a value inside of SugarCRM using the Sugar REST connector.

function jsProcedure() {
	//SugarID_to_Email_Address
	//var obj = JSON.parse('{"email_address": "justin.kuehlthau@starfish.com", "primary_address": true, "reply_to_address": false, "invalid_email": false, "opt_out": false, "email_address_id": "a876b74c-a9a7-11e9-9d45-525400ec9068"}');
	var json = OriginData("EMAIL").toString();
	json = json.substring(1);
	json = json.substring(0, json.length - 1);
	//Starfish.LogMessage(json);
	var obj = JSON.parse(json);
	//Starfish.LogMessage(obj.email_address.toString());
	Starfish.XRefWrite("SugarID_to_Email_Address",OriginData("ID").toString(),obj.email_address.toString());

	attachmentURLs = [];
	ui = 0;
	var re = /(https:\/\/basyspro.com\/index.php\?gf-download=[^"<> \n]+)/g;
	var body = Origin("UniqueBody");
	attachmentURLs = body.match(re);
	if (attachmentURLs) {
		Starfish.LogMessage(attachmentURLs.toString());
	}

    var res = OriginData("EMAILADDRESS").toString();
    if(res)
    {
        if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(res))
        {
            var badEmails = Starfish.GetSetting("InvalidEmailAddresses");
            //Starfish.LogMessage("BadEmails: "+badEmails);
            if (badEmails) {
                badEmails = badEmails + "|";
            }
            //Starfish.LogMessage("Email Address: "+OriginData("EMAILADDRESS").toString());
            //Starfish.LogMessage("BadEmails: "+badEmails);
            //Starfish.LogMessage("BadEmails+New: "+badEmails+OriginData("EMAILADDRESS").toString()+","+OriginData("FIRSTNAME").toString()+","+OriginData("LASTNAME").toString()+","+OriginData("PERCONID").toString());
            Starfish.SaveSetting("InvalidEmailAddresses",badEmails+OriginData("EMAILADDRESS").toString()+","+OriginData("FIRSTNAME").toString()+","+OriginData("LASTNAME").toString()+","+OriginData("PERCONID").toString());
            //Starfish.SaveSetting("InvalidEmailAddresses",OriginData("EMAILADDRESS").toString());
            Starfish.GotoNextRow();
        }
    }

    var str = Starfish.GetSetting("InvalidEmailAddresses");
    if (str)
    {
        var subject = "Bad Email Addresses Found In Epicor Data";
        var body = "<p>Hello,</p><p>The following email addresses were found in Epicor and are invalid.</p><p>EmailAddress,FirstName,LastName,PerConID<br />";
        var arr = str.split("|");
        for (i=0; i<arr.length; i++)
        {
            
            body = body+arr[i]+"<br />";
        }
        body = body+"</p><p>Thank you,<br />Starfish</p>";
        //Starfish.LogMessage(body);
        Starfish.SendEmail("justin.kuehlthau@techadv.com",subject,body,"support@techadv.com","support@techadv.com","","",true);
        Starfish.SaveSetting("InvalidEmailAddresses","");
    }
}