0

I've been trying to create a trigger in Opportunities that updates the value from an opportunity to a contact. The id for the two is the same "Admit_Type__c", but the Opportunity version is a picklist and the Contact version is a Text (with 3 characters available).

The current code I have based on my own searching around is as follows:

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

for(Opportunity opp: trigger.new)
{
    Contact admitType = new Contact(Id = opp.Admit_Type__c);

    contUpdate.add(admitType);
}

When I try and update the opportunity to see if it will reflect in the contact, I get this error:

System.StringException: Invalid id: TRN: Trigger.OpportunityUpdateAdmitTerm: line 17, column 1

I'm pretty sure this has to do with the fact that the Opportunity Admit_Type__c is a picklist and the Contact Admit_Type__c is not, but I do not know the solution.

Any help would be appreciated.

4
  • please put your whole code with this question so that we can see context of current code.
    – Himanshu
    Feb 24, 2016 at 20:15
  • You can't assign a SF record Id Feb 24, 2016 at 20:16
  • opp.Admit_Type__c should contain contact record id. please check this first.
    – Himanshu
    Feb 24, 2016 at 20:17
  • The logic above doesn't indicate how you know which Contact to update. As the trigger is on Opportunity, the contact to update is either (1) some lookup field to Contact on Opportunity, (2) one of the Contacts associated via the OpportunityContactRole junction object [and this will only work on update triggers], or you need to query for some Contact based on some criteria. More info needed please
    – cropredy
    Feb 24, 2016 at 23:47

1 Answer 1

3

You can not assign an id to a Salesforce record, these are done by the system when they are inserted.

Perhaps you are intending to do something else here.

Contact admitType = new Contact(Id = opp.Admit_Type__c);

Are you trying to do this?

Contact c = new Contact(
    //required fields first,
    admitType__c = opp.Admit_Type__c
);
4
  • I guess I should be more specific about the exact nature of what I want the code to do. There is a field in Opportunity called Admit_Type__c that I want to update in the Contact version of the Opportunity. For example: There is an Opportunity with the name John Smith who also has a Contact with the same name. If I update the field Admit_Type__c in the Opportunity version of John Smith, I want it to update in the Contact version. Does this help clarify? Feb 25, 2016 at 18:09
  • @DanielRivera Are you relating the Opportunity to that Contact version of itself in anyway? Such as a custom lookup field on the Opportunity? Feb 25, 2016 at 21:51
  • Do you mean what makes sure that if there are multiple John Smiths that they don't all get updated? If so, every Opportunity/Contact has a unique ID number assigned when created with the field name PantherID__c. If this doesn't answer your question, could you elaborate possibly because then I'm not sure I understand the question. Sorry. Feb 26, 2016 at 17:04
  • @DanielRivera Ok, before I adjust my answer for you I just want to be clear here. Each Opportunity and Contact has a field called PantherID__c on them that are the same unique number per record combination (Contact/Opportunity)? For my previous comment, I was curious why not just add a custom Lookup field on the Opportunity that you assign to the appropriate Contact when the Opportunity is created. Feb 26, 2016 at 17:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .