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
Nicholas MiramsNicholas Mirams 

update contact fields on closed won opportunity

Hi
I am trying to write a trigger on opportunity.  Once the opportuntiy is Won, I want to look at the product picklist field on the opportunity and update the corresponding contact roles with checking the checkbox of the product.
i.e. Opportunity is closed won, product on the opportuntiy is a picklist is Product A I want to update a contact role (contact) checkbox field called product A.
So far I ued some old code I tried when I had lots of rollup summary fields and now getting an error that the expression must be of type boolean and not string

trigger Updateproductsbought on Opportunity (after update) {

Set<Id> st_OppId = new Set<Id>();
List<Contact> con_List;

for(Opportunity opp :Trigger.new)
  {
     if(opp.StageName=='Complete - Live' )
         st_OppId.add(opp.id);
 }

if(st_OppId!=null && !st_OppId.isEmpty())
    {

         for(OpportunityContactRole iterating_oppConRole : [SELECT Role, 
                                                             OpportunityId, 
                                                             IsPrimary, 
                                                             Id, 
                                                             ContactId,
                                                             Contact.Order_Routing__c, 
                                                             Contact.Settlements__c,
                                                             Contact.Dividends__c,
                                                             Contact.Transfers__c,
                                                             Contact.Payments__c,
                                                             Contact.Re_registration__c,
                                                             Contact.Reporting_Reconciliations__c,
                                                             Opportunity.CTN_Service__c
                                                             FROM OpportunityContactRole  where OpportunityId IN : st_OppId])
                               {                                                                

                    if(iterating_oppConRole.Opportunity.CTN_Service__c = 'Order Routing')
                        {
                              iterating_oppConRole.Contact.Order_Routing__c = true;
                        }
                    
                     if(iterating_oppConRole.Opportunity.CTN_Service__c = 'Settlements')
                         {
                               iterating_oppConRole.Contact.Settlements__c = true;
                         }
                         
                      if(iterating_oppConRole.Opportunity.CTN_Service__c = 'Dividends')
                        {
                              iterating_oppConRole.Contact.Dividends__c = true;
                        }
                    
                     if(iterating_oppConRole.Opportunity.CTN_Service__c = 'Transfers')
                         {
                               iterating_oppConRole.Contact.Transfers__c = true;
                         }
                         
                      if(iterating_oppConRole.Opportunity.CTN_Service__c = 'Payments')
                        {
                              iterating_oppConRole.Contact.Payments__c = true;
                        }
                    
                     if(iterating_oppConRole.Opportunity.CTN_Service__c = 'Re-registration')
                         {
                               iterating_oppConRole.Contact.Re_registration__c = true;
                         }    
                         
                      if(iterating_oppConRole.Opportunity.CTN_Service__c = 'Reporting_Reconciliations__c')
                         {
                               iterating_oppConRole.Contact.Reporting_Reconciliations__c = true;
                         }                                      

                                       if(con_List == null)
                                           con_List = new List<Contact>();

                                          con_List.add(iterating_oppConRole.Contact);
                                }

               
           }
        
        if(con_List!=null && !con_List.isEmpty())
            update con_List;
}

As I am new to apex any help would be appreciated
Thanks
 
Hemant_SoniHemant_Soni
Hi Nicholas,
As much i understand your above code you putting wrong condition in if condtion.
Your Code  ---
 if(iterating_oppConRole.Opportunity.CTN_Service__c = 'Order Routing')
                        {
                              iterating_oppConRole.Contact.Order_Routing__c = true;
                        }

Updated Code -- 

 if(iterating_oppConRole.Opportunity.CTN_Service__c == 'Order Routing')
                        {
                              iterating_oppConRole.Contact.Order_Routing__c = true;
                        }
Please update these kind of error from your code and i think then you will not get any issue.
if still you will get any issue then let me know.
Thanks
Nicholas MiramsNicholas Mirams
Genius! thanking you!

I have written a test class for this bad boy and got 77%, which I guess is just about enough but still annoting me.  Here it is, seems to be missing out the line 'iterating_oppConRole.Contact........ = true;' unless you have any better ideas; -

@istest
Public Class testUpdateproductsbought
 {
    static testMethod void ValidateUpdateproductsbought()
    {
    
    Account a = new Account();
    a.Name ='test';
    a.Industry = 'Government';
    a.BillingCountry = 'United Kingdom';
    a.Client_Type__c = 'Fund Manager';
    a.Status__c = 'Prospecting';
    a.Primary_Regions__c = 'United Kingdom';
    a.Description = 'This is a test account';
    a.website = 'www.google.co.uk';
    a.Ownerid = '005b0000005cOGeAAM';
    insert a;
    
    Contact c = new Contact();
    c.FirstName = 'Frey';
    c.LastName = 'Bentos';
    c.Accountid = a.id; 
    c.Marketing_Prospect_Level__c = 'Customer';
    c.Level__c = 'Level 1';
    c.LeadSource = 'Conference';
    c.Key_Contact__c = TRUE;
    c.Title = 'Funds Manager';                                                        
    c.Description = 'This is a test contact';
    c.Ownerid = '005b0000005cOGeAAM';
    insert c;
    
    Contact ci = new Contact();
    ci.FirstName = 'Gareth';
    ci.LastName  = 'Bale';
    ci.AccountId = a.id;
    ci.Marketing_Prospect_Level__c = 'Customer';
    ci.Level__c = 'Level 3'; 
    ci.LeadSource = 'Conference';
    ci.Key_Contact__c = TRUE;
    ci.Title = 'Funds Manager';
    ci.Description = 'This is a test contact';
    ci.Ownerid = '005b0000005cOGeAAM';
    insert ci;
      
    Opportunity o = new Opportunity();
    o.AccountId = a.Id;
    o.StageName = 'In Pipeline';
    o.Name = 'Test';
    o.CloseDate = Date.TODAY() +2;
    o.Probability = 100;
    o.Description = 'Testing Opportunity';
    o.Type = '1 - Existing Client, New Flow';
    o.CTN_Service__c = 'Order Routing';
    o.Amount = 1000;
    o.Monthly_Flow_Volume__c = 2;
    o.Client_Role__c = 'Initiator';
    o.Primary_Contact__c = c.id;
    o.Ownerid = '005b0000005cOGeAAM';      
    insert o;
            
    OpportunityContactRole cr = new OpportunityContactRole();
    cr.Opportunityid = o.id;
    cr.Contactid = c.id;
    cr.Role = 'Administrator';
    cr.IsPrimary = true;
    insert cr;
     
    OpportunityContactRole ocr1 = new OpportunityContactRole();
    ocr1.OpportunityId = o.Id;
    ocr1.ContactId = ci.Id;
    ocr1.IsPrimary = FALSE;
    ocr1.Role = 'Decision Maker';
    insert ocr1;

    o.StageName = 'Complete - Live';
    update o;
    update cr;
    update ocr1;
    update c;
    update ci;
    



}
}
Nicholas MiramsNicholas Mirams
Any ideas?
The developer console says its not coverign the second product in the list and the rest after that