Blog

Main » Salesforce

Problem: a random user should be selected from a queue to assign a lead. The number of users in the queue is unknown and calculated on the go (variable UserCount).

Implications: multiple records may enter the flow at the same time, so the random number should be generated for those (and the timestamp can't be used to generate the random number).

Solution: Use date with the combination with data that came with a record to generate a random number. The seed (the starting number to generate a random(~er) number) is a concatenation of date and length of the Lead Name (variable getLead.Name)

Implication: we don't know the span of a random number (max and min), so, we can't normalize the output.
Solution: use last digit of the random number -> this normalizes the span [0-9]

Final solution:


ROUND(value(Right(text(ROUND(sqrt(
(value(
 (left(right(text(now()),6),2)
		
		... 
		
			Read more »
		


Validation

Contacts

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" >
  <entity name="contact" >

    <attribute name="contactid" />
    <attribute name="hsl_forecordid" />
    <attribute name="fullname" />
    <attribute name="firstname" />
    <attribute name="lastname" />
    <attribute name="emailaddress1" />
    <attribute name="hsl_legacyid" />
    <attribute name="hsl_id18salesforcelegacy" />
  </entity>
</fetch>

Brand Preferences

 

Addresses

<fetch version="1.0" output-format="xml-platform" mapping="logical ... Read more »



list fixCity = new list([Select Id, City__c from Contact where City__c like '%,%' LIMIT 2);
//system.debug(tweContact.size());

for (Contact c: fixCity ) {
    Set<String> city = new Set<String>();
     c.addAll(city.City__c.split(','));
    system.debug(city);
}



split a single account into individual contacts

list tweContact = new list([Select Id, FirstName, LastName, AccountID from Contact where AccountID = '001E000001cxEiGIAU' LIMIT 10]);
system.debug(tweContact.size());
list newAccounts = new list();
for (Contact c: tweContact ) {
 Account a = new Account(Name=c.FirstName+ ' '+c.LastName);
 insert a;
 c.AccountID = :a.Id;
 update c;
}


list userlist = [SELECT ID, Name FROM User WHERE et4ae5__ExactTargetForAppExchangeUser__c = TRUE];
 for (User i : userlist){
 try {
 string toDelete = i.id; 
 et4ae5.supportutilities.deleteusertokens(toDelete, TRUE);
 }
 catch (Exception e) {
 system.debug('Current user ='+i.Name+' Parent Record.');
 }
 try {
 et4ae5.supportutilities.deleteusertokens('APIUSER');
 }
 Catch (Exception e) {
 system.debug('API Error');
 }
 }


Set past date:

Date closeDate = Date.today().addDays(-7);

Quickest way to get a list of IDs from a list of sObjects:

List<opportunity> opptys = new List <opportunity>();
Map <id, opportunity> opptyMap = new Map <id, opportunity>(opptys);
List <id> opptyIds = new List <id>(opptyMap.keySet());

Get the most recent record using SOQL

[SELECT Id FROM sObject ORDER BY CreatedDate DESC LIMIT 1];


global class UpdateContactAddresses implements 
 Database.Batchable<sobject>, Database.Stateful {
 
 // instance member to retain state across transactions
 global Integer recordsProcessed = 0;

 global Database.QueryLocator start(Database.BatchableContext bc) {
 return Database.getQueryLocator(
 'SELECT ID, BillingStreet, BillingCity, BillingState, ' +
 'BillingPostalCode, (SELECT ID, MailingStreet, MailingCity, ' +
 'MailingState, MailingPostalCode FROM Contacts) FROM Account ' + 
 'Where BillingCountry = \'USA\''
 );
 }

 global void execute(Database.BatchableContext bc, List<account> scope){
 // process each batch of records
 List<contact> contacts = new List<contact>();
 for (Account account : scope) {
 for (Contact contact : account.contacts) {
 contact.MailingStreet = account.BillingStreet;
 contact.MailingCity = account.BillingCity;
 contact.MailingState = account.BillingState;
 contact.Mailing
		
		... 
		
			Read more »
		


Trigger

Trigger MileageTrigger on Mileage__c (before insert, before update) {
 Set<ID> ids = Trigger.newMap.keySet();
 List<User> c = [SELECT Id FROM user WHERE mileageid__c in :ids];
}
Apex

for ( List<contact> contacts : [SELECT Id FROM Contact] {
 for (Contact aContact : contacts) {
 //modify aContact
 }Database.update(contacts);
}


1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required 
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the  percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .

  • Single Action -To verify that the the single record produces the correct an expected result .
  • ... Read more »


Select Product__r.Id, Product__r.Name, Product__r.Category__c 
FROM Order_Lines__c 
WHERE Order__r.eWinery_Date_Completed__c = LAST_N_DAYS:365 
AND Order__r.Status = 'Completed' 
AND Product__r.Category__c = ''