TransWikia.com

Opportunity is not updated on contact detail page

Salesforce Asked by Shalini goyal on October 3, 2021

I have a custom lookup on opportunity (Contact_Lookup__c) and a custom Field in contact(Total_no_of_Opportunities__c) and when opp is created, updated or delete the count will updated(Total_no_of_Opportunities__c) in that field but opportunity is not shown in the contact detail page and when I create opp from contact detail page then the value of custom lookup (parent contact) will not populated automatically.

trigger

RollUp_Opportunity_at_Contact on Opportunity (after insert, after update, after undelete, after delete)
{
    if(Trigger.isAfter)
    {
    List<Opportunity> oppList = new List<Opportunity>();
        Set<Id> contactIdsSet = new Set<Id>();
        if(Trigger.isDelete)
        {
  oppList = Trigger.Old;
        }
        else
        {
            oppList = Trigger.New;
        }
        for(Opportunity  opp : oppList)
        {
            if(opp.Contact_Lookup__c != null)
                {
                    contactIdsSet.add(opp.Contact_Lookup__c);
                }
            if(Trigger.isUpdate)
            {
                Opportunity oldOpportunity = (Opportunity)Trigger.oldMap.get(opp.Id);
                if(oldOpportunity.Contact_Lookup__c != opp.Contact_Lookup__c)
                {
                    contactIdsSet.add(oldOpportunity.Contact_Lookup__c);
                }
            }
        }
   RollUp_Opportunity_at_Contact_Helper.OpportunityCount(contactIdsSet);
    }
}

Helper Class

public class RollUp_Opportunity_at_Contact_Helper
{
public static void OpportunityCount(set<id> abc)
    {
        List<Contact> conList = new List<Contact>();
        List<AggregateResult> agrResult = [Select Contact_Lookup__c, Count(Id) countOpp from Opportunity where Contact_Lookup__c IN : abc Group By Contact_Lookup__c];
        Integer size = agrResult.size();
        for ( Integer i =0; i < size ;  i ++ )  
        {
            AggregateResult agr = agrResult.get(i);
            Id conid = (Id)agr.get('Contact_Lookup__c');
            Decimal count = (Decimal)agr.get('countOpp');
            Contact acc = new Contact(Id = conid, Total_no_of_Opportunities__c = count);
            conList.add(acc);
            if ( abc.contains( conid ))
            {
                abc.remove(conid);
            }
        }
        for ( Id accId : abc )
        {
            Contact acc = new Contact(Id = accId, Total_no_of_Opportunities__c = 0 );
            conList.add(acc);
        }
        update conList;
    }
}

One Answer

opportunity is not shown in the contact detail page and when I create opp from contact detail page then the value of custom lookup (parent contact) will not populated automatically.

This doesn't appear to me to be a code-related problem. Rather, you have the wrong Related List on your Contact Page Layout. Presumably, you have the Opportunity Related List on your Contact layout that utilizes the built-in Opportunity.ContactId relationship. You need to edit your Page Layout to show the Related List that is defined by your custom relationship field, Contact_Lookup__c.

Here's an example from an NPSP org where you can see multiple Related Lists for Opportunity on Contact, due to multiple relationships between the two objects:

Page Layout screenshot of Related Lists view

Correct answer by David Reed on October 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP