Salesforce Scenario based Interview Questions

Salesforce Scenario based Interview Questions

Please click on the scenario to see the solution

Scenario#1 : On Apex Trigger -

Create a Parent Object with name 'Parent' and a Child Object with name 'Child'

Create a Picklist field with name 'Parent Values' in Parent Object with Values '1,2,3,4'

Create a Picklist field with name 'Child Values' in Child Object '1a,1b,1c,2a,2b,2c,Others'

Trigger code should do the below tasks -

  1. If parent object picklist is set to 1 then in Child set picklist value back to null and they can select only 1a,1b,1c in child, for remaining throw error
  2. If parent object picklist is set to 2 then in Child set picklist value back to null and they can select only can select only 2a,2b,2c for remaining throw error
  3. If parent object picklist is set to 3 then in Child set picklist value back to null and they can select only can select only Others for remaining throw error

Solution:-

Please follow the below steps to proceed with this example.

S.No Component
Follow the order as mentioned below to create the components
Configuration
1 Create New App
App Label: Sample
App Name Sample
Note: Enable security for all the profiles.
2 Create New Custom Object
Singular Label: Parent
Plural Label: Parents
Object Name: Parent

Standard Field:
Field Label Parent No:
Data Type: Auto Number
Display Format: P-{00000}
3 Create New Custom Field
Object: Parent__c
Data Type: Picklist
Field Label: Parent Values
Values: Enter values, with each value separated by a new line.
Note:
  • Enter 1, 2, 3, 4 values (each value in a new line)
  • Enable security for all the profiles.
Field Name: Parent_Values
4 Create New Custom Object
Singular Label: Child
Plural Label: Childs
Object Name: Child

Standard Field:
Field Label Child No:
Data Type: Auto Number
Display Format: C-{00000}
5 Create New Custom Field
Object: Child__c
Data Type: Lookup(Parent__c)
Field Label: Parent
Field Name: Parent
Note: Enable security for all the profiles.
6 Create New Custom Field
Object: Child__c
Data Type: Picklist
Field Label: Child Values
Values: Enter values, with each value separated by a new line.
Note:
  • Enter 1a,1b,1c,2a,2b,2c,Others values (each value in a new line)
  • Enable security for all the profiles.
Field Name: Child_Values
7 Create New Custom Tabs
Create tabs for Parent and Child objects Note: Enable only for the Sample Application.
Customization
1 Create New Apex Trigger
Object: Parent__c
Name: ParentTrigger
Note: Please see the trigger logic below under: ***ParentTrigger***
2 Create New Apex Trigger
Object: Parent__c
Name: ChildTrigger
Note: Please see the trigger logic below under: ***ChildTrigger***

***ParentTrigger***

     trigger ParentTrigger on Parent__c (after insert, after update) {    
      List childUpdLst = new List();
      for(Parent__c prnt : [select id,Parent_Values__c, (select Child_Values__c from Childs__r) from Parent__c where id in: trigger.newMap.keyset()]) {
       if(prnt.Parent_Values__c == '1' || prnt.Parent_Values__c == '2' || prnt.Parent_Values__c == '3') {
        for(Child__c child : prnt.Childs__r) {
         child.Child_Values__c = '';
         childUpdLst.add(child);
        }
       }
      }
      if(childUpdLst.size() > 0) {
       TriggerUtility.bypassChildVald = true;
       update childUpdLst;
      }
     }
    

***ChildTrigger***

     trigger ChildTrigger on child__c (before insert, before update) {    
      Set parentIds = new Set();
      for(Child__c child : trigger.new) {
       parentIds.add(child.Parent__c);
      }
      
      Map parentMap = new Map(
       [select Parent_Values__c from Parent__c where id in: parentIds]
      );
      
      for(Child__c child : trigger.new) {
       if(parentMap.get(child.Parent__c).Parent_Values__c == '1' && child.Child_Values__c != '1a' && 
       child.Child_Values__c != '1b' && child.Child_Values__c != '1c' && !TriggerUtility.bypassChildVald)
        child.addError('You can select only 1a or 1b or 1c.');
       else if(parentMap.get(child.Parent__c).Parent_Values__c == '2' && child.Child_Values__c != '2a' && 
       child.Child_Values__c != '2b' && child.Child_Values__c != '2c' && !TriggerUtility.bypassChildVald)
        child.addError('You can select only 2a or 2b or 2c.');
       else if(parentMap.get(child.Parent__c).Parent_Values__c == '3' && child.Child_Values__c != 'Others' 
       && !TriggerUtility.bypassChildVald)
        child.addError('You can select only Others.');
      }
     }
    

Under Construction.

Under Construction.

2 comments:

  1. Hallo There,

    A really interesting, clear and easily readable salesforce-scenario-based-interview article of interesting and different perspectives.I will clap. So much is so well covered here.

    I am saving the name of a salesforce report in a custom setting and want to add the same report as an attachment in an email message that I have to send. I have not really worked on this part and require some assistance. how should I go about?

    I read multiple articles and watched many videos about how to use this tool - and was still confused! Your instructions were easy to understand and made the process simple.

    Thanks and Regards,
    Preethi.

    ReplyDelete
  2. Really very useful information given Srinu. Hats Off to your efforts.

    ReplyDelete