Skip to main content
Neeraja Suribhatla (KQED) ha fatto una domanda in #Apex
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
5 risposte
  1. 30 mar 2017, 22:29

    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
0/9000