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
Manish DeshmukhManish Deshmukh 

If contact is associated to an opportunity and opportunity stage = "PreQual" THEN update Contact Picklist field to "PreQual Op Nurture"

How to write Trigger for this automation Logic-:
IF contact is associated to an opportunity and opportunity stage = "PreQual" THEN update Contact Picklist field to "PreQual Op Nurture"

IF contact associated to an opportunity and opportunity state = "Qualified", "Discovery", "Solution Overview", "Scoping", "Proposal", "Pending Approval","Won", "Closed Lost", or "Cancelled" THEN clear out Contact Picklist field to <None>

Please help, thanks!
PawanKumarPawanKumar
Please try below trigger on the opportunity.

-----------------
trigger UpdateContact on Opportunity (after update) {

    List<String> oppIdList = new List<String>();
    for (Opportunity opp : Trigger.new) {
        if (opp.StageName == 'PreQual') {
            oppIdList.add(opp.Id);
        } //end if
    } // End of For

   // query contact: assuming you have opportunity lookup at contact with field Opportunity__c
   List<Contact> contactList = [Select Name,Email from Contact where Opportunity__c IN :oppIdList];
   Set<Id> setContactList = new Set<Id>();
   if(contactList!=null && !contactList.isEmpty()){
    for(contact eachContact : contactList){
        eachContact.yourPickListField = 'PreQual Op Nurture';
        setContactList.add(eachContact.Id);
    }
       update contactList;
   }

   // update rest of contact as null
   List<Contact> allContactList = [Select Name,Email from Contact where Opportunity__c IN : trigger.newMap.keySet()];
   if(allContactList!=null && !allContactList.isEmpty()){
    for(contact eachContact : allContactList){
        if(setContactList!=null && !setContactList.contains(eachContact.Id)){
            eachContact.yourPickListField = null;
        }
    }
       update allContactList;
   }
   
}
--------------

Please mark it best if it helps you. Thanks.

Regards,
Pawan Kumar


 
Manish DeshmukhManish Deshmukh
Hi pawan
There, we are taking handle of all contacts from contacts role in opportunity. 
I have Implement Trigger but it will throw error when Bulkify Opportunity we update .
Error-: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.MD98.updateContact: line 42, column 

Below is my trigger
trigger updateContact on Opportunity (after insert,after update) {
       
    if(Trigger.isInsert || Trigger.isUpdate ){
	 
		Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>(); 
        List<Id> contactIdList = new List<Id>();
		List<Contact> lstcontact = new List<Contact>();
        Map<Id, List<Contact>> opptyContactMap = new Map<Id, List<Contact>>();
		for(Opportunity opp : Trigger.new){
			oppMap.put(opp.Id, opp);
		}
		//Contact[] contactList = [ SELECT Id, Nurture_Path__c FROM Contact WHERE Id IN ( SELECT ContactId FROM OpportunityContactRole WHERE OpportunityId IN: oppMap.keySet()) ];
        
        OpportunityContactRole[] opptyConRole = [SELECT ContactId, OpportunityId FROM OpportunityContactRole
											 WHERE OpportunityId IN: oppMap.keySet()];
        
        Map<Id, List<ID>> opptyContIdMap = new Map<Id, List<ID>>();
        
        for(OpportunityContactRole ocr : opptyConRole){
            
            if(opptyContIdMap.containsKey(ocr.OpportunityId))
                contactIdList = opptyContIdMap.get(ocr.OpportunityId);
            else
                contactIdList = new List<Id>();
            
            contactIdList.add(ocr.ContactId);
            opptyContIdMap.put(ocr.OpportunityId, contactIdList);
            System.debug('opptyContIdMap-'+opptyContIdMap);
        }
        
		for(Opportunity oppty : oppMap.values() ) {
            if(opptyContIdMap != NULL && !opptyContIdMap.isEmpty() ){
				if(oppty.stageName.contains('Prospecting')){
    	            for(Id idr : opptyContIdMap.get(oppty.Id)){
        	            Contact con = new contact(id = idr);
            	        con.Nurture_Path__c = 'PreQual Op Nurture';
						lstcontact.add(con);
                }
				
			}
			else{
                for(Id idr : opptyContIdMap.get(oppty.Id)){
                    Contact con = new contact(id = idr);
                    con.Nurture_Path__c = '';
					lstcontact.add(con);
                }
					
				}
            }
        }
		update lstcontact;
	}  
}