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
SFDCDevQASFDCDevQA 

Updating Contacts after Opportunity Update/Insert via Contact Roles

I'm attempting to update Contacts that are associated with an opportunity via contact roles when the opportunity Demo Status (custom field) is NOT equal to Null.  (Its a picklist that starts out blank).  I've written this code so far but I'm getting stuck as to how to make the transition from Contact Role to updating the contact.  It is giving me an error of Compile Error: Illegal assignment from SOBJECT:OpportunityContactRole to SOBJECT:Contact at line 36 column 13

 

Any help would be greatly appreciated.

 

Thanks,

Amanda

 

trigger OpportunityDemoContactUpdate on Opportunity (after insert, after update) {

    // map tracks records on OpportunityID

    Map<String, Opportunity> OpportunityMap = new Map<String, Opportunity>();

    for( Opportunity record : Trigger.new )
    {
        if( record.Demo_Status__c != null)

        {
              OpportunityMap.put( record.ID, record );            
        }
        
    }

    if( OpportunityMap.keySet().size() > 0 ) // if there are no eligible Quote records, end
    {
 
 //map to keep track of the opportunity contact roles
    map<Id, OpportunityContactRole> oppycontactroles = new map<Id, OpportunityContactRole>();

    //select OpportunityContactRoles for the opportunities with contact role required

    List<OpportunityContactRole> ContactRoles = [select OpportunityID, ContactID from OpportunityContactRole where OpportunityID in :OpportunityMap.keySet() ];


    for (OpportunityContactRole ocr : ContactRoles) {
        //puts the contact roles in the map with the Opportunity ID as the key
        oppycontactroles.put(ocr.ContactId,ocr);
    }

        // update Contact Prior Demo field from the matching record
        for( Contact contact : ContactRoles)
        {
            Contact record = oppycontactroles.get( Contact.id ); // grab the correct record from the map
            Contact.Prior_Demo__c = 1;
        }

        // Save Prior Demo value to the database
        Database.SaveResult[] results = Database.update( ContactRoles );
        for( Database.SaveResult result : results )
if( !result.isSuccess() ) System.debug( '<< '
+ result.getErrors().size() + ' update error(s) on Contacts: ' + result.getErrors()[0].getMessage() );
    }   

}

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

You are querying ContactRoles in this list:

 

 List<OpportunityContactRole> ContactRoles = [select OpportunityID, ContactID from OpportunityContactRole where OpportunityID in :OpportunityMap.keySet() ];

 

And then when you are iterating through them, you're iterating as a Contact (that is why illegal assignment error).

Here :

 

   for( Contact contact : ContactRoles)
        {
            Contact record = oppycontactroles.get( Contact.id ); // grab the correct record from the map
            Contact.Prior_Demo__c = 1;
        }

 

======== Please try using the below code and let me know ===

 

trigger OpportunityDemoContactUpdate on Opportunity (after insert, after update) {

    // map tracks records on OpportunityID

    Map<String, Opportunity> OpportunityMap = new Map<String, Opportunity>();

    for( Opportunity record : Trigger.new )
    {
        if( record.Demo_Status__c != null)

        {
              OpportunityMap.put( record.ID, record );            
        }
        
    }

    if( OpportunityMap.keySet().size() > 0 ) // if there are no eligible Quote records, end
    {
 
 //map to keep track of the opportunity contact roles
    map<Id, OpportunityContactRole> oppycontactroles = new map<Id, OpportunityContactRole>();

    List<Contact> lstToUpdate = new List<Contact>();

    //select OpportunityContactRoles for the opportunities with contact role required

    List<OpportunityContactRole> ContactRoles = [select OpportunityID, ContactID from OpportunityContactRole where OpportunityID in :OpportunityMap.keySet() ];


    for (OpportunityContactRole ocr : ContactRoles) {
        //puts the contact roles in the map with the Opportunity ID as the key
        oppycontactroles.put(ocr.ContactId,ocr);
    }

        // update Contact Prior Demo field from the matching record
        for( Contact contact : [Select Prior_Demo__c, Id From Contact Where ID IN : oppcontactroles.keySet()])
        {
            contact.Prior_Demo__c = 1;

            lstToUpdate.add(contact);
        }

        if(lstToUpdate.size() > 0)

       {

             // Save Prior Demo value to the database
            Database.SaveResult[] results = Database.update( lstToUpdate);
            for( Database.SaveResult result : results )
             if( !result.isSuccess() ) System.debug( '<< '
             + result.getErrors().size() + ' update error(s) on Contacts: ' + result.getErrors()[0].getMessage() );
      }   
}

 

All Answers

vishal@forcevishal@force

You are querying ContactRoles in this list:

 

 List<OpportunityContactRole> ContactRoles = [select OpportunityID, ContactID from OpportunityContactRole where OpportunityID in :OpportunityMap.keySet() ];

 

And then when you are iterating through them, you're iterating as a Contact (that is why illegal assignment error).

Here :

 

   for( Contact contact : ContactRoles)
        {
            Contact record = oppycontactroles.get( Contact.id ); // grab the correct record from the map
            Contact.Prior_Demo__c = 1;
        }

 

======== Please try using the below code and let me know ===

 

trigger OpportunityDemoContactUpdate on Opportunity (after insert, after update) {

    // map tracks records on OpportunityID

    Map<String, Opportunity> OpportunityMap = new Map<String, Opportunity>();

    for( Opportunity record : Trigger.new )
    {
        if( record.Demo_Status__c != null)

        {
              OpportunityMap.put( record.ID, record );            
        }
        
    }

    if( OpportunityMap.keySet().size() > 0 ) // if there are no eligible Quote records, end
    {
 
 //map to keep track of the opportunity contact roles
    map<Id, OpportunityContactRole> oppycontactroles = new map<Id, OpportunityContactRole>();

    List<Contact> lstToUpdate = new List<Contact>();

    //select OpportunityContactRoles for the opportunities with contact role required

    List<OpportunityContactRole> ContactRoles = [select OpportunityID, ContactID from OpportunityContactRole where OpportunityID in :OpportunityMap.keySet() ];


    for (OpportunityContactRole ocr : ContactRoles) {
        //puts the contact roles in the map with the Opportunity ID as the key
        oppycontactroles.put(ocr.ContactId,ocr);
    }

        // update Contact Prior Demo field from the matching record
        for( Contact contact : [Select Prior_Demo__c, Id From Contact Where ID IN : oppcontactroles.keySet()])
        {
            contact.Prior_Demo__c = 1;

            lstToUpdate.add(contact);
        }

        if(lstToUpdate.size() > 0)

       {

             // Save Prior Demo value to the database
            Database.SaveResult[] results = Database.update( lstToUpdate);
            for( Database.SaveResult result : results )
             if( !result.isSuccess() ) System.debug( '<< '
             + result.getErrors().size() + ' update error(s) on Contacts: ' + result.getErrors()[0].getMessage() );
      }   
}

 

This was selected as the best answer
SFDCDevQASFDCDevQA

That's what it was.  I figured it was something I was missing to link the contact role to the contact...I just couldn't figure out the wording properly.  I had tried mapping the contacts in another rendition but just got more errors.  Your wording worked perfectly. 

Thanks so much,

Amanda

SFDCDevQASFDCDevQA

I'm having issues with the deployment of my trigger in production.  It's throwing an error on another trigger's test class.  This is the test class that it is having issues with and the error I am receiving is "System.AssertException: Assertion Failed", Failure Stack Trace: "Class.AccountTimeZoneUpdate.testTrigger: line 42, column 1"  That line is the second assert in the class for A1 time zone.  I don't understand why this trigger would could that class code to fail.  I even added code in my test class for the new trigger to make sure the Account had a billing and shipping country.  If you could help that would be so wonderful.

 

Thanks,

Amanda

 

@IsTest
private class AccountTimeZoneUpdate
{
    private static TestMethod void testTrigger(){
    test.startTest();        
        //Step 1 : Data Insertion
       Country__c c=new Country__c (Name='Peru', Timezone__c='GMT-05:00');
           insert c;
        Account a=new Account(Name='Test Account', BillingCountry='Peru');
           insert a;  
        Account a1=new Account(Name='Test Account1', BillingCountry='United States', BillingState='NC');
           insert a1;
        Account a2=new Account(Name='Test Account2', BillingCountry='United States', BillingState='TN');
           insert a2;
        Account a3=new Account(Name='Test Account3', BillingCountry='United States', BillingState='UT');
           insert a3;
        Account a4=new Account(Name='Test Account4', BillingCountry='United States', BillingState='CA');
           insert a4;
        Account a5=new Account(Name='Test Account5', BillingCountry='United States', BillingState='AZ');
           insert a5;
        Account a6=new Account(Name='Test Account6', BillingCountry='United States', BillingState='AK');
           insert a6;
        Account a7=new Account(Name='Test Account7', BillingCountry='United States', BillingState='HI');
           insert a7;
       
    
        //reload objects to make sure values are loaded
        c = [select id, name, Timezone__c from country__c where id=:c.id];
        a= [select id, billingcountry, Timezone__c from Account where id=:a.id];
        a1= [select id, billingcountry, Timezone__c from Account where id=:a1.id];
        a2= [select id, billingcountry, Timezone__c from Account where id=:a2.id];
        a3= [select id, billingcountry, Timezone__c from Account where id=:a3.id];
        a4= [select id, billingcountry, Timezone__c from Account where id=:a4.id];
        a5= [select id, billingcountry, Timezone__c from Account where id=:a5.id];
        a6= [select id, billingcountry, Timezone__c from Account where id=:a6.id];
        a7= [select id, billingcountry, Timezone__c from Account where id=:a7.id];
        
               
       
        //assert your results using system.assert and system.asserEquals
        system.assert(a.Timezone__c == c.Timezone__c);            
        system.assert(a1.Timezone__c == 'Eastern Time GMT-5');
        system.assert(a2.Timezone__c == 'Central Time GMT-6');
        system.assert(a3.Timezone__c == 'Mountain Time GMT-7');
        system.assert(a4.Timezone__c == 'Pacific Time GMT-8');
        system.assert(a5.Timezone__c == 'Arizona GMT-7');
        system.assert(a6.Timezone__c == 'Alaska GMT-9');
        system.assert(a7.Timezone__c == 'Hawaii GMT-10');
        
        
     test.stopTest();
    }
}
SFDCDevQASFDCDevQA

Nevermind...it's that older class/trigger that suddenly is getting an error when running the test.  Very odd.  I'll start a seperate thread.

 

Thanks,

Amanda