You're almost there! Please answer a few more questions for access to the Applications content. Complete registration
Interested in joining? Complete your registration by providing Areas of Interest here. Register

Removal of duplicate emails for a contact

Received Response
94
Views
1
Comments
edited Mar 30, 2020 10:26AM in Reporting & Analytics for B2C Service 1 comment

Summary

Script to remove emails with .00XX

Content

Hi All,

I've added the below script in one of my report to delete duplicate emails

 
Scenario: email_alt1 is valid and email_alt2 is duplicate and has .00
 
Contact ID primary_email email_alt1 email_alt2
10806305 7500211@uni.myc.edu.au smm@hotmail.com smm@hotmail.com.0001

I am unsure why works sometimes but not in this scenario. Can anyone please look at my code and advise if I have overlooked something?

Thank you,

Kate

Version

19A

Code Snippet

require_once(get_cfg_var('doc_root') . '/include/ConnectPHP/Connect_init.phph' );
initConnectAPI();
putenv('TEMPORARY_RNW_CONF_DIRECTORY');

use RightNow\Connect\v1_3 as RNCPHP;

$ar= RNCPHP\AnalyticsReport::fetch('#C1.DUP.EML.DEL.PREV'); 

$arr = $ar->run();

echo "Number of records to be processed is: ".$arr->count() . "<br>";
for ($i = $arr->count(); $i--;) {
	$note = null;
	$row = $arr->next();
	$email_alt2 = null;
	$primary_email = null;
	$email_alt1 = null;
	
	echo "Record count processing is: ".$i."<br>";
	
	
	//Alt1 email is valid
	 if((!preg_match('^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}.[\d]{4}$',$row['email_alt1']))){
	
		//Alt2 email is invalid
		if((preg_match('^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}.[\d]{4}$',$row['email_alt2']))){
		
			$subalt2=substr($row['email_alt2'],0,-5);
			
			if($row['email_alt1']==$subalt2){
			
				echo "<br>Alt1 and Alt2 emails have the same value, contact ID is: ".$row['Contact ID']."<br>";
				$contact = RNCPHP\Contact::first("ID = ".$row['Contact ID']);
				$note = "Alt1 and Alt2 have same Emails.\n";
				$note .= "Email Alt 1 is valid:  : ".$contact->Emails[1]->Address."\n";
				$note .= "Email Alt 2 address value before removing was:  : ".$contact->Emails[2]->Address."\n";
				$note_store = saveNote($contact,$note);

				//delete email alt2
				$contact->Emails->offsetUnset(2);
				$contact->save();
				RNCPHP\ConnectAPI::commit();
				
				}
		}
		
		
		//Primary email is

Howdy, Stranger!

Log In

To view full details, sign in.

Register

Don't have an account? Click here to get started!