Salesforce Developer Saved Code

salesforce-developer

Code to create 1 account:

Account newAccount = new Account (
    name = 'KhaiTest2',
    BillingCity ='TestCity',
    BillingCountry ='TestCountry',
    BillingStreet ='TestStreet',
    BillingPostalCode ='t3stcd3'
);

insert newAccount;
System.debug('New Account ID:' + newAccount.Id);

Code to create 10,000 contacts under a given account:

// If we try to create more than 1000 contacts at one time, we may run into
// CPU usage limit.  If we need to create more users, we will have to run this 
// code multiple times using the Developer Console.  In the standard developer 
// org, we can only create about 3000 contacts.  If we try to create more 
// contacts, we may run into the storage limit.  We can purchase more storage, 
// but I am investigating if we can use Dropbox with Salesforce somehow.

List<Contact> contacts = new List<Contact>();
for (integer i = 0; i < 1000; i++) {
    Contact NewContact = new Contact (
        FirstName = 'Khai',
        LastName = 'Test',
        AccountId = '0014100000ILN80AAH',
        Email = 'xyzmail@example.com'
    );
    contacts.add(NewContact);
}

insert contacts;

Code to get a list of column names from an object:

Schema.DescribeSObjectResult a_desc = Account.sObjectType.getDescribe(); 
Map<String, Schema.SObjectField> a_fields = a_desc.fields.getMap();

for(Schema.sObjectField fld:a_fields.values()){ 
               system.debug(fld);
}

Code to create community user from contact:

@isTest
private class KhaiQuickTest {
    static String createAlias(String firstName, String lastName) {
        String result = firstName + lastName;
        result = result.replaceAll(' ','').toLowerCase();
        return result.substring(0, 7);
    }
    @isTest static void quickTest() {
        Account acct = new Account(
            Name = 'KhaiTest'
        );
        insert acct;

        // Assume that one contact already exist as a user
        Contact c = new Contact(
            Lastname = 'Test',
            Firstname = 'KhaiTest',
            accountId = acct.Id,
            email = 'test03142017_1@example.com'
        );
        insert c;
        String nick = c.email!=null ? c.email.substring(0, c.email.indexOf('@')):'';
        nick += Datetime.now().getTime();

        Profile p = [SELECT Id FROM Profile WHERE NAME='Customer Community Login User - Custom'];
        String profileId = p.Id;

        User u = new User(
            Lastname = c.Lastname,
            Firstname = c.Firstname,
            email = c.email,
            alias = createAlias(c.firstName, c.lastName),

            emailencodingkey = 'UTF-8', 
            languagelocalekey = 'en_US', 
            localesidkey = 'en_US', 
            contactId = c.Id,
            timezonesidkey = 'America/Los_Angeles', 
            username = c.email,
            IsActive = true,
            CommunityNickname = nick,
            ProfileId = profileId

        );
        insert u;

        User u2 = [SELECT Id,accountId FROM User WHERE contactId= :c.Id];
        System.debug('Comparing ' + acct.Id + ' with ' + u2.accountId);
        if (acct.Id == u2.accountId) {
            System.debug('The two are equal.');
        } else {
            System.debug('The two are not equal.');
        }
        System.assertEquals(acct.Id + '',u2.accountId + '');
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License