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
Jennifer LaingJennifer Laing 

Update Contact Status when Opportunity Created

Hello

Does anyone know how I could update a Contact Status field to the custom value of "Opportunity" when a new Opportunity is created then update the Status again when the Opportunity is closed?  Note, the Org is Professional Edition so I can't use Workflow Rules for this and I have no experience/knowledge of Triggers.

Any help would be appreciated!
Best Answer chosen by Jennifer Laing
Dushyant SonwarDushyant Sonwar
Hi Jennifer,

Currently what i understood from above :
You have a field on opportunity called Contact_Status__c . You want set value in this field when opportunity stage name is some option . I am assuming Option1 to be the option that you want to set . When Opportunity is closed , you again want to update Contact Status field .

Please correct if my understanding is correct.
trigger OpportunityTrigger on Opportunity(before insert , before update){
	if(trigger.isBefore && trigger.isInsert){
		for(Opportunity OppObj : Trigger.New ){
			if(OppObj.stageName == 'Option1'){ //Opportunitu stage name is option 1
				OppObj.Contact_Status__c = 'abc';
			}
		}
	}
	if(trigger.isBefore && trigger.isUpdate){
		for(Opportunity OppObj : Trigger.New ){
			if(trigger.oldMap.get(OppObj.id).IsClosed != OppObj.IsClosed && OppObj.IsClosed == true ){ // Opportunity is Closed
				OppObj.Contact_Status__c = 'abc';
			}
		}
	}
}
Hope this helps.
 

All Answers

Dushyant SonwarDushyant Sonwar
Hi Jennifer,

Currently what i understood from above :
You have a field on opportunity called Contact_Status__c . You want set value in this field when opportunity stage name is some option . I am assuming Option1 to be the option that you want to set . When Opportunity is closed , you again want to update Contact Status field .

Please correct if my understanding is correct.
trigger OpportunityTrigger on Opportunity(before insert , before update){
	if(trigger.isBefore && trigger.isInsert){
		for(Opportunity OppObj : Trigger.New ){
			if(OppObj.stageName == 'Option1'){ //Opportunitu stage name is option 1
				OppObj.Contact_Status__c = 'abc';
			}
		}
	}
	if(trigger.isBefore && trigger.isUpdate){
		for(Opportunity OppObj : Trigger.New ){
			if(trigger.oldMap.get(OppObj.id).IsClosed != OppObj.IsClosed && OppObj.IsClosed == true ){ // Opportunity is Closed
				OppObj.Contact_Status__c = 'abc';
			}
		}
	}
}
Hope this helps.
 
This was selected as the best answer
Jennifer LaingJennifer Laing
Hello Dushyant, thank you for your reply.  Yes, there is a custom field named Contact Status on the Contact record.  When a new Opportunity is created, I would like the Contact Status to update to "Opportunity" and when that Opportunity is closed won, I'd like the Contact Status to update to "Customer".  In your Trigger, do I just add "Opportunity" to line 05 and "Customer" to line 12?
Dushyant SonwarDushyant Sonwar
HI Jennifer ,

I am not clear as you are saying that opportunity is related to Contact . As you said above that there is a Contact Status field on Contact Record.
Did you created a custom field on Opportunity to create relationship to Contact? 

As in Salesforce standard relationship ,  opportunity is related to Account. 

Could you post the field name over here that you created for relationship of Opportunity to Contact?