Skip to main content

I write below logic in lead helper class and requirement is when Lead is created by integration user profile so the field Lead source is getting locked no one can update lead source  throw an error

public static void integrationuserleadsourceValidation(List<Lead__c> newLeads) {

        // Iterate through the new leads // Check if the lead is created by an integration user

           

        map<Id,Lead__c> oldLeadMap = new map<Id,Lead__c>();

          Set<Id> integrationUserIds = new Set<Id>();

         for (User u : [SELECT Id FROM User WHERE Profile.Name = 'Integration Users']) {

            integrationUserIds.add(u.Id);

        }

        for (Lead__c leadRecord : newLeads) {

             System.debug('Integration User IDs: ' + integrationUserIds);

            System.debug('Lead Created By ID: ' + leadRecord.CreatedById);

         

                      if (integrationUserIds.contains(leadRecord.CreatedById)) {

                           System.debug('Old Lead Source: ' + oldLeadMap.get(leadRecord.Id).Lead_Source__c);

                          System.debug('New Lead Source: ' + leadRecord.Lead_Source__c);

           

     if (leadRecord.CreatedById != leadRecord.OwnerId && oldLeadMap.get(leadRecord.Id).Lead_Source__c !=leadRecord.Lead_Source__c) {

                        leadRecord.addError('Lead Source cannot be changed for leads created by integration users.');

     }}}}

 

and call this method in before update in triggerhandler class 

 

 protected override void beforeUpdate() {

        List<Lead__c> newLeads = new List<Lead__c>();

            LeadHelper.integrationuserleadsourceValidation(newLeads); 

}

 

but its not working  we are not getting error while updating lead source and lead source getting change

답변 1개
  1. 2024년 1월 23일 오전 11:19

    oldLeadMap would be empty here, right? Then your if condition would be failing in most cases.

    Depending on your trigger framework, you should add the oldMap to the parameters and try and see.