betelgeuze
betelgeuze

Reputation: 1

Apex Trigger to Update Lookup Field (Contact)

Need some advise on how to populate a lookup field (contact) via apex trigger

  1. I've created a lookup field called Contact__c on Idea object.
  2. I would like to populate the Contact__c with the createdby User if it was originated from the web (CreatedBy.Contact.Account.Name == "Web Ideas") and leave it empty for internal idea creation.
  3. I have read up and created the following trigger and was able to save and run. However, upon saving the idea record, i am getting an error : UpdateContactonComplaints: data changed by trigger for field Contact: id value of incorrect type: 005N0000000l9iMIAQ

trigger UpdateContactonComplaints on Idea (before insert, before Update) {

list<id> oid = new list<id>();
for(Idea o: trigger.new){                   
    oid.add(o.id);
        }
map<id, Idea> ExtendU = new map<id, Idea>(
    [select CreatedbyID from Idea where id in: oid]);

for(Idea o: trigger.new){
    o.Contact__c = ExtendU.get(o.id).CreatedbyID;
}

}

Upvotes: 0

Views: 2792

Answers (1)

Vinoth Vijayabaskar
Vinoth Vijayabaskar

Reputation: 101

In the Trigger, the user id(id of the User who created the idea) is assigned to Contact custom lookup field).

So, it throws an error, data changed by trigger for field Contact: id value of incorrect type:

Upvotes: 0

Related Questions