You need to sign in to do that
Don't have an account?
Santosh Borfalkar 15
Please help me with this assignment
Write a trigger on the Lead object that populates a custom “Key Fields Populated” (API Name: Key_Fields_Populated__c) number field whenever the Lead is created. “Key Fields Populated” should count the total number of the following fields that are not null: FirstName LastName Email Phone Website Title This is my code trigger KeyFieldsPopulated on Lead (before insert) { List<Lead> leads = new List<Lead>(); for (Lead L :trigger.new) { Set<String> fieldsPopulated = new Set<String>(); fieldsPopulated.add(L.FirstName); fieldsPopulated.add(L.LastName); fieldsPopulated.add(L.Website); fieldsPopulated.add(L.Title); fieldsPopulated.add(L.Email); fieldsPopulated.add(L.Phone); integer count; for (string field : fieldsPopulated ) { for ( integer i = 0 ; i< fieldsPopulated.size();i++) { if ( field !=null) { System.debug(field); count += 1; L.Key_fields_Populated__c = count; } } } } }
Greetings to you!
I have made few changes in your code. Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
All Answers
Try this code:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Greetings to you!
I have made few changes in your code. Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
I have made some changes to the code this will check the condition that given fields are not null.
Try this:
trigger KeyFieldsPopulated on Lead (before insert)
Thanks,
Ajay Dubedi