public void CleanJobAppsByRecrID() { bool bContinueProcessing = true; string strLastRecrId = "0"; while( bContinueProcessing ) { List<Job_Application__c> objJobApps = objAPI.Query<Job_Application__c>( "select Id, Name, Contact__r.FirstName, Contact__r.LastName, Contact__r.RecruitingID__c, Contact__r.Degree__c, LastModifiedDate " + "from Job_Application__c " + "where Contact__r.RecruitingID__c != null and Contact__r.RecruitingID__c >= " + strLastRecrId + " order by Contact__r.RecruitingID__c, LastModifiedDate DESC, Name DESC LIMIT 2000 " ); // where Name like 'a0iE0%' if( !objAPI.ErrorMessage.Equals( "" ) ) { lblError.Text = objAPI.ErrorMessage; return; } if( objJobApps.Count == 0 ) break; List<string> strIds = new List<string>( 2000 ); List<Job_Application__c> objJAForUpdate = new List<Job_Application__c>( 2000 ); DateTime dtCutOff = Convert.ToDateTime( "9/27/2011" ); Job_Application__c objPrevious = null; foreach( Job_Application__c objJA in objJobApps ) { // if record is older than 9/27/11, check if it is a duplicate DateTime dtModified = (DateTime) objJA.LastModifiedDate; if( dtModified.CompareTo( dtCutOff ) < 0 ) { // if it is a duplicate, collect for removal if( objJA.Contact__r.RecruitingID__c.ToString().Equals( strLastRecrId ) ) { // add Job App to the list to delete strIds.Add( objJA.Id ); // correct the Provider Name & Degree on the previous record objPrevious.Provider_Name_Degree__c = string.Concat( objJA.Contact__r.LastName, ", ", objJA.Contact__r.FirstName , ", ", objJA.Contact__r.Degree__c ); // make a copy of previous record for the update // because SalesForce doesn't accept more than one reference in Contact__r f/update Job_Application__c objJACopy = new Job_Application__c(); objJACopy.Id = objPrevious.Id; objJACopy.Provider_Name_Degree__c = objPrevious.Provider_Name_Degree__c; objJAForUpdate.Add( objJACopy ); continue; } } #region Previous Clean up //// previous clean up //// if it is a duplicate that starts with a0iE0, collect for removal //if( !objJA.Name.StartsWith( "Job App" ) ) //{ // if( objJA.Contact__r.RecruitingID__c.ToString().Equals( strLastRecrId ) ) // { // // add Job App to the list to delete // strIds.Add( objJA.Id ); // continue; // } // // it is not a duplicate, so we correct the name // objJA.Name = string.Concat( "Job Application for ", objJA.Contact__r.FirstName, " ", objJA.Contact__r.LastName ); // // make a copy of object because SalesForce doesn't accept more than one reference in Contact__r f/update // Job_Application__c objJACopy = new Job_Application__c(); // objJACopy.Id = objJA.Id; // objJACopy.Name = objJA.Name; // objJAForUpdate.Add( objJACopy ); //} #endregion strLastRecrId = objJA.Contact__r.RecruitingID__c.ToString(); objPrevious = objJA; } DeleteResult[] objDelResult = objAPI.Delete( strIds.ToArray() ); SaveResult[] objUpdResult = objAPI.Update( objJAForUpdate.ToArray() ); // after the last one was processed, stop if( objJobApps.Count == 1 ) bContinueProcessing = false; } return; }
public void CleanJobAppsByMENbr() { ReportStatus( "Starting Job App clean up." ); bool bContinueProcessing = true; int iCount = 0; string strLastMENumber = ""; while( bContinueProcessing ) { List<Job_Application__c> objJobApps = objAPI.Query<Job_Application__c>( "select Id, Name, Contact__r.FirstName, Contact__r.LastName, Contact__r.MeNumber__c " + "from Job_Application__c " + "where Contact__r.AMAOnly__c = '1' and Contact__r.MeNumber__c >= '" + strLastMENumber + "' order by Contact__r.MeNumber__c, Name DESC LIMIT 2000 " ); // and ( NOT Name LIKE 'Job App%' ) if( !objAPI.ErrorMessage.Equals( "" ) ) { lblError.Text = objAPI.ErrorMessage; return; } iCount++; ReportStatus( "Retrieved Job Apps with ME Number >= ", strLastMENumber, " - # ", iCount.ToString() ); if( objJobApps.Count == 0 ) break; List<string> strIds = new List<string>( 2000 ); List<Job_Application__c> objJAForUpdate = new List<Job_Application__c>( 2000 ); foreach( Job_Application__c objJA in objJobApps ) { // if it is a duplicate that starts with a0iE0, collect for removal if( !objJA.Name.StartsWith( "Job App" ) ) { if( objJA.Contact__r.MeNumber__c.ToString().Equals( strLastMENumber ) ) { ReportStatus( "Preparing to remove Job App ", objJA.Name, " with ME# ", strLastMENumber ); // add Job App to the list to delete strIds.Add( objJA.Id ); continue; } // it is not a duplicate, so we correct the name objJA.Name = string.Concat( "Job Application for ", objJA.Contact__r.FirstName, " ", objJA.Contact__r.LastName ); ReportStatus( "Preparing to rename Job App with ME# ", strLastMENumber, " to ", objJA.Name ); // make a copy of object because SalesForce doesn't accept more than one reference in Contact__r f/update Job_Application__c objJACopy = new Job_Application__c(); objJACopy.Id = objJA.Id; objJACopy.Name = objJA.Name; objJAForUpdate.Add( objJACopy ); } strLastMENumber = objJA.Contact__r.MeNumber__c.ToString(); } ReportStatus( "Removing ", strIds.Count.ToString(), " duplicate Job Apps." ); DeleteResult[] objDelResult = objAPI.Delete( strIds.ToArray() ); ReportStatus( "Renaming ", objJAForUpdate.Count.ToString(), " Job Apps." ); SaveResult[] objUpdResult = objAPI.Update( objJAForUpdate.ToArray() ); // after the last one was processed, stop if( objJobApps.Count == 1 ) bContinueProcessing = false; } ReportStatus( "Finished cleaning up Job Apps." ); return; }