+ Start a Discussion
Abraham DurojaiyeAbraham Durojaiye 

How do I create an Apex Trigger to make a custom field in Contacts mandatory to fill out when a specific relationship type is selected?

Hi,

Still getting my feet wet with the developer thing, so I am not sure where to start on this one. 

I have a custom lookup field in my Contacts object called Relationship Manager, which is manually populated with a user name. I also have a custom picklist(multi select) field called Relationship Type, which includes Donor and Board Member as choices. 

I want a trigger that will make the Relationship Manager field mandatory to populate by the user when either the Donor and/or Board Member types are selected.

Thanks
Best Answer chosen by Abraham Durojaiye
Mike ArthurMike Arthur
Hi Abraham

Sorry, my fault!  Try this:

AND (
OR( INCLUDES(relationship_type__c, "Donor"), INCLUDES(relationship_type__c, "Board Member") ), ISBLANK(relationship_manager__c)
)

All Answers

Mike ArthurMike Arthur
Hi Abraham,

Why do you want to use a trigger for this?

I would go for a validation rule on the object.

Make the condition

OR( INCLUDES(relationship_type__c, "Donor"), INCLUDES(relationship_type__c, "Board Member") )

INCLUDES(multiselect_picklist_field, text_literal) Determines if any value selected in a multi-select picklist field equals a text literal you specify.

When this returns TRUE the error message will be displayed.
ManojjenaManojjena
Hi Abraham,

Try with below code  it  will helps !!
trigger multiSelectContactDemo on Contact (before insert,before update ) {
  for (Contact con :Trigger.new ){
    if(con.Relationship_Type__c=='Board Member'||  con.Relationship_Type__c='Donor;Board Member' || con.Relationship_Type__c='B' || con.Relationship_Type__c='Board Member;Donor' && con.Relationship_Manager__c ==null){
        con.addError('Please select relationship manager');
    }
  }
}
Replace your field api name as per your org !!
Let me know if it helps !!
Thanks
Manoj

 
Abraham DurojaiyeAbraham Durojaiye
Hi Mike,

The validation rule you sent me worked. The only issue I am seeing now is that even though I insert a Relationship Manager, the record still does not save and still gives the error message to insert a Relationship Manager.

I attached a screenshot of the validation rule I set upUser-added image

Once a Relationship Manager has been populated, the record sould be able to be saved.

Please advise.
 
Abraham DurojaiyeAbraham Durojaiye
Hi Manoj,

I tried the Apex Trigger you sent me and got an error message about the OR operator only being able to be applied to Boolean expressions.

See below:
User-added image
ManojjenaManojjena
HI Abraham Durojaiye,
In the if condition replace =with == ,means Relationship_Type__c=='Donar;Board member'
Mike ArthurMike Arthur
Hi Abraham

Sorry, my fault!  Try this:

AND (
OR( INCLUDES(relationship_type__c, "Donor"), INCLUDES(relationship_type__c, "Board Member") ), ISBLANK(relationship_manager__c)
)
This was selected as the best answer