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
KirbySFKirbySF 

Trigger to update a custom lookup field from a custom picklist

I'm new to developing in general and am trying to create a trigger that updates the custom lookup field Market_lookup__c with the value selected in the field Market_picklist__c. Both fields reside in the standard Opportunity object, and the Market_lookup__c field points to the standard Account object. What I came up with is:

 

1
2
3
4
trigger OpptyMktTrigger on Opportunity (before insert, before update) {
    for (Opportunity a: Trigger.new)
            a.Market_lookup__c = a.Market_picklist__c;
}

 

But running this trigger returns the error: OpptyMktTrigger: execution of BeforeUpdate caused by: System.StringException: Invalid id: Market 1: Trigger.OpptyMktTrigger: line 3, column 1

 

I'm sure it's a simple fix. Can anyone help?

Abhijeet Purohit01Abhijeet Purohit01

HI. Try this code. Copy Paste it and try inserting a new record or change the picklist value of an existing record.

 

trigger OpptyMktTrigger on Opportunity (before insert, before update) {
    List <Opportunity> opListInsert = new List<Opportunity>();
    List <Opportunity> opListUpdate = new List<Opportunity>();
    if(trigger.isInsert){
        for(Opportunity op:trigger.New){
            if(op.Market_Picklist__c != Null){
                op.Market_LookUp__c = op.Market_Picklist__c;
                opListInsert.add(op);
            }
        }
    }
    else if(trigger.isUpdate){
        for(Opportunity op:trigger.New){
            if(op.Market_Picklist__c != Null && op.Market_Picklist__c !=trigger.oldMap.get(op.id).Market_Picklist__c){
                op.Market_LookUp__c = op.Market_Picklist__c;
                opListUpdate.add(op);
            }    
        }
    }
}

KirbySFKirbySF

It gives the same error as before: Invalid ID

Abhijeet Purohit01Abhijeet Purohit01

What is the error msg? I tried it in my system. Its working and with 100% code coverage.

KirbySFKirbySF

Error when updating an existing record:

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger OpptyMktTrigger caused an unexpected exception, contact your administrator: OpptyMktTrigger: execution of BeforeUpdate caused by: System.StringException: Invalid id: Market 1: Trigger.OpptyMktTrigger: line 17, column 1

 

The error is the same when creating a new record, except that it references line 8. I doubled checked and the market name and picklist values are exact matches.

SammyComesHereSammyComesHere

The Issue is most probably due to Mismatch between String and Id. The value coming from Trigger.New is returning a String Id and Yoy are trying to assign it to a look up which expects and Id value.

 

Hence you are trying to typecast a String to an Id which may the reason. Would Suggest to either typecast and Then try