+ Start a Discussion
NeerajaNeeraja 

Help with the trigger

I need to write a trigger for the following scenario using Map. 
I should be able to retrieve Salesforce Id from a parent object whenever the value in child object matches with the Parent object.
The relationship between is the objects is lookup. I really appreicate if someone helps me with this trigger.

Thanks,
Neeraja
Charisse de BelenCharisse de Belen
Hi Neeraja,

What are the parent and child objects you are using? Also, what is the field that you are matching between the two objects?
Amit Chaudhary 8Amit Chaudhary 8
Please check below sample code
Set<Id> SetAccountId = new Set<Id>();
 for(Contact cont: Trigger.new) 
 {
  if(cont.AccountId != null) 
  {
            SetAccountId.add(cont.AccountId);
  }
 }
    
 if(SetAccountId.size() > 0 ) 
 {
        Map<Id, Account> mapAccount = new Map<Id, Account>([Select Id, Name, Type from Account where Id IN :SetAccountId ]);
  for(Contact cont: Trigger.new) 
  {
             if(cont.AccountId != null && mapAccount.containsKey(cont.AccountId) ) 
     {
                cont.Type__c = mapAccount.get(cont.AccountId).Type;
     }
  }
 }

Let us know if this will help you
NeerajaNeeraja
@Charisse de Belen Parent object fields and child object fields. I need to lookup for the parent value and if there is match then I need to get the
Salesforce ID of the parent object.
@Amit Chaudhary 8 Thank you very much. I will try your solution and let you know.


 User-added image

User-added image
 
NeerajaNeeraja
@Amit Chaudhary 8 Thank you. I didnt get any errors with your code but I need to store Salesforce ID in a text box on Child object when there is
value matches with child object. I am not a developer thats the reason its taking time to process the information. 



User-added image








 
Charisse de BelenCharisse de Belen
Hi Neeraja,

To accomplish this, you do not need to write a trigger. You can instead create a text formula field on Child Object to get the ID of the Parent Object.