function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Akshay MhetreAkshay Mhetre 

How to create Case/Record on custom object with apex logic.


Hello Experts,  
  1. There are 2 objects. Lookup(Applicant) and Bank . (Lookup Relationship). 
  2. There is One checkbox is on ApplicantAreYouSure__c
  3. There is One PicklistApplicant_Type__c with values -- MaleFemale Both.                                   
Create Bank Record only when--
  1. On Applicant when AreYouSure__c=true, then it will ask for Applicant_Type__c  New? or Old ?, or both of them? 
  2. If Applicant_Type__c =both, then bank record needs to be created only for ‘Male’, and stamp the relevant applicant’s Applicant Id.
  3. If it’s checked for only one of them, then create a record for the one for which the flag is true and stamp the relevant applicant’s Applicant Id. 
  4. If AreYouSure__c=false , then don't create a record.
Best Answer chosen by Akshay Mhetre
AnkaiahAnkaiah (Salesforce Developers) 
Trigger:

trigger Bankinsert on Application__C (after insert , after update){
    if((trigger.isInsert||trigger.isUpdate) && trigger.isAfter){	  
		HandlerClass.Bankinsert(trigger.new);
	 }
    }
Apex Class:
public class HandlerClass{
   public static void Bankinsert(List<Appilcation__c> applist){
   
   List<Bank__C> Newbanklist = new List<Bank__C>();
		
        for(Application__C app : applist){
		
		if(app.AreYouSure__c==true && (app.Applicant_Type__c=='Female'||app.Applicant_Type__c=='Male'||app.Applicant_Type__c=='Both')){
		
		Bank__C Bnk = new Bank__C();
		Bnk.Name = app.Name;
		Bnk.Appilcation__c = app.id;
		Bnk.phone__c = app.phone__c;
		Newbanklist.add(Bnk);           
		}
        }
        if(Newbanklist.size()>0){
            insert Newbanklist;
        }
 
   }
}
If this helps, please mark it as best answer.

Thanks!!
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Akshay,

As per my understanding,
1.when application record created with type both  & AreYouSure__c=true then bank record needs to be created.
2.when application record created with type Male/Female  & AreYouSure__c=true then bank record needs to be created.
3. If AreYouSure__c=false , then don't create a bank record.

when user selects the AreYouSure__c=true then is there any validation rule to alert the user like "please select the Applicant_Type__c "

Please correct me if am wrong.

​​​​​​​Thanks!!




 
Akshay MhetreAkshay Mhetre

Hi Ankaiah,
When creation of Applicant record is going on (on Applicant obj), there is one checkbox AreYouSure__c ,if its selected as True, then next,User will choose Male/Female/Both from Applicant_Type__c  picklist. Once Applicant record gets created,at the same time I need to generate Bank record related to Applicant Record.

Your above understanding is correct but No need of any validation. I just need code. I am not versed with Map or List..Can you please provide me simple code for above logic.
Thanks

AnkaiahAnkaiah (Salesforce Developers) 
Hi Akshay,

Try with below code.
trigger Bankinsert on Application__C (after insert , after update){
    if((trigger.isInsert||trigger.isUpdate) && trigger.isAfter){
	  List<Bank__C> Newbanklist = new List<Bank__C>();
		
        for(Application__C app : trigger.new){
		
		if(app.AreYouSure__c==true && (app.Applicant_Type__c=='Female'||app.Applicant_Type__c=='Male'||app.Applicant_Type__c=='Both')){
		
		Bank__C Bnk = new Bank__C();
		Bnk.Name = app.Name;
		Bnk.Appilcation__c = app.id;
		Bnk.phone__c = app.phone__c;
		Newbanklist.add(Bnk);           
		}
        }
        if(Newbanklist.size()>0){
            insert Newbanklist;
        }
		
	 }
    }

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
Akshay MhetreAkshay Mhetre

Hello Ankaiah

You are awsome.Thanks. 

Instead of writing Trigger Is it possible to write trigger logic in Apex class.

AnkaiahAnkaiah (Salesforce Developers) 
Trigger:

trigger Bankinsert on Application__C (after insert , after update){
    if((trigger.isInsert||trigger.isUpdate) && trigger.isAfter){	  
		HandlerClass.Bankinsert(trigger.new);
	 }
    }
Apex Class:
public class HandlerClass{
   public static void Bankinsert(List<Appilcation__c> applist){
   
   List<Bank__C> Newbanklist = new List<Bank__C>();
		
        for(Application__C app : applist){
		
		if(app.AreYouSure__c==true && (app.Applicant_Type__c=='Female'||app.Applicant_Type__c=='Male'||app.Applicant_Type__c=='Both')){
		
		Bank__C Bnk = new Bank__C();
		Bnk.Name = app.Name;
		Bnk.Appilcation__c = app.id;
		Bnk.phone__c = app.phone__c;
		Newbanklist.add(Bnk);           
		}
        }
        if(Newbanklist.size()>0){
            insert Newbanklist;
        }
 
   }
}
If this helps, please mark it as best answer.

Thanks!!
 
This was selected as the best answer