Loading
The online Trailblazer Community becomes read-only Oct 30, 2026Read More
Important Advisory: iOS Update Impacting CG Mobile ApplicationRead More
Extend Salesforce with Clicks, Not Code
Track Certain Data Privacy Preferences for Leads and Contacts Already in Salesforce

Track Certain Data Privacy Preferences for Leads and Contacts Already in Salesforce

Create data privacy records based on the Individual object for leads and contacts already in Salesforce using scripts.

Required Editions

Available in: both Salesforce Classic and Lightning Experience
Available in: all editions, including partner and customer community users.

These scripts create unique data privacy records for each lead and contact. Keep in mind, if you already have any data privacy records for your leads and contacts, this script creates duplicate records.

Create Data Privacy Records for Contacts

Create data privacy records and link them to contacts already in Salesforce when you run this script.

global class CreateIndividualFromContact implements Database.Batchable<sObject> {
    global Database.Querylocator start(Database.BatchableContext BC) {
       //Query to fetch contacts that don't have Individual created. You may modify the query to add custom fields  
       //Please add  "IsPersonAccount = false" condition to below query to exclude person accounts
       return Database.getQueryLocator('Select FirstName, LastName, Salutation from Contact where IndividualId = NULL');
    }
    
    global void execute(Database.BatchableContext BC, List<Contact> contactList) { 
        Map<Id, Individual> individualRecordsToCreate = new Map<Id, Individual>();
        for(Contact con : contactList) {
            individualRecordsToCreate.put(con.Id, new Individual(FirstName = con.FirstName, LastName = con.LastName, Salutation=con.Salutation));
        }
        insert individualRecordsToCreate.values();
        for(Contact con : contactList) {
            con.IndividualId = individualRecordsToCreate.get(con.Id).Id;
        }
        update contactList;
    }
    
    global void finish(Database.BatchableContext BC) {}
   
}

Create Data Privacy Records for Leads

Create data privacy records and link them to leads already in Salesforce when you run this script.

global class CreateIndividualFromLead implements Database.Batchable<sObject> {

    global Database.Querylocator start(Database.BatchableContext BC) {
       //Query to fetch non-converted Leads that don't have Individual created. You may modify the query to add custom fields  
       return Database.getQueryLocator('Select FirstName, LastName, Salutation from Lead where IsConverted = false and IndividualId = NULL');
    }
    
    global void execute(Database.BatchableContext BC, List<Lead> leadList) { 
        Map<Id, Individual> individualRecordsToCreate = new Map<Id, Individual>();
        for(Lead l : leadList) {
            individualRecordsToCreate.put(l.Id, new Individual(FirstName = l.FirstName, LastName = l.LastName, Salutation=l.Salutation));
        }
        insert individualRecordsToCreate.values();
        for(Lead l : leadList) {
            l.IndividualId = individualRecordsToCreate.get(l.Id).Id;
        }
        update leadList;
    }
    
    global void finish(Database.BatchableContext BC) {}   
}
 
Loading
Salesforce Help | Article