Search This Blog

Sunday, February 18, 2018

How to pass trigger.new or trigger.newMap or trigger.oldMap to future method from trigger in salesforce.

In Salesforce we can use serialize and deserialize methods to pass required data from trigger to future method.

Simple Example below.
Apex Trigger Code:
trigger updatePhone on Account (after update) {
    String triggerNewList = JSON.serialize(Trigger.new);
    String oldMap = JSON.serialize(Trigger.oldMap);
    UpdatePhoneOnContact.updatePhone(triggerNewList,oldMap);
}

ApexClass Code:
public class UpdatePhoneOnContact {
    public static void updatePhone(String triggerNewList,String oldMap) {
        Set <Id> accountIdSet = new Set <Id>();
        Map <Id,String> accountMap = new Map <Id,string> ();
         List<Account> lstAccount = (List<Account>) JSON.deserialize(triggerNewList,List<Account>.class);
         Map<id,Account> accountOldMap = (Map<id,Account>) JSON.deserialize(oldMap,Map<id,Account>.class);
        for(Account acc:lstAccount) {
            if(accountOldMap.get(acc.Id).phone != acc.phone) {
                accountIdSet.add(acc.id);
            }
            if(acc.phone != null) {
                accountMap.put(acc.id,acc.phone);
            }
        }
        List <contact> contactList = new List <Contact>();
        for(Contact con:[select id,accountId,Phone from contact where accountId in:accountIdSet]) {
            if(accountMap != null && accountMap.containsKey(con.accountId)) {
                con.phone = accountMap.get(con.accountId);
                contactList.add(con);
            }
        }
        if(contactList.size() >0) {
            update contactList;
        }
    }
}

Enjoy Coding...

Simple trigger to update phone number in related contacts when phone number changed in its parent account

Apex Trigger Code:
trigger updatePhone on Account (after update) {
    Set <Id> accountIdSet = new Set <Id>();
    Map <Id,String> accountMap = new Map <Id,string> ();
    for(Account acc:trigger.new) {
        if(Trigger.oldMap.get(acc.Id).phone != acc.phone) {
            accountIdSet.add(acc.id);
        }
        if(acc.phone != null) {
            accountMap.put(acc.id,acc.phone);
        }
    }
    List <contact> contactList = new List <Contact>();
    for(Contact con:[select id,accountId,Phone from contact where accountId in:accountIdSet]) {
        if(accountMap != null && accountMap.containsKey(con.accountId)) {
            con.phone = accountMap.get(con.accountId);
            contactList.add(con);
        }
    }
    if(contactList.size() >0) {
        update contactList;
    }

}

Output: Go to any Account and update phone number.The phone number in its related contacts automatically gets updated.

Coding is easy.Just Explore it...

How to prevent password from expiring?

Salesforce provided an option to keep salesforce login password active forever without expiring.

Go to Setup --> Security Controls --> Password Policies 
In 'User Passwords expire in' dropdown we can select 'Never expires' option to prevent password from expiring and click save as shown in the image below.


Friday, February 9, 2018

Apex Code to get previous Monday based on today's date Salesforce

Sample Code:

DateTime previousMondayDate = system.now();
while(!previousMondayDate.format('EEEE').equalsIgnoreCase('Monday')){
previousMondayDate = previousMondayDate.addDays(1);
}
System.debug('Previous Monday Date:'+previousMondayDate);

Output:

Salesforce login URL to login to system directly on single click

If the instance is Sandbox:

https://test.salesforce.com/login.jsp?pw=instancepassword&un=instanceUsername

For example if instance username is test@gmail.com and password is India123 ,then URL to login will be

https://test.salesforce.com/login.jsp?pw=India123&un=test@gmail.com

If the instance is production:
https://login.salesforce.com/login.jsp?pw=instancepassword&un=instanceUsername

For example if instance username is test@gmail.com and password is India123 ,then URL to login will be

https://login.salesforce.com/login.jsp?pw=India123&un=test@gmail.com