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
JaredPHJaredPH 

Apex trigger passing text to picklist please help beginner

Brand new brand new with apex triggers here. I am wanting to create a trigger with lead convert that creates a new contact role when a lead is converted using the newly created contact as the primary contact and making the role "other". I've modified some code provided by Jeff Douglas. I think it will work, but the simplest part--making the role to "other" is giving me trouble. Realized I can't just put quotes around it. Can anyone help with the second to last line of code??

 

Also, if there is anything that makes it look like this won't work, could you give me a hand and let me know?

 

trigger LeadConvert on Lead (after update) {
 
  // no bulk processing; will only run from the UI
  if (Trigger.new.size() == 1) {
 
    if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true) {
 
      // if a new opportunity was created
      if (Trigger.new[0].ConvertedOpportunityId != null && Trigger.new[0].ConvertedContactId != null) {
            Opportunity opp = [Select o.Id from Opportunity o Where o.Id = :Trigger.new[0].ConvertedOpportunityId];
            Contact c = [Select c.Id From Contact c Where c.Id = :Trigger.new[0].ConvertedContactId];           
        
            // add an opportunity contact role
            OpportunityContactRole ocr = new OpportunityContactRole();
            ocr.OpportunityId = opp.Id;
            ocr.ContactId = c.Id;
            ocr.IsPrimary = True;
//This next line doesn't work. It's a picklist value, what syntax do I use?
//ocr.Role = "Other"
            insert ocr;
         
      }         
 
    }
 
  }     
 
}



sharrissharris

Try using single quotes and a semi-colon.

 

ocr.Role = 'Other';

Does this work?