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
Danny@ReachLocalDanny@ReachLocal 

Lookup field to auto populate

 

So we're looking to be able to auto-populate a field within the Case object (Entitlement) upon case creation with the value of a field from a custom field from the Accounts object (Entitlement__c).  Looks like from what we've been told, this can be done only via Apex code.  Can someone give any suggestions as to how it should be written.....thanks. 

mikefmikef

This use case is one of the most popular problem to solve with Apex code.

 

 

trigger CaseTrigger on Case (before insert){
  Set<Id> accIds = new Set<Id>();
  Map<Id, Account> accMap;

  for(Case c : trigger.new){
    if(c.AccountId != null){
      accIds.add(c.AccountId);
    }    

  }

  accMap = [select Id, Entitlement__c from Account where Id in : accIds];

  for(Case c : trigger.new){
    if(c.AccountId != null && accMap.containsKey(c.AccountId)){    
     c.Entitlement__c = accMap.get(c.AccountId).Entitlement__c;
    }
  }
}

 Please remember you will need to write tests for this code, and I didn't test this before posting just wrote it so you might have to tweek it.

 

crop1645crop1645

You can also do this by overriding the New button for Case as follows (note this is not org-proof and will not port to your sandbox successfully as the IDs are org-specific)

 

https://na2.salesforce.com/500/e?id of field to set={!Account.Entitlement__c}
&id of Account lookup on Case_lkid={!Account.Id} 
&retURL=/{!Account.Id}

 

Use Firebug or equivalent to locate the ids of the fields on the Case Edit page

BALA_RAMBALA_RAM

TRY this code defenitly u get the solution for Autopopulation just you have to make Looup relation with User

 

trigger populateContactfromUser on Contact (before insert , before update){
    
    Set<ID> setConIds = new Set<ID>();
    for(Contact obj : trigger.new){
        if(obj.User_Name__c!= null)
        setConIds.add(obj.User_Name__c);
    }
    
     MAP<ID , User> mapCon = new MAP<ID , User>([Select Id,Email,Phone from User where id in: setConIds]);
     for(Contact obj : trigger.new)
       {
        if(obj.User_Name__c != null)
          {
            User c = mapCon.get(obj.User_Name__c);
            obj.Email= c.Email;
            //Similarly you can assign Address fields as well, just add those field in Contact SOQL as well
          }
       
       }
}

 

 

 

BALA_RAMBALA_RAM
TRY this code defenitly u get the solution for Autopopulation just you have to make Looup relation with User

trigger populateContactfromUser on Contact (before insert , before update){

Set<ID> setConIds = new Set<ID>();
for(Contact obj : trigger.new){
if(obj.User_Name__c!= null)
setConIds.add(obj.User_Name__c);
}

MAP<ID , User> mapCon = new MAP<ID , User>([Select Id,Email,Phone from User where id in: setConIds]);
for(Contact obj : trigger.new)
{
if(obj.User_Name__c != null)
{
User c = mapCon.get(obj.User_Name__c);
obj.Email= c.Email;
//Similarly you can assign Address fields as well, just add those field in Contact SOQL as well
}

}
}