Reply
Regular Contributor
rameshvpsg
Posts: 19
0

Roll up opportunity amount to contact roles

Hi, I have an requirement to roll up opportunity amount to all contacts linked through contact roles for an opportunity. Do any one have solution for this.

Thanks,

Ramesh V

Ramesh
Trusted Contributor
jd123
Posts: 282
0

Re: Roll up opportunity amount to contact roles

Hi

 

  I think you can do throght the workflow rule with FIled Update. Did you try?

  i know they are using junction object between Contact and Opp.

------------
Mahi
Regular Contributor
Andrew Wilkinson
Posts: 121
0

Re: Roll up opportunity amount to contact roles

[ Edited ]

Your requirement was a little unclear to me...but if you meant the amount of opportunities rolled up onto the contact record then this accomplishes that.

 

Here is some sample code to get you started. You will probably want to add an isPrimary Where clause in the subquery in the final for soql loop.

 

trigger oppContactRoleSize on Opportunity (after update,after insert) {
    Set<Id> oppIds = new Set<Id>();
    oppIds.addAll(Trigger.newMap.keySet());
    Set<Id> contactIds = new Set<Id>();
    
    
    //grab the contact ids from the oppconrole object
    for(OpportunityContactRole o: [SELECT ContactId FROM OpportunityContactRole WHERE OpportunityId IN :oppIds])
    contactIds.add(o.ContactId);
    
    List<Contact> consToUpdate = new List<Contact>();
    
    //query contacts and roles and set field to size...you can restrict the query by adding a where clause if needed
    for(Contact c : [SELECT Id,(SELECT id FROM OpportunityContactRoles) FROM Contact WHERE ID IN :contactIds]){
        //put the name of the field in the below line...i used a field from my org which is pidm__c
        c.pidm__c = c.OpportunityContactRoles.size();
        consToUpdate.add(c);
    }
   update consToUpdate;
}

 

Andrew Wilkinson
Certified Salesforce Developer
Check out my blog. http://andrewwilkinsonsf.blogspot.com/
Regular Contributor
rameshvpsg
Posts: 19
0

Re: Roll up opportunity amount to contact roles

Thanks Andrew. I will try this. I actually wanted to populate total amount that a contact has influenced part of contact layout. For all won opportunities, i wanted to populate the amount from opportunity object to all contacts that are mapped in contact roles.

Ramesh
Regular Contributor
rameshvpsg
Posts: 19
0

Re: Roll up opportunity amount to contact roles

Hi Andrew, Need a help. I tried using the code you provided after creating custom field "pidm__c" in contact. But while saving the trigger i get below error.

Error: Compile Error: Variable does not exist: pidm__c at line 16 column 9.

 

Line 16 is : c.pidm__c = c.OpportunityContactRoles.size();

Ramesh
Trusted Contributor
jd123
Posts: 282
0

Re: Roll up opportunity amount to contact roles

Hi Ramesh,

 

Andrew created that field.I think you didn't create that field or the name is different. See in your Contact Object.

 

 //put the name of the field in the below line..I used a field from my org which is pidm__c
        c.pidm__c = c.OpportunityContactRoles.size();
        consToUpdate.add(c);
------------
Mahi
Regular Contributor
rameshvpsg
Posts: 19
0

Re: Roll up opportunity amount to contact roles

HI Mahi, i have created the field pidm ("pidm__c") in contact object. But still it gives that error.

 

 

Ramesh
Trusted Contributor
jd123
Posts: 282
0

Re: Roll up opportunity amount to contact roles

See the field name in the Contact object (Speeling).

 

can you post your code then i can help you out. 

------------
Mahi
Regular Contributor
rameshvpsg
Posts: 19
0

Re: Roll up opportunity amount to contact roles

Mahi,

Here is the code:

 

trigger oppContactRoleSize on Opportunity (after update,after insert) {
    Set<Id> oppIds = new Set<Id>();
    oppIds.addAll(Trigger.newMap.keySet());
    Set<Id> contactIds = new Set<Id>();
   
   
    //grab the contact ids from the oppconrole object
    for(OpportunityContactRole o: [SELECT ContactId FROM OpportunityContactRole WHERE OpportunityId IN :oppIds])
    contactIds.add(o.ContactId);
   
    List<Contact> consToUpdate = new List<Contact>();
   
    //query contacts and roles and set field to size...you can restrict the query by adding a where clause if needed
    for(Contact c : [SELECT Id,(SELECT id FROM OpportunityContactRoles) FROM Contact WHERE ID IN :contactIds]){
        //put the name of the field in the below line...i used a field from my org which is pidm__c
        c.pidm__c = c.OpportunityContactRoles.size();
        consToUpdate.add(c);
    }
   update consToUpdate;
}

 

 

//and the field name in contact object is pidm__c

Ramesh
Trusted Contributor
jd123
Posts: 282
0

Re: Roll up opportunity amount to contact roles

Hi

 

Can you try the belwo code.IF it is not working please let me know.

 

trigger oppContactRoleSize on Opportunity (after update,after insert) {
    Set<Id> oppIds = new Set<Id>();
    oppIds.addAll(Trigger.newMap.keySet());
    Set<Id> contactIds = new Set<Id>();
   
   
    //grab the contact ids from the oppconrole object
    for(OpportunityContactRole o: [SELECT ContactId FROM OpportunityContactRole WHERE OpportunityId IN :oppIds])
    contactIds.add(o.ContactId);
   
    List<Contact> consToUpdate = new List<Contact>();
   
    //query contacts and roles and set field to size...you can restrict the query by adding a where clause if needed
    for(Contact c : [SELECT Id,pidm__c,(SELECT id FROM OpportunityContactRoles) FROM Contact WHERE ID IN :contactIds]){
        //put the name of the field in the below line...i used a field from my org which is pidm__c
        c.pidm__c = c.OpportunityContactRoles.size();
        consToUpdate.add(c);
    }
   update consToUpdate;
}

------------
Mahi