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
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12 

Update a custom lookup field with associated contact

Hi Everyone,

If Opportunity Name and Account Name not equal to null,
then i want to update a custom lookup field with associated contact.


Can anyone Tell, How do we do it?

Thanks in advance.

SRKSRK
Hi
You can achive it by using trigger or a workflow depand on how you are picking up the contact

Use trigger it will give you more option as you can code and handel complex log there
let me know if you want to discuss the further
and also explane you senario  
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
Hi SRK
Thanks for your reply,

We cannot achieve this by Workflow,earlier i tried but it won't work.

I think by trigger only we achieve this.

If you write a sample trigger according to my scenario it would be more help to me.

Thanks in advance.
SRKSRK
assume that we have this trigger on  Opportunity
 Try this

trigger TestTriggerName on Oppoprtunity (before insert , before update)
{
set<id> AccountIDSet = new set<id>();
map<id,contact> AccountContactMap = new map<id,contact>();
       for(Oppoprtunity TempOppObj : Trigger.new)
       { 
               if(TempOppObj.Name != null and TempOppObj.Account != null) // at opportunity both account and opportunty field are field
                  {
                         AccountIDSet.add(TempOppObj.Account);
                   } 
       }
list<contact> TempLst = [select id,name,account from contact where AccountId in : AccountIDSet];
for(Contact TempContactObj : TempLst)
{
AccountContactMap.put(TempContactObj.Account, TempContactObj);
}
for(Oppoprtunity TempOppObj : Trigger.new)
       {
              if(TempOppObj.Name != null and TempOppObj.Account != null)
              {
                 TempOppObj.Associated_Contact_Lookupfield__c = AccountContactMap.get(TempOppObj.AccountId).id;
             }
        }
}
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
Hey Thank u SRK,

The Trigger is saved after couple of changes,
but it is not working according to my scenario.
check once this is the trigger:

trigger TestTriggerName on Opportunity (before insert , before update)
{
    set<id> AccountIDSet = new set<id>();
    map<id,contact> AccountContactMap = new map<id,contact>();
           for(Opportunity TempOppObj : Trigger.new)
           {
                   if(TempOppObj.Name != null && TempOppObj.Account != null) // at opportunity both account and opportunty field are field
                      {
                             AccountIDSet.add(TempOppObj.id);
                       }
           }
    list<contact> TempLst = [select id,name,Account.Name from contact where Id in : AccountIDSet];
    for(Contact TempContactObj : TempLst)
    {
    AccountContactMap.put(TempContactObj.id, TempContactObj);
    }
    for(Opportunity TempOppObj : Trigger.new)
           {
                  if(TempOppObj.Name != null && TempOppObj.Account != null)
                  {
                     TempOppObj.Donor_Name__c = AccountContactMap.get(TempOppObj.AccountId).id;
   

                  }
           }
}

Where Donor_Name__c is Associated_Contact_Lookupfield__c.

When i enter opportunity name and Account name in opportunity it will not display Associated contact(Associated_Contact_Lookupfield__c).

What was the problem?

Thanks in advance.
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
And one more thing SRK................
I have different Recor Types on Opportunity..
But I want to applicable this scenario for only one Recor Type(Lets Assume Feast Donor).

How do we do it?

Thanks in advance.........
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
Hi SRK
 
Current Trigger:

trigger TestTriggerName on Opportunity (before insert , before update)
{
    set<id> AccountIDSet = new set<id>();
    map<id,contact> AccountContactMap = new map<id,contact>();
    RecordType rt = [SELECT Id,Name,SobjectType FROM RecordType WHERE Name = 'Feast Donor' AND SobjectType = 'Opportunity '];

           for(Opportunity TempOppObj : Trigger.new)
           {
                   if(TempOppObj.Name != null && TempOppObj.Account != null && TempOppObj .RecordTypeId == rt.Id) // at opportunity both account and opportunty field are field
                      {
                             AccountIDSet.add(TempOppObj.id);
                             //TempOppObj.RecordType = [select Id from RecordType where Name = 'Feast Doner' and SobjectType = 'Opportunity '];
                       }
           }
    list<contact> TempLst = [select id,name,Account.Name from contact where Id in : AccountIDSet];
    for(Contact TempContactObj : TempLst)
    {
    AccountContactMap.put(TempContactObj.id, TempContactObj);
   
    }
    //Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Opportunity.getRecordTypeInfosById();
    for(Opportunity TempOppObj : Trigger.new)
           {
                  if(TempOppObj.Name != null && TempOppObj.Account != null && TempOppObj .RecordTypeId == rt.Id)
                  {
                     TempOppObj.Donor_Name__c = AccountContactMap.get(TempOppObj.AccountId).id;
                     TempOppObj.RecordType = [select Id from RecordType where Name = 'Feast Donor' and SobjectType = 'Opportunity '];

                  }
           }
}

When i enter opportunity name and account name in opportunity and when i click on save the system showing this error:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger TestTriggerName caused an unexpected exception, contact your administrator: TestTriggerName: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.TestTriggerName: line 5, column 1

Can  you tell me what was the problem?

Thanks in advance.......
SRKSRK

the error rhat you have mention according to that the error is because the query is not reurn any record
are you sure the Opportunity have a active record type named as "Feast Donor"

please check that and let me know if you still have the issues


Also what you can do is use some system.debug as i mention below in code and check that the query is returning any record or not 

list<RecordType> rt = [SELECT Id,Name,SobjectType FROM RecordType WHERE Name = 'Feast Donor' AND SobjectType = 'Opportunity'];

System.debug('#@#!@#!@#!@$!@$#23'+Rt.size());