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
rameshvpsgrameshvpsg 

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

jd123jd123

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.

Andrew WilkinsonAndrew Wilkinson

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;
}

 

rameshvpsgrameshvpsg

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.

rameshvpsgrameshvpsg

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();

jd123jd123

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);
rameshvpsgrameshvpsg

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

 

 

jd123jd123

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

 

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

rameshvpsgrameshvpsg

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

jd123jd123

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;
}

rameshvpsgrameshvpsg

Hi Mahi,

Still i get the same error. Error: Compile Error: Variable does not exist: pidm__c at line 16 column 9

jd123jd123

ok, commnet line no 16 and save it if it is giving the same error in line 14 then you can carete one more field and use that field instead of pidm__c.try and let me know

rameshvpsgrameshvpsg

Ok. Now it says Error: Compile Error: Loop variable must be an SObject or list of Contact at line 14 column 17

jd123jd123

ok, Create a new variable like pidm1__c use that varaible instead of pidm__c

rameshvpsgrameshvpsg

it gives the same error for new field also.