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
Saranya MSaranya M 

Illegal assignment from list<event> to sobject. Please help me in modifying the code.

I'm trying to update activity custom field - contact sys id with the value from sys id or siebel id based on the id type field

trigger ContactSysID on Event (before insert) { ID evid; for (event e:trigger.new){ evid=e.whatid; } event e=[select id,contact_sys_id__c from event where whatid=:evid]; Merchandise__c merch=[SELECT TYPEOF What WHEN Merchandise__C THEN name,id_type__C,sys_id__c,siebel_id__C END FROM event where WHAT.TYPE='Merchandise__c' limit 1]; For (Merchandise__C m:merch){ if(m.id_type__C=='Sys ID'){ e.Contact_sys_ID__c=m.sys_id__c; } else{ e.contact_sys_id__C=m.siebel_id__C; }
}
AshwaniAshwani
If you can post your code in well ndented way then it would be helpful. From the heading it comes to know that you are assigning diffrent type of object in a different list.
Shingo YamazakiShingo Yamazaki
Dear Saranya,

I'm Shingo.

event e=[select id,contact_sys_id__c from event where whatid=:evid];
This SOQL will return List<Event>, not single Event.
That's why you got above error.

You can modify it by adding "LIMIT 1" to the SOQL.

AshwaniAshwani
Try with this:

Event[] e=[select id,contact_sys_id__c from event where whatid=:evid];


Saranya MSaranya M
Thanks all. The query is working now. But I face another problem now. If I try to create ane event for Contact object, the below trigger is fired and it is working perfectly fine. But when I create an event for some other object eg Merchandise object, then also the below trigger is getting fired and it displays error and the event is not saved.

How can I restrict the below trigger to work only for Contact object and not get fired for other objects.

trigger ContactSysIDEvent on Event (before insert) {
       IDevid;
       for (event e:trigger.new)
       {
       evid=e.whoid;
       contact m123 =[select name,id_type__C,sys_id__c,siebel_id__C from contact  where id=:evid];
       if(m123.id_type__C=='Sys ID')
                  { e.Contact_sys_ID__c=m123.sys_id__c; }
       else
                 { e.contact_sys_id__C=m123.siebel_id__C; }

       }
}