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 -
- 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
- 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
- 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:
|
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:
|
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) { ListchildUpdLst = 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) { SetparentIds = 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.
Hallo There,
ReplyDeleteA 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.
Really very useful information given Srinu. Hats Off to your efforts.
ReplyDeleteRequirement to share the records -
ReplyDeleteUser >
Contact / Person Account
> Client Cases
> Account --> Read/Write Access
Set conIds = new Set();
for(User usr : trigger.new) {
conIds.add(usr.ContactId);
}
if(conIds.size() > 0) {
Map conIdAccIdMap = new Map();
for(Case cs : [select Id, ContactId, AccountId from Case where ContactId in: conIds]) {
conIdAccIdMap.put(cs.ContactId,cs.AccountId);
}
//Triggering sharing Logic
List accshareLst = new List();
for(User usr : trigger.new) {
if(conIdAccIdMap.containsKey(usr.ContactId) && conIdAccIdMap.get(usr.ContactId) != null) {
accshareLst.add(
new AccountShare(
AccountId = conIdAccIdMap.get(usr.ContactId),
UserOrGroupID = usr.Id,
AccountAccessLevel = 'Edit'
)
);
}
}
if(accshareLst.size() > 0) {
try {
insert accshareLst;
}
catch(catch e) {
system.debug('Account Trigger > accshareLst exception: '+e.getMessage()+' in line number: '+e.getLineNumber());
}
}
}
AccountShare share = new AccountShare();
share.setAccountId(rec.getId());
//Set the portal user Id to share the accounts with
share.setUserOrGroupId("003D000000QB8T8");
share.setAccountAccessLevel("Edit");
share.setOpportunityAccessLevel("Read");
share.setCaseAccessLevel("Edit");
shares.add(share);
if(trigger.isInsert) {
DeleteSet conIds = new Set();
for(User usr : trigger.new) {
conIds.add(usr.ContactId);
}
if(conIds.size() > 0) {
Map> conIdAccIdMap = new Map>();
for(Case cs : [select Id, ContactId, AccountId from Case where ContactId in: conIds]) {
if(conIdAccIdMap.containsKey(cs.ContactId)) {
Set accIds = new Set();
accIds = conIdAccIdMap.get(cs.ContactId);
accIds.add(cs.AccountId);
conIdAccIdMap.put(cs.ContactId,accIds);
}
else {
conIdAccIdMap.put(cs.ContactId,new Set{cs.AccountId});
}
}
//Triggering sharing Logic
List accshareLst = new List();
for(User usr : trigger.new) {
if(conIdAccIdMap.containsKey(usr.ContactId) && conIdAccIdMap.get(usr.ContactId) != null) {
for(Id accId : conIdAccIdMap.get(usr.ContactId)) {
accshareLst.add(
new AccountShare(
AccountId = accId,
UserOrGroupID = usr.Id,
AccountAccessLevel = 'Edit',
OpportunityAccessLevel = 'Read',
CaseAccessLevel = 'Read'
)
);
}
}
}
if(accshareLst.size() > 0) {
System.enqueueJob(new UserQueuableHandler(accshareLst));
}
}
}