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
Sai ManojSai Manoj 

Need Simple trigger to update case fields

Hi Experts,

I'm new to salesforce coding. I need a trigger for updating case fields based on account number.
For more information please find below image.

User-added image

Regards,
Manu
Niraj Kumar 9Niraj Kumar 9
Hi Manu,

This trigger will help you.
 
trigger OnCustobjCaseFields on Customobj__c (after insert, after update)
{
    map<string, Customobj__c> ObjMap = new map<string, Customobj__c>();
    
    for(Customobj__c obj: Trigger.new)
    {
        if (obj.Shared_Field__c != Null)
        {
            ObjMap.put(obj.Shared_Field__c, obj);
        }
    }
    
    List<Case> cases = [SELECT Id, Shared_Field__c, AccountId, Expected_Event_Types__c 
    					FROM Case WHERE Shared_Field__c IN :ObjMap.KeySet()];
    List<Case> caseUpdateList = new List<Case>();
    
    for(Case c: cases)
    {
    	Customobj__c obj = ObjMap.get(c.Shared_Field__c);
        c.AccountId = obj.Account__c;
        c.App__c = obj.Id;
        c.Expected_Event_Types__c = obj.Event_Types__c;
        caseUpdateList.add(c);
    }
    
    if(caseUpdateList.size() > 0)
    {
        update caseUpdateList;
    }
}

Regards,
niraj Kumar.
Dhanya NDhanya N
Hi Sai Manoj,

Refer below code:

Trigger:
trigger caseTrigger on Case (before insert) {

    caseHandler objHandler = new caseHandler();
    
    if(trigger.isInsert && trigger.isBefore)
    objHandler.OnBeforeInsert(trigger.new);

}

Controller:
public class caseHandler {

    public void OnBeforeInsert(list<Case> lstCase) {
        
		set<String> setAccountNumber= new set<String>();
		map<String, Account> mapAccount = new map<String, Account>();
		
		for(Case objCase : lstCase) {
			
			if(objCase.AccountNumber__c != null)
			setAccountNumber.add(objCase.AccountNumber__c);
		}
		
		for(Account objAccount : [Select Id, Name, AccountNumber, VRP_Account_Number__c From Account Where AccountNumber IN: setAccountNumber]) {
			
			mapAccount.put(objAccount.AccountNumber , objAccount);
		}
		
		for(Case objCase : lstCase) {
			
			if(mapAccount.containsKey(objCase.AccountNumber__c)) {
				
				objCase.AccountId = mapAccount.get(objCase.AccountNumber__c).Id;
				objCase.VRP_Account_Number__c = mapAccount.get(objCase.AccountNumber__c).VRP_Account_Number__c;
				System.debug('===objCase.AccountId==='+objCase.AccountId);            
			}
		}
	}
}

You can use above code for updating Account details. 
Since for one Account there will be many Contact, so not sure which Contact's name you want to display. Is there any criteria to display Contact Name?

Thanks,
Dhanya
Sai ManojSai Manoj
Hi All,
 
Thanks for your inputs.
Finally I have resolved my trigger issue with my own effort. Please find below trigger code.
 
Trigger AccountandContactUpdateTrigger on Case(before insert, before update){
 
    List<String> AccNoSet = new List<String>();
    for(Case c: trigger.new){
     AccNoSet.add(c.Account_Number__c);
    }
 
    Map<String, Account> AccMap  = new Map<String, Account>();
    for(Account acc : [SELECT Id, Parent_VRP_Number__c, VRP_Account_Number__c from Account where VRP_Account_Number__c IN :AccNoSet]){
     AccMap.put(acc.Parent_VRP_Number__c , acc);
    }
 
    for(Case c:trigger.new){
         c.Accountid = AccMap.get(c.Account_Number__c).id;
         if(c.accountid!=null){
         List<Contact> ConList = [select id, name from Contact where accountid =:c.accountid];
         if(!ConList.isempty()){
         c.Contactid = ConList[0].id;
         }
         }
    }
}
 
Regards,
Sai
Dhanya NDhanya N
Hi Sai,

You have used query inside for loop. So it will fail if you insert large amount of records from data loader.
Please check below code:

Trigger:
trigger caseTrigger on Case (before insert) {

    caseHandler objHandler = new caseHandler();
    
    if(trigger.isInsert && trigger.isBefore)
    objHandler.OnBeforeInsertORUpdate(trigger.new);

if(trigger.isUpdate && trigger.isBefore)
    objHandler.OnBeforeInsertORUpdate(trigger.new);

}


Controller:
public class caseHandler {
    public void OnBeforeInsertORUpdate(list<Case> lstCase) {
        
		set<Id> setAccountId = new set<Id>();
		set<String> setAccountNumber= new set<String>();
		map<String, Account> mapAccount = new map<String, Account>();
		
		for(Case objCase : lstCase) {
			
			if(objCase.AccountNumber__c != null)
			setAccountNumber.add(objCase.AccountNumber__c);
		}
		
		for(Account objAccount : [Select Id, Name, AccountNumber, VRP_Account_Number__c From Account Where AccountNumber IN: setAccountNumber]) {
			
			mapAccount.put(objAccount.AccountNumber , objAccount);
			setAccountId.add(objAccount.Id);
		}
		
		list<Contact> lstContact = [Select Id From Contact Where AccountId IN: setAccountId limit 1]
		
		for(Case objCase : lstCase) {
			
			if(mapAccount.containsKey(objCase.AccountNumber__c)) {
				
				objCase.AccountId = mapAccount.get(objCase.AccountNumber__c).Id;
				objCase.VRP_Account_Number__c = mapAccount.get(objCase.AccountNumber__c).VRP_Account_Number__c;
				objCase.ContactId = lstContact[0].Id;
				System.debug('===objCase.AccountId==='+objCase.AccountId);            
			}
		}
	}
}
Thanks,
Dhanya
 
Dhanya NDhanya N
Test Class for this:
@isTest
private class Test_CaseUpdate {

    static testMethod void myUnitTest() {
	
	Test.startTest();
	Account objAccount = new Account(Name = 'Test Account', VRP_Account_Number__c = 1234, Parent_VRP_Number__c = 2345);
	insert objAccount;
	
	Contact objContact = new Contact(LastName = 'Test', accountId = objAccount.Id);
	insert objContact;
	
	Case objCase = new Case(Account_Number__c = 1234);
	insert objCase;
	
	Test.stopTest();
	System.assertEquals(objAccount.Id, [Select AccountId From Case Where Id = objCase.ID].AccountId);
	System.assertEquals(objContact.Id, [Select ContactId From Case Where Id = objCase.ID].ContactId);	
	}
}

 
Dhanya NDhanya N
Please mark it as best answer if it helps you.
Salesforce seekarSalesforce seekar
hi dhanya , from the above program , what kind o field VRP_Account_Number__c is it , i hope it is a look up field to which one . can you specify it please . i am practising it in my own org . 

@thanks in adv