• Mamadou Diallo 14
  • NEWBIE
  • 105 Points
  • Member since 2015

  • Chatter
    Feed
  • 2
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 30
    Replies
Hey there,

We have the standard field "Name" for an object, and then a custom picklist Type field. We want to auto-populate the standard name with whatever the user picksf or the picklist, and then be able to change it if needed.

I can't create a new custom formula field because the Name is standard and cannot be removed, and it wouldn't be editable, any other thoughts?
I need help with a validation rule to make a selection from picklist call_outcome__c mandatory if value "Call" was selected from picklist subject.

This is one of many things I've tried: AND(ISPICKVAL( Subject ,'Call'),ISPICKVAL( call_outcome__c ,''))

Thank you in advance
Hello,

I'm merging Account triggers into one but I'm getting Apex CPU time limit exceeded. 

Here is the Apex Class
trigger AccountTrigger on Account (after insert, after update, before insert, before update) {
     AccountTriggerHandler  handler = new AccountTriggerHandler (Trigger.oldMap, Trigger.newMap);

               if((Trigger.isAfter && Trigger.isUpdate) || (Trigger.isAfter && Trigger.isInsert)){
                handler.acctSegmentRules();
           
      }        
}

And the trigger
public class AccountTriggerHandler {
    Map<Id, Account> oldAccts;
    Map<Id, Account> newAccts;
    
    public AccountTriggerHandler (Map<Id, Account> oldTriggerAccts, 
    Map<Id, Account> newTriggerAccts){
        oldAccts = oldTriggerAccts;
        newAccts = newTriggerAccts;
    }

/ Account Segment Rules
    public void acctSegmentRules(){
   // Set of Owners Ids
    Set<Id> ownerIds = new Set<Id>();
    //Create a list of accounts to update 
    List<Account> acctsListToUpdate = new List<Account>(); 
    if(newAccts != null){
		for (Account acc : newAccts.values()) {
            ownerIds.add(acc.ownerId);
        }
    }   
    String accName;      
    
    //Check if the trigger is running again
    if(RecursiveTriggerHandler.runOnce()){
        
        for (Account a : newAccts.values()) {
         //Query the owner role description
            // Check if the account ID is not null 
            // to avoid "referencing null object" error       
            if(a != NULL  && a.ID !=NULL){
          
				// create a placeholder account a1    
				 Account a1  = new Account(Id = a.Id, Name = a.Name, Original_ID__c = a.Original_ID__c, OwnerId = a.OwnerId);
				//Check if a1 is not not null and name is not empty
				if(a1 != NULL && a1.Name != NULL){
					accName = a1.Name;
				}
				//Set the condition for AAA & BBB
				Boolean result1 = accName.contains('AAA');
				Boolean result2 = accName.contains('BBB');  
            
                if(a1.Original_ID__c == '895010423'&& result1 == FALSE && result2 == FALSE ){
                        //Change the SubGroup to Other Dealer    
                       a1.SubGroup__c = 'Other Dealer';
                        // add a1 to the list of accounts to update     
                        acctsListToUpdate.add(a1);  
                       } else    
                        // Check if name contains AAA
                        if(result1 == TRUE && result2 == FALSE && a1.Original_ID__c != '895010423'){
                             //Change the Segment to AAA    
                            a1.SubGroup__c = 'Dealer - AAA';
                              //  add a1 to the list of accounts to update     
                              acctsListToUpdate.add(a1);  
                            }else if( //Check if name contains BBB
                            result2 == TRUE && result1 == FALSE && a1.Original_ID__c != '895010423'){
                             //Change the Segment to BBB   
                             a1.SubGroup__c = 'Dealer - BBB';
                              //  add a1 to the list of accounts to update     
                              acctsListToUpdate.add(a1);  
                            }else if(result2 == FALSE && result1 == FALSE && a1.Original_ID__c != '895010423'){
                                 
                               Map<Id,User> userMap = new Map<Id,User>([Select Id,Employee_Function__c From User where Id IN :ownerIds]);
                                if(userMap.containskey(a1.OwnerId)) {
                                    if( userMap.get(a1.OwnerId).Employee_Function__c=='Operations'){
                                        a1.SubGroup__c='Other';
                                        acctsListToUpdate.add(a1);  
                                    }
                                    else
                                    {
                                        a1.SubGroup__c=userMap.get(a1.OwnerId).Employee_Function__c;
                                        acctsListToUpdate.add(a1);  
                                    }
                                }      
                            }  
                } 
                //Bulkify the update    
                if(acctsListToUpdate.size()>0){ 
                    update acctsListToUpdate; 
                }            
        }     
    } 
}
Thank you for your help.
Hello,

I have trigger that will update a custom field Segment__c on the account based on the owner Role Description. I'm getting this error when I change the owner of the account to someone in the Operations department:
Error: Apex trigger SegmentRulesTrigger caused an unexpected exception, contact your administrator: SegmentRulesTrigger: execution of AfterUpdate

caused by: System.ListException: Duplicate id in list:

Here is the part of the trigger that is not working:
trigger SegmentRulesTrigger on Account (after insert, after update) {
    
    //Create a list of accounts to update 
    List<Account> acctsListToUpdate = new List<Account>(); 
   
   
	//check if trigger is running again
    if(RecursiveTriggerHandler.runOnce()){
    
        for (Account a : Trigger.new) {
         //Query the owner role description
			for(User u : [SELECT id, Role_Description__c FROM User WHERE id = :a.OwnerId]){
				// Check if the account ID is not null 
				// to avoid "referencing null object" error       
				if(a != NULL  && a.ID !=NULL){
				// create a placeholder account a1    
				Account a1  = new Account(Id = a.Id, Name = a.Name, OwnerId = a.OwnerId);
				
					//Map Account ID
					Account oldAcct = Trigger.oldMap.get(a1.Id);
									//Check if Account Owner or Name has changed
								   if (oldAcct.OwnerId != a1.OwnerId || oldAcct.Name != a1.Name){
									//  User u1 = new User(Id= u.Id, Role_Description__c = u.Role_Description__c);
										//Check if Owner role description is Operations
									   if( u.Role_Description__c == 'Operations' )
											 {
											  a1.Segment__c = 'Other';
											  acctsListToUpdate.add(a1); 
										}
											 a1.Segment__c = u.Role_Description__c;
											 acctsListToUpdate.add(a1); 
									 } 
								
				} 
                //Bulkify the update    
                if(acctsListToUpdate.size()>0){
                    update acctsListToUpdate;            
                } 
			}				
		}	
	}
}

Thank you for your help.​
Hello,

I have 3 custom objects: Commission, Tax ID and Broker Appointment. Commission and Broker Appointment are not related but Tax ID has 1 to many relationship with each of those objects. Commission and Broker Appointment have TAX ID as a lookup field. 
I want to updated a field in Commission when Broker Appointment Status is Approved. 

Here is the code I built.
trigger ReleaseCommissionWhenBrokerApproved on Broker_Appointment__c (after update) {

     map<Id, Broker_Appointment__c> brokerApptIds = new map<Id, Broker_Appointment__c>();
   
        for (Broker_Appointment__c a : trigger.New){
            if ( a.Tax_ID__c != null && a != null && a.Appointment_Status__c == 'Approved'){
            brokerApptIds.put(a.Tax_ID__c, a);
          }
        }
    
    List<Commission__c> comm = [ SELECT Id, Tax_ID__c, Underwriter__c, Domicile_State1__c, Broker_Writing_Agent__c, On_Hold_Reason__c, Hold_Payment__c FROM Commission__c WHERE Tax_ID__c IN :brokerApptIds.keySet() AND Hold_Payment__c = TRUE];
  
    Broker_Appointment__c appt;
       
    for (Commission__c c: comm){
        
             if(!brokerApptIds.isEmpty() && brokerApptIds.containsKey(c.Tax_ID__c) && appt != null ){
       			 if (appt.Underwriter_Account__c == c.Underwriter__c && appt.Producer_Contact__c == c.Broker_Writing_Agent__c && appt.State__c == c.Domicile_State1__c)
                 {
                    c.Hold_Payment__c = FALSE;      
               }  
          }
       update comm;
    }        
}

When I remove the line "if (appt.Underwriter_Account__c == c.Underwriter__c && appt.Producer_Contact__c == c.Broker_Writing_Agent__c && appt.State__c == c.Domicile_State1__c)", the trigger works but I need to compare thos fields before updating the fields.

Help need please. Maybe I'm missing something.

Thank you.
Hello,
I have a controller and its test class. The code coverage is currently at 0%. I tried different scenarios but I had the same issue. I'm sharing the 2 Apex classes. There are 2 formula fields Number_Of__Employees_Summary__c and Winning_Team__c. The custom field Date_Time_Closed__c is updated by workflow when an opportunity is won or renewed.
@isTest
public class MyControllerTest {
    static testMethod void MyController(){
        Integer numAccts;
        Integer numOppsPerAcct;
        List<Account> accts = new List<Account>();
        
        for(Integer i=0;i<numAccts;i++) {
            Account a = new Account(Name='TestAccount' + i,
                                   RecordTypeId = '012800000003UY8', 
                                   Type = 'Prospect',  
                                   Number_Of_Employees__c = 190 + i
                                   );
            accts.add(a);
        }
        insert accts;
        
        List<Opportunity> opps = new List<Opportunity>();
        for (Integer j=0;j<numAccts;j++) {
            Account acct = accts[j];
            // For each account just inserted, add opportunities
            for (Integer k=0;k<numOppsPerAcct;k++) {
                opps.add(new Opportunity(Name=acct.Name + ' Opportunity ' + k,
                                       CloseDate=System.today().addMonths(1),
                                       StageName = 'Closed Won',
                                       Number_Of_Employees_From_Opp = 200 + k,
                                       AccountId=acct.Id));
            }
        }
        // Insert all opportunities for all accounts.
        insert opps;
        //Populate the formula fields
       List<Opportunity> testOppAfterInsert = new List<Opportunity>();
         testOppAfterInsert = [select Name, Owner.Name, Number_Of__Employees_Summary__c, Number_Of_Employees_From_Opp, Account.Number_Of__Employees__c, Winning_Team__c, StageName, Date_Time_Closed__c from Opportunity WHERE Id IN :opps];
        }  
         MyController xyz = new MyController();
          }
}

Here is the Apex controller
 
public with sharing class MyController{ 
public List<Opportunity> Records {get; set;} 
public MyController(){ 
Records = [select Name, Owner.Name, Date_Time_Closed__c, Winning_Team__c, Number_Of_Employees_Summary__c, StageName from Opportunity 
 WHERE Number_Of_Employees_Summary > 200 AND Date_Time_Closed__c != null ORDER BY Date_Time_Closed__c DESC Limit 1 ]  ; 
} 
}

Thank you for your help.
Hello, I'm creating a test class for a visualforce controller but I'm getting the "Initial term of field expression must be a concrete SObject: List<Opportunity>" error. 

I have a formula field on the opportunity object : Number_of_Employess_Summary__c

Here is my code
@isTest
public class MyController {
    static testMethod void ringMyBell(){
        Integer numAccts;
        Integer numOppsPerAcct;
        List<Account> accts = new List<Account>();
        
        for(Integer i=0;i<numAccts;i++) {
            Account a = new Account(Name='TestAccount' + i,
                                   RecordTypeId = '012800000003UY8', 
                                   Type = 'Prospect',  
                                   Number_Of_Employees__c = 190 + i
                                   );
            accts.add(a);
        }
        insert accts;
        
        List<Opportunity> opps = new List<Opportunity>();
        for (Integer j=0;j<numAccts;j++) {
            Account acct = accts[j];
            // For each account just inserted, add opportunities
            for (Integer k=0;k<numOppsPerAcct;k++) {
                opps.add(new Opportunity(Name=acct.Name + ' Opportunity ' + k,
                                       CloseDate=System.today().addMonths(1),
                                       StageName = 'Closed Won',
                                       Number_Of_Employees_From_Opp = 200 + k,
                                       AccountId=acct.Id));
            }
        }
        // Insert all opportunities for all accounts.
        insert opps;
        //Populate the formula field Eligible Employees Summary
        for(List<Opportunity> testOppAfterInsert: opps){
         testOppAfterInsert = [select Name, Owner.Name, Number_Of__Employees_Summary__c, Number_Of_Employees_From_Opp, Account.Number_Of__Employees__c, StageName from Opportunity 
                                          WHERE Id IN :opps.Id];
        }  
         MyController xyz = new MyController();
          }
}

Help needed please.

Thank you.
Hello there. I have an issue with this trigger and I need help please. I have a custom object called obejctA and a custom picklist field Test in the opportunity object. Here is I want: if a new record X is added to the objectA, I want that record X to be added to the picklist Test. Unfortunatly, when I create a new record on objectA, I got this error "Web service callout failed: WebService returned a SOAP Fault:
INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session faultcode=sf:INVALID_SESSION_ID faultactor=_
"

Here is my code (trigger and class):
//////////// THE TRIGGER //////////

trigger UpdateTestFromObjectA on objectA__c (after insert) {
    objectA__c[] xyz = trigger.new;
    UpdateTestPicklist.updatePicklist(Trigger.newMap.keySet()); 
}

//////////// THE CLASS ////////////

public class UpdateTestPicklist {
    public static MetadataService.MetadataPort createService() {
        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        return service;
    }
    public class MetadataServiceExamplesException extends Exception { }
    public static void handleSaveResults(MetadataService.SaveResult saveResult)
    {
        // Nothing to see?
        if(saveResult==null || saveResult.success)
            return;
        // Construct error message and throw an exception
        if(saveResult.errors!=null)
        {
            List<String> messages = new List<String>();
            messages.add(
                (saveResult.errors.size()==1 ? 'Error ' : 'Errors ') +
                    'occured processing component ' + saveResult.fullName + '.');
            for(MetadataService.Error error : saveResult.errors)
                messages.add(
                    error.message + ' (' + error.statusCode + ').' +
                    ( error.fields!=null && error.fields.size()>0 ?
                        ' Fields ' + String.join(error.fields, ',') + '.' : '' ) );
            if(messages.size()>0)
                throw new MetadataServiceExamplesException(String.join(messages, ' '));
        }
        if(!saveResult.success)
            throw new MetadataServiceExamplesException('Request failed with no specified error.');
    }
    
    public class soapSforceComSchemasClassUpdateOpportunity {
        soapSforceComSchemasClassUpdateOpportunity.SessionHeader_element webserviceSessionHeader = new soapSforceComSchemasClassUpdateOpportunity.SessionHeader_element(); 
        webserviceSessionHeader.sessionId = partnerLoginResult.sessionId; 
        soapSforceComSchemasClassUpdateOpportunity.updatePicklist  myWebservice = new soapSforceComSchemasUpdateOpportunity.updatePicklist();
        myWebservice.SessionHeader = webserviceSessionHeader; 
        myWebservice.updatePicklist('myusername','mypassword')
    }
    @future(callout=true)
    public static void updatePicklist(set<ID> recordIDs)
    {
              
        // Get new record from objectA 
        list<objectA__c> xyz = [SELECT Id, Name FROM objectA__c WHERE Id IN: recordIds];
        for (objectA__c a: xyz){
        
             MetadataService.MetadataPort service = createService();

        // Read Custom Field
        MetadataService.CustomField customField =
            (MetadataService.CustomField) service.readMetadata('CustomField',
                new String[] { 'Opportunity.test__c' }).getRecords()[0];
            
        // Add objectA to picklist values
        metadataservice.PicklistValue theValue = new metadataservice.PicklistValue();
        theValue.fullName= a.Name;
        theValue.default_x=false;  
        customField.picklist.picklistValues.add(theValue);
        
        // Update Custom Field
        handleSaveResults(
            service.updateMetadata(
                new MetadataService.Metadata[] { customField })[0]); 
        }    
    }
}
The SOAP apex classes (AsyncSoapSforceCom200608Apex, soapSforceCom200608Apex etc.. )exist in my org. 
Thank you again for your help.
Hello!

I am getting a "Attempt to de-reference a null object" error in the developer console. I go to the log section and double click on the log, and find the error message. Though it doesnt tell me anything about what the cause is or where its coming from... all it says is "12:24:10:089 FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object" 


is there anyway I can view the full strack trace? (Would like to see line, column, variable, etc). 
Hey there,

We have the standard field "Name" for an object, and then a custom picklist Type field. We want to auto-populate the standard name with whatever the user picksf or the picklist, and then be able to change it if needed.

I can't create a new custom formula field because the Name is standard and cannot be removed, and it wouldn't be editable, any other thoughts?
Hello,

I have trigger that will update a custom field Segment__c on the account based on the owner Role Description. I'm getting this error when I change the owner of the account to someone in the Operations department:
Error: Apex trigger SegmentRulesTrigger caused an unexpected exception, contact your administrator: SegmentRulesTrigger: execution of AfterUpdate

caused by: System.ListException: Duplicate id in list:

Here is the part of the trigger that is not working:
trigger SegmentRulesTrigger on Account (after insert, after update) {
    
    //Create a list of accounts to update 
    List<Account> acctsListToUpdate = new List<Account>(); 
   
   
	//check if trigger is running again
    if(RecursiveTriggerHandler.runOnce()){
    
        for (Account a : Trigger.new) {
         //Query the owner role description
			for(User u : [SELECT id, Role_Description__c FROM User WHERE id = :a.OwnerId]){
				// Check if the account ID is not null 
				// to avoid "referencing null object" error       
				if(a != NULL  && a.ID !=NULL){
				// create a placeholder account a1    
				Account a1  = new Account(Id = a.Id, Name = a.Name, OwnerId = a.OwnerId);
				
					//Map Account ID
					Account oldAcct = Trigger.oldMap.get(a1.Id);
									//Check if Account Owner or Name has changed
								   if (oldAcct.OwnerId != a1.OwnerId || oldAcct.Name != a1.Name){
									//  User u1 = new User(Id= u.Id, Role_Description__c = u.Role_Description__c);
										//Check if Owner role description is Operations
									   if( u.Role_Description__c == 'Operations' )
											 {
											  a1.Segment__c = 'Other';
											  acctsListToUpdate.add(a1); 
										}
											 a1.Segment__c = u.Role_Description__c;
											 acctsListToUpdate.add(a1); 
									 } 
								
				} 
                //Bulkify the update    
                if(acctsListToUpdate.size()>0){
                    update acctsListToUpdate;            
                } 
			}				
		}	
	}
}

Thank you for your help.​
Hello,

I have 3 custom objects: Commission, Tax ID and Broker Appointment. Commission and Broker Appointment are not related but Tax ID has 1 to many relationship with each of those objects. Commission and Broker Appointment have TAX ID as a lookup field. 
I want to updated a field in Commission when Broker Appointment Status is Approved. 

Here is the code I built.
trigger ReleaseCommissionWhenBrokerApproved on Broker_Appointment__c (after update) {

     map<Id, Broker_Appointment__c> brokerApptIds = new map<Id, Broker_Appointment__c>();
   
        for (Broker_Appointment__c a : trigger.New){
            if ( a.Tax_ID__c != null && a != null && a.Appointment_Status__c == 'Approved'){
            brokerApptIds.put(a.Tax_ID__c, a);
          }
        }
    
    List<Commission__c> comm = [ SELECT Id, Tax_ID__c, Underwriter__c, Domicile_State1__c, Broker_Writing_Agent__c, On_Hold_Reason__c, Hold_Payment__c FROM Commission__c WHERE Tax_ID__c IN :brokerApptIds.keySet() AND Hold_Payment__c = TRUE];
  
    Broker_Appointment__c appt;
       
    for (Commission__c c: comm){
        
             if(!brokerApptIds.isEmpty() && brokerApptIds.containsKey(c.Tax_ID__c) && appt != null ){
       			 if (appt.Underwriter_Account__c == c.Underwriter__c && appt.Producer_Contact__c == c.Broker_Writing_Agent__c && appt.State__c == c.Domicile_State1__c)
                 {
                    c.Hold_Payment__c = FALSE;      
               }  
          }
       update comm;
    }        
}

When I remove the line "if (appt.Underwriter_Account__c == c.Underwriter__c && appt.Producer_Contact__c == c.Broker_Writing_Agent__c && appt.State__c == c.Domicile_State1__c)", the trigger works but I need to compare thos fields before updating the fields.

Help need please. Maybe I'm missing something.

Thank you.
Hello,
I have a controller and its test class. The code coverage is currently at 0%. I tried different scenarios but I had the same issue. I'm sharing the 2 Apex classes. There are 2 formula fields Number_Of__Employees_Summary__c and Winning_Team__c. The custom field Date_Time_Closed__c is updated by workflow when an opportunity is won or renewed.
@isTest
public class MyControllerTest {
    static testMethod void MyController(){
        Integer numAccts;
        Integer numOppsPerAcct;
        List<Account> accts = new List<Account>();
        
        for(Integer i=0;i<numAccts;i++) {
            Account a = new Account(Name='TestAccount' + i,
                                   RecordTypeId = '012800000003UY8', 
                                   Type = 'Prospect',  
                                   Number_Of_Employees__c = 190 + i
                                   );
            accts.add(a);
        }
        insert accts;
        
        List<Opportunity> opps = new List<Opportunity>();
        for (Integer j=0;j<numAccts;j++) {
            Account acct = accts[j];
            // For each account just inserted, add opportunities
            for (Integer k=0;k<numOppsPerAcct;k++) {
                opps.add(new Opportunity(Name=acct.Name + ' Opportunity ' + k,
                                       CloseDate=System.today().addMonths(1),
                                       StageName = 'Closed Won',
                                       Number_Of_Employees_From_Opp = 200 + k,
                                       AccountId=acct.Id));
            }
        }
        // Insert all opportunities for all accounts.
        insert opps;
        //Populate the formula fields
       List<Opportunity> testOppAfterInsert = new List<Opportunity>();
         testOppAfterInsert = [select Name, Owner.Name, Number_Of__Employees_Summary__c, Number_Of_Employees_From_Opp, Account.Number_Of__Employees__c, Winning_Team__c, StageName, Date_Time_Closed__c from Opportunity WHERE Id IN :opps];
        }  
         MyController xyz = new MyController();
          }
}

Here is the Apex controller
 
public with sharing class MyController{ 
public List<Opportunity> Records {get; set;} 
public MyController(){ 
Records = [select Name, Owner.Name, Date_Time_Closed__c, Winning_Team__c, Number_Of_Employees_Summary__c, StageName from Opportunity 
 WHERE Number_Of_Employees_Summary > 200 AND Date_Time_Closed__c != null ORDER BY Date_Time_Closed__c DESC Limit 1 ]  ; 
} 
}

Thank you for your help.