You need to sign in to do that
Don't have an account?
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() );
}
}
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
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() );
}
}
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
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
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