+ Start a Discussion
Sana123Sana123 

How can i do insert, update and delete with if and else condition for checking everything for insert,update and delete.

#trigger code
trigger AccountTrigger1 on Account (after insert ) {
    if(trigger.isAfter && trigger.isInsert){
        AccountTriggerHandler1.accountDetails(trigger.new);
    }
}


#triggerhandlercode
public without sharing class AccountTriggerHandler1 {
    public static void accountDetails(list<Account> lstAccount)
    {
        list<Contact> lstContact = new list<Contact>();
       // list<Account> lstAccount = new list<Account>();
        set<Id> setAccountIds = new set<Id>();
        for(account acc : lstAccount){
            setAccountIds.add(acc.Id);  
            }
        if(setAccountIds.size() > 0){
            for(account acc : [SELECT Id , Name ,Contacts__c From Account Where Id In : setAccountIds]){
                    List<String> listofcon = acc.Contacts__c.split('\r\n');
           
                for(String con : listofcon){
                  Contact con1 = new Contact();
                     con1.firstname = con.substringBetween('=',',');
                    con1.LastName =con.substringBetween('=',',');
                        
                con1.MobilePhone = con.substringBetween('=',',');
             con1.Email = con.substringAfter('Email=').substringBefore(',');
             con1.AccountId = acc.Id;
              lstContact.add(con1);
             
                
            }
        }
        if(lstContact.size() > 0){ 
            insert lstContact;    
        }
        
  
    }
}
}

#please help me in this i am newbie to salesforce
 
Best Answer chosen by Sana123
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please check the below code.

public static void accountDetails(list<Account> lstAccount)
    {
        list<Contact> lstContact = new list<Contact>();
        list<Account> accountList = new list<Account>();
        set<Id> setAccountIds = new set<Id>();
        for(account acc : lstAccount){
            setAccountIds.add(acc.Id);  
            }
        if(setAccountIds.size() > 0){
            for(account acc : [SELECT Id , Name ,Contacts__c From Account Where Id In : setAccountIds]){
                    List<String> listofcon = acc.Contacts__c.split('\r\n');
           
                for(String con : listofcon){
                  Contact con1 = new Contact();
                     con1.firstname = con.substringBetween('=',',');
                    con1.LastName =con.substringBetween('=',',');
                        
                con1.MobilePhone = con.substringBetween('=',',');
             con1.Email = con.substringAfter('Email=').substringBefore(',');
             con1.AccountId = acc.Id;
              lstContact.add(con1);
             
                
            }
        }
        if(lstContact.size() > 0){ 
            insert lstContact;    
        }
        
  
    }else if(setAccountIds.size() > 0){
	
	 for(Account ac: lstAccount){
	   ac.Name='Test';
	   accountList.add(ac);
	 }
	 
	 if(accountList.size() > 0){ 
            Update accountList;    
        }
	 
	}else if(setAccountIds.size() > 0){
	
	  Delete lstAccount[0];
	 
	}
}

Thank You