• Nandhu
  • NEWBIE
  • 40 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 13
    Questions
  • 9
    Replies
LeavesTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0Wp0000002cmVZEAY; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LeavesTrigger: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Class.TriggerHandlerLeave.afterUpdate: line 12, column 1 Trigger.LeavesTrigger: line 3, column 1: [] Class.TriggerHandlerLeave.afterUpdate: line 16, column 1 Trigger.LeavesTrigger: line 3, column 1

Trigger:trigger LeavesTrigger on Leaves__c (after update, after insert) {
  if (trigger.isAfter && trigger.isUpdate) {
    TriggerHandlerLeave.afterUpdate(Trigger.New);
   }    
}
public with sharing class TriggerHandlerLeave {
  
     public static void handlerAfterUpdate(List<Leaves__c> leaveList){
        Map<Id, Leaves__c> merMap = new Map<Id, Leaves__c>([Select Id, Name,Approval_Status__c,
                                                     Total_Leave__c,
                                                     Req_Days_Off__c 
                                                     From Leaves__c WHERE Approval_Status__c ='Approved']);         
        
         for(Leaves__c approvedList:leaveList){
            if(approvedList.Approval_Status__c =='Approved'){
          
               merMap.get(approvedList.Id).Total_Leave__c = merMap.get(approvedList.Id).Total_Leave__c - merMap.get(approvedList.Id).Req_Days_Off__c;
              
                }
           }      
               Insert merMap.values();
     }
}
test class:
@isTest
public with sharing class TestTriggerHandlerLeave {
    @isTest
    public static void testAfterInsert(){
        Leaves__c objLeave = new Leaves__c (Employee_Name__c='Nani',Approval_Status__c = 'Approved');       
        insert objLeave;
        
        Test.startTest();
        objLeave = new Leaves__c (Employee_Name__c='Nani',Approval_Status__c = 'Approved');
        Database.SaveResult result = Database.insert(objLeave, false);
        Test.stopTest();
    }

}
  • April 11, 2020
  • Like
  • 0
trigger LeavesTrigger on Leaves__c (after update, after insert) {
  if (trigger.isAfter && trigger.isUpdate) {
    TriggerHandlerLeave.onAfterUpdate(Trigger.new,Trigger.oldMap);
   }    
}
public with sharing class TriggerHandlerLeave {
  
     public static void onAfterUpdate(List<Leaves__c> leaveList){
        Map<Id, Leaves__c> merMap = new Map<Id, Leaves__c>([Select Id, Name,Approval_Status__c,
                                                     Total_Leave__c,
                                                     Req_Days_Off__c 
                                                     From Leaves__c]);         
        
         for(Leaves__c approvedList:leaveList){
            if(approvedList.Approval_Status__c =='Approved'){
              Leaves__c   objLeave= new Leaves__c();
               merMap.get(approvedList.Id).Total_Leave__c = merMap.get(approvedList.Id).Total_Leave__c - merMap.get(approvedList.Id).Req_Days_Off__c;
               leaveList.add(objLeave);
                }
           }      
                Insert leaveList;
     }
}
  • April 09, 2020
  • Like
  • 0
Trigger to be written when a new leave request created is approved.
When the Approval process incurs and leave is approved, the Leave status field is changed to Approved.

Criteria : Trigger should work when the 'Approval status' field on Leaves object changes its picklist value to 'APPROVED'
Action : Total Available Leaves = Total Available leaves - Requested Days Off

Trigger:
trigger LeavesTrigger on Leaves__c (after update) {
  TriggerHandlerLeave handler = new TriggerHandlerLeave();
   
   if(trigger.isAfter || trigger.isUpdate){
      handler.onAfterInsert();
   }    
}

Handler class:
public with sharing class TriggerHandlerLeave {
 public void onAfterInsert(){
    totalLeave();
  }
Map<Id, Leaves__c> merMap = new Map<Id, Leaves__c>([Select Id, Name,Approval_Status__c,
                                                     Total_Available_Leave__c,
                                                     Req_Days_Off__c 
                                                     From Leaves__c]);
public void totalLeave(){ 
 for(Leaves__c approvedList:trigger.oldMap){
 if(approvedList.Approval_Status__c =='Approved'){               
 merMap.get(approvedList.Leaves__c).Total_Available_Leave__c=merMap.get(approvedList.Leaves__c).Total_Available_Leave__c - merMap.get(approvedList.Leaves__c).Req_Days_Off__c;
 
       }
}      
update merMap.values();
}
}
 
  • April 09, 2020
  • Like
  • 0
Scenario:Trigger to be written when a new leave request created is approved.
When the Approval process incurs and leave is approved, the Leave status field is changed to Approved.

Criteria : Trigger should work when the 'Approval status' field on Leaves object changes its picklist value to 'APPROVED'
Action : Total Available Leaves = Total Available leaves - Requested Days Off
Trigger:
trigger LeavesTrigger on Leaves__c (after update) {
  TriggerHandlerLeave handler = new TriggerHandlerLeave();  
   handler.onAfterInsert();
}

Handler Class
public with sharing class TriggerHandlerLeave {
 public void onAfterInsert(){
    totalLeave();
  }
Map<Id, Leaves__c> merMap = new Map<Id, Leaves__c>([Select Id, Name,Approval_Status__c,
                                                     Total_Available_Leave__c,
                                                     Req_Days_Off__c 
                                                     From Leaves__c]);
public void totalLeave(){ 
 for(Leaves__c approvedList:Trigger.New){
 if(approvedList.Approval_Status__c =='Approved'){               
 merMap.get(approvedList.Leaves__c).Total_Available_Leave__c=merMap.get(approvedList.Leaves__c).Total_Available_Leave__c - merMap.get(approvedList.Leaves__c).Req_Days_Off__c;
 
       }
}      
update merMap.values();
}
}
  • April 07, 2020
  • Like
  • 0
trigger LeaveTrigger on Leaves__c (before insert,before update) {
  LeaveTriggerHandler objHandler = new LeaveTriggerHandler();

  if(trigger.isBefore){
    if(trigger.isInsert){
      objHandler.beforeInsert(trigger.New);
    }
    else if (trigger.isUpdate){
      objHandler.beforeUpdate(trigger.New);
    }                                                                                                                                                            
  } 
}
handler class:
public  with sharing class LeaveTriggerHandler {
    public void beforeInsert(List<Leaves__c> leaveList){ 
         validateLeave(leaveList);
     }
    public void beforeUpdate(List<Leaves__c> leaveList){
         validateLeave(leaveList);
    }
    public void validateLeave(List<Leaves__c> leaveList){
         Map<Id,Leaves__c> mapCase = new Map<Id,Leaves__c>();
         mapCase=new Map<Id,Leaves__c>([SELECT Id,Name,Approval_Status__c FROM Leaves__c]);
       
        for(Leaves__c obj:leaveList){
           if(Approval_Status__c=='Approved'){
              Total_Available_Leaves__c = Total_Available_Leaves__c - Req_Days_Off__c;
            }
            leaveList.add(obj);
        }
    }
}    
  • April 07, 2020
  • Like
  • 0
I need to calculate the no.of.working day in a month with weekends also for 7 days.?
i) Available of leave is 4 days in a monthly, if he is taking more than 4 days that must go to loss pay should be ,minus from his annual salary.
ii) if he is taking less than 4 days. if the leave taken must be deduction the available leave. The value should not be negative.
  • January 30, 2020
  • Like
  • 0
Parentobject field:firstname,lastname
 given lookup relation that auto populates list in the childobject as fullname using trigger.

 
  • January 20, 2020
  • Like
  • 0
trigger TriggerFullNameExample on Opportunity (before insert) 
{   
    List<Account> acc=new List<Account>();
    for(Opportunity opp:trigger.new) 
{    
    if(acc.First_Name__c + '' + acc.Last_Name__c==opp.FullName__c)
    {
        Account acco= new Account();
        acco.First_Name__c+ '' + acco.Last_Name__c== opp.FullName__c;
        acc.add(acco);
    }
}
    insert acc;
}
  • January 20, 2020
  • Like
  • 0
Calculate the no.of.working day,sick leave,?
can anybody suggest or idea to me?
  • January 17, 2020
  • Like
  • 0
Hi 
I need latest dumps for salesforce platform-1 for winter 2020
  • December 20, 2019
  • Like
  • 0
public class ContactSearch {

    public Static List<Contact> getSearchForContacts(String lastName,String mailingPostalCode)
    {
        List<Contact> myobj = [SELECT ID,LastName,MailingPostalCode FROM Contact WHERE LastName=:lastName AND MailingPostalCode=:mailingPostalCode];
       System.debug('.....:' +myobj);
        return myobj;
}
}

Sql editor
:SELECT ID, Name, MailingPostalCode FROM Contact WHERE LastName ='Davis'
Anonymous debug:
ContactSearch.getSearchForContacts('Young','66045');

 
  • November 12, 2019
  • Like
  • 0
I am trying skip particular user in a formula ,but when I applied the condition it blocking all the users.

highlited the part
also in custom lable added the  user in 'user',
 AND(
NOT($User.Username !=$Label.customlabelname),
NOT(ISNEW()),
OR(ISCHANGED([Object.field1),ISCHANGED([object].field2)),
[object].field1=true,
[object].field2!=null,
[object.field2 = false
)
  • April 09, 2020
  • Like
  • 0
trigger LeaveTrigger on Leaves__c (before insert,before update) {
  LeaveTriggerHandler objHandler = new LeaveTriggerHandler();

  if(trigger.isBefore){
    if(trigger.isInsert){
      objHandler.beforeInsert(trigger.New);
    }
    else if (trigger.isUpdate){
      objHandler.beforeUpdate(trigger.New);
    }                                                                                                                                                            
  } 
}
handler class:
public  with sharing class LeaveTriggerHandler {
    public void beforeInsert(List<Leaves__c> leaveList){ 
         validateLeave(leaveList);
     }
    public void beforeUpdate(List<Leaves__c> leaveList){
         validateLeave(leaveList);
    }
    public void validateLeave(List<Leaves__c> leaveList){
         Map<Id,Leaves__c> mapCase = new Map<Id,Leaves__c>();
         mapCase=new Map<Id,Leaves__c>([SELECT Id,Name,Approval_Status__c FROM Leaves__c]);
       
        for(Leaves__c obj:leaveList){
           if(Approval_Status__c=='Approved'){
              Total_Available_Leaves__c = Total_Available_Leaves__c - Req_Days_Off__c;
            }
            leaveList.add(obj);
        }
    }
}    
  • April 07, 2020
  • Like
  • 0
Hi All
I'm completely new to Apex triggers but have managed to put together the trigger below to automatically send account records to a 2nd salesforce org when the record is created usng S2S .The trigger works perfectly on our sandbox but I am now unable to deploy it to production because the relevant tests have not been run. I've tried looking at documentation on how to do this but I've had no success so far. If anyone could help me out on this I'd be extremely grateful!

trigger SendAccountsToConnection on Account (after insert) {
        PartnerNetworkConnection conn = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection  where ConnectionStatus = 'Accepted' and ConnectionName = 'xxxabc'];
        List<PartnerNetworkRecordConnection> recordConnectionToInsert  = new List<PartnerNetworkRecordConnection>  ();
        for (Account acc : Trigger.new){
            PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();

            newrecord.ConnectionId = conn.Id;
            newrecord.LocalRecordId = acc.id;  
            newrecord.SendClosedTasks = false;
            newrecord.SendOpenTasks = false;
            newrecord.SendEmails = false;
            recordConnectionToInsert.add(newrecord);
        }
        if (recordConnectionToInsert.size() > 0){
            System.debug('>>> Sharing ' + recordConnectionToInsert.size() + ' records');
            insert recordConnectionToInsert;
        }
}
trigger TriggerFullNameExample on Opportunity (before insert) 
{   
    List<Account> acc=new List<Account>();
    for(Opportunity opp:trigger.new) 
{    
    if(acc.First_Name__c + '' + acc.Last_Name__c==opp.FullName__c)
    {
        Account acco= new Account();
        acco.First_Name__c+ '' + acco.Last_Name__c== opp.FullName__c;
        acc.add(acco);
    }
}
    insert acc;
}
  • January 20, 2020
  • Like
  • 0
public class ContactSearch {

    public Static List<Contact> getSearchForContacts(String lastName,String mailingPostalCode)
    {
        List<Contact> myobj = [SELECT ID,LastName,MailingPostalCode FROM Contact WHERE LastName=:lastName AND MailingPostalCode=:mailingPostalCode];
       System.debug('.....:' +myobj);
        return myobj;
}
}

Sql editor
:SELECT ID, Name, MailingPostalCode FROM Contact WHERE LastName ='Davis'
Anonymous debug:
ContactSearch.getSearchForContacts('Young','66045');

 
  • November 12, 2019
  • Like
  • 0
I've been working on this trail for a bit - and while I undertsand the concepts, I'm stumped on why I'm getting this error: 
 
Challenge Not yet complete... here's what's wrong: 
A Create a Record action for the Closed Won criteria node isn't properly configured. Make sure that it creates a draft contract according to the instructions in the ‘Create contract for closed opportunity’ task action. Make sure that Start Date is set by using a formula.

I've gone over all of the workflow tasks information to recreate the contact. If I activate the process and close an opportunity, it creates a contract with the expected information and the date is 1mo away with 12mo term. The workflow's task (which is the template for the actual contract) specifies the following: 
 
Use the closed opportunity to create a contract for the associated account. 

Account: The account associated with this opportunity
Status: Draft
Contract Start Date: 1 month from today
Contract Term: 12
Here are my actions: 
User-added image

The Contract Start Date is set to the following formula:
DATE(
    YEAR(Today()) +
    FLOOR((1 + MONTH(Today())) / 12) -
    IF (MOD(MONTH(Today()) + 1, 12) = 0,
        1,
        0),

    MOD((1 + MONTH(Today()) - 1), 12) + 1,

    DAY(Today())
)

Again, for all practical purposes I'm passing this, as it functions and the contract is created. But I must be missing some small detail, named field, something that's tripping up the validation settings. Any help is appreciated!