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
abdulqabdulq 

Trigger Error data changed by trigger for field <sObject>: id value of incorrect type:

Hi All, I'm quite new to apex, I have created a trigger on two objects CONTACT & OPPORTUNITIES.

 

When I convert a LEAD to ACCOUNT, it also creates a Contact and an Opportunity, so in this process I want a Custom Field(Consultant__C) on both of these objects to be updated with a field that is there on LEAD and later comes in ACCOUNT.

 

I have also written a test class, but when I try to deploy it comes with the error given below - not sure where I'm going wrong.

 

"ConsultantUpdateTest.validateConsultantonContact System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ConsultantUpdateOnContact: data changed by trigger for field Consultant: id value of incorrect type: a0VD0000002KYPfMAO: []"

 

Trigger Code for Contact 

----------------------------------

 

trigger ConsultantUpdateOnContact on Contact (before insert) {
Account a = [select consultant__c from Account where id = :Trigger.new[0].AccountId];
Trigger.new[0].Consultant__c = a.Consultant__c;
}

 

 

Trigger Code for Opportunity

-------------------------------------

trigger ConsultantUpdate on Opportunity (before insert) {

Account o = [select Consultant__c from Account where Id = :trigger.new[0].AccountId];
trigger.new[0].Consultant__c = o.Consultant__c;

}

 

My Test Class

---------------------

@IsTest
public with sharing class ConsultantUpdateTest {
static testMethod void validateConsultantonContact() {

Consultant__c c = new Consultant__c(name = 'Mac Kay', first_name__c = 'Mac');
insert c;
system.assertNotEquals(null, c.id);
Account a = new Account(name='Test Account', consultant__c = c.id);
insert a;
system.assertNotEquals(null, a.id);
Contact con = new Contact(lastname = 'Smith', Accountid = a.id);
insert con;
system.assertNotEquals(null, con.id);
Opportunity o = new Opportunity(name = 'Harry' , Accountid = a.id , CloseDate = Date.Today() , StageName = 'Open' , LeadSource = 'Website', Lead_Source_Detail__c = '-- na --');
insert o;
system.assertNotEquals(null, o.id);
}