Posts

Showing posts from March, 2017

Update Account Comment from Task Insertion Using Trigger

Requirement: Write a trigger to update account comment field from Task comment value on insertion of Task under Account Solution : trigger updateAccountCommentFromTask on Task (before insert) { String account_prefix = Schema.SObjectType.Account.getKeyPrefix(); Map<Id,Account> accountMap = new Map<Id,Account>(); Set<id> setofWhatID = new Set<id>(); for (Task objTask: Trigger.new)  { setofWhatID.add(objTask.WhatId); } Map<id,Account> MapAccIDToAccount = new Map<id,Account>([Select Id, Account_Status_Sales__c from Account Where Id in :setofWhatID]); for (Task objTask: Trigger.new) if(objTask.Subject != null && objTask.WhatId !=null && ((String)objTask.WhatId).startsWith(account_prefix)) { Account a = MapAccIDToAccount.get(objTask.WhatId); a.Comment__C = objTask.Comment__c; accountMap.put(a.id,a); } try { update accountMap.values(); } catch (system.Dmlexception e)  { system.debu

Duplicate Prevent Lead Based On Email

Requirement : Write a trigger to prevent duplicate Email on Lead Object Solution : trigger leadDuplicatePreventer on Lead (before insert, before update) { Map<String, Lead> leadMap = new Map<String, Lead>(); for (Lead lead : System.Trigger.new) { if ((lead.Email != null) &&     (System.Trigger.isInsert || (lead.Email != System.Trigger.oldMap.get(lead.Id).Email))) { if (leadMap.containsKey(lead.Email)) {   lead.Email.addError('Another new lead has the ' + 'same email address.'); } else {      leadMap.put(lead.Email, lead);  } } } for (Lead lead : [SELECT Email FROM Lead WHERE Email IN :leadMap.KeySet()]) { Lead newLead = leadMap.get(lead.Email); newLead.Email.addError('A lead with this email ' + 'address already exists.'); } }                              Thanks,                            Sumit Shukla

Save the Batch Error Job ID in Custom Object in case of Failure

Requirement :  Get the Failure record when processing thru batch Class Solution : Create a custom object for saving batch log errors and create a batch class   Custom Object: Batch_Log_Error__c  Custom Fields : Job_id__c,Record_Id__c,Error__c global class BatchClassName implements Database.batchable<sObject>,Schedulable{ String query ; // define Constructor global BatchClassName(){  query = 'Select id from SobjectapiName'; }  // Start Method Call    global Database.QueryLocator start(Database.BatchableContext BC){         return Database.getQueryLocator(query);    }        //  Execute Method Call global void execute(Database.BatchableContext bc, List<SobjectapiName> scope){ // Declare SobjectList and BatchErrorLoglst  list<SobjectapiName> lstofsobject  = new list<SobjectapiName>(); list<Batch_Log_Error__c> lstObjBatchLogError = new list<Batch_Log_Error__c>() try{ for(SobjectapiName obj :sco

Show Hyper Link On Add Error in Salesforce Trigger

Requirement: Check Duplicate record in system and show the add error in form of hyperlink corresponding duplicate record ID Solution : Write a trigger and show add error objExistingRecord >>> OldRecord Id objNewRecord >> New Record  objNewRecord.addError('Same Combination of Record already Inserted in System: <a href=\'/'+ objExistingRecord.id +'\'>'+objExistingRecord.Name+'</a>', false);                                                                           Thanks                                                Sumit Shukla

Schedule Class for every One Hour Everyday in Salesforce

Write below code in "Developer Console" to schedule class for every One Hour Everyday.  Apex Code: NameofScheduleclassEveryhour objschclass =  new NameofScheduleclassEveryhour (); string strSeconds = '0'; string strMinutes = '0'; string strHours = '0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23'; string strDay_of_month = '*'; string strMonth = '*'; string strDay_of_week = '?'; String sch = strSeconds + ' ' + strMinutes + ' ' + strHours + ' ' + strDay_of_month + ' ' + strMonth + ' ' + strDay_of_week; system.schedule('ScheduleclassEveryhour', sch, objschclass);                                    Thanks,                               Sumit Shukla