Illegal polymorphic assignment from polymorphic domain [SOBJECT:User, SOBJECT:Calendar] - salesforce

I am writing a task's trigger and getting an error in salesforce Illegal polymorphic assignment from polymorphic domain [SOBJECT:User, SOBJECT:Calendar]
trigger Status_Change on Task (after update) {
List<Task>updated_tasks=trigger.new;
List<Task> tt=trigger.old;
Task_History__c history=new Task_History__c();
Integer i=0;
for(i=0;i<updated_tasks.size();i++)
{
history.Name=tt.get(i).Subject;
history=new Task_History__c();
history.OldValue__c=tt.get(i).Status;
history.NewValue__c=updated_tasks.get(i).Status;
history.User__c=updated_tasks.get(i).Owner;
insert history;
}
}
error is on line
history.User__c=updated_tasks.get(i).Owner;
When I write history.User__c=updated_tasks.get(i).owner.id then there is no error
but when I tried to get a User's email address from this id then its showing no user corresponding to this id. How do I get Owner's user id from Task SObject's owner field. I think error is due to Owner is a lookup to [SObject.User,SObject.Calender].so owner's id should be different from User'id .but how to get only User's id from Owner's field in Task object?

You are so close. The syntax is:
history.User__c=updated_tasks.get(i).OwnerId;
You were correct. the task.Owner field is an SObject, task.Owner.Id is valid, but the value being referenced is not populated in the trigger context.
Your trigger is not very well written, it has a dml statement in a loop, and there doesn't appear to be a lookup from the task history to the task, I have referenced one in the updated example below.
trigger Status_Change on Task (after update) {
List<Task_History__c> histories = new List<Task_History__c>();
Task oldValue;
for(Task task : Trigger.new) {
oldValue = Trigger.oldMap.get(task.Id);
histories.add(new Task_History__c(
Name=task.Subject,
OldValue__c=oldValue.Status,
NewValue__c=task.Status,
User__c=task.OwnerId,
//This should be created as well
Task__c=task.Id
));
}
if(histories.size() > 0) {
insert histories;
}
}

Related

Access an ID of a record before its finished being created laravel

I'm trying to store the id of a record within the record. I am aware I could just use a where statement to find the last ID and increment by one but I was curious is it possible to get the ID of the record whilst its been made.
$ScreenshotOBJ = Screenshots::create([
"CollectionID" => $ScreenshotOBJ->id,
//This isn't the whole record just the important part
]);
Is this possible it throws an error saying the variable doesn't exist.
You could make the CollectionID field nullable. And then in the boot method of your Screenshots model, you can set the CollectionID like this,
protected static function boot()
{
parent::boot();
static::created(function ($screenshot) {
$screenshot->update(
['CollectionID' => $screenshot->id)]
);
});
}

Creating a Multi-Contact Event with Apex in Salesforce

I am attempting to use Apex to create a multi-contact event.
I have already enabled Allow Users to Relate Multiple Contacts to Tasks and Events in the activity settings in the scratch org.
I am following the guide and the example at the bottom of these docs but I am constantly getting an error when pushing to the scratch org:
// ...
event.setEventWhoIds(attendeeContactIds);
// ...
Method does not exist or incorrect signature: void setEventWhoIds(List<String>) from the type Event.
I also tried to write directly to the field with:
event.EventWhoIds = attendeeContactIds;
With that, I get the error, that the field is not writable.
attendeeContactIds is a List of Strings representing Contact IDs.
What could I be missing? 🤔🙇🏻‍♂️
It's bit stupid, it's readonly in apex. It's exposed so integrations can quickly create event and essentially a related list together in one all-or-nothing transaction. See also https://salesforce.stackexchange.com/questions/238094/eventwhoids-is-not-writeable-in-apex-class-but-working-on-jsforce
Try something like that?
Savepoint sp = Database.setSavepoint();
event e = new Event(
StartDateTime = System.now(),
EndDateTime = System.now().addHours(1)
);
insert e;
List<EventRelation> invitations = new List<EventRelation>();
for(Contact c : [SELECT Id FROM Contact LIMIT 5]){
invitations.add(new EventRelation(
EventId = e.Id,
RelationId = c.Id,
IsInvitee = true
));
}
insert invitations;
Database.rollback(sp); // unless you really want to send it out

Apex trigger which provides targets on sub campaign generates the error on parent campaign

I have a problem with one trigger. I was trying to set up the solution which will give me the possibility to assign flexible targets for campaigns. I was trying to solve it via formula field but it occurred to be too complex and I was advised it needs to be the apex trigger. I created the custom setting of list type, so then I could query it in the apex trigger. I created the "Apex_Calculator__c" field which combined values from two fields (Event Region [located on the parent campaign level] and Started Campaign [located on the sub-campaign level]). The value from the "Apex_Calculator__c" field was about to bring the same values as in the custom setting.
My knowledge of Apex triggers is limited, so I asked for the help on the internet. I received the help and the trigger was bringing target values in the sub campaign. However, the problem occurred on the parent campaign level. As far as I wanted to change the Event Region (which as I mentioned is part of the "Apex_Calculator__c" formula field), I started to receive the error message:
*
PopulateTarget: execution of BeforeUpdate caused by:
System.NullPointerException: Attempt to de-reference a null object:
Trigger.PopulateTarget: line 18, column 1
*
trigger PopulateTarget on Campaign (before insert, before update)
{
Map <String, Id> recordTypes = new Map <String, Id> ();
for (RecordType recordType : [SELECT Id, DeveloperName FROM RecordType WHERE sObjectType = 'Campaign'])
{
if (recordType.DeveloperName == 'Conference')
{
recordTypes.put(recordType.DeveloperName, recordType.Id);
}
}
for(Campaign campaign : Trigger.new)
{
// Make sure that the campaign record type is not in the map (in the map we keep the ones that we want to exclude)
if (recordTypes.get(campaign.RecordTypeId) == null && String.isNotBlank(campaign.Apex_Calculator__c) == true)
{
String target = DSTargets__c.getInstance(campaign.Apex_Calculator__c).Target__c;
campaign.DS_Target_Multiplier__c = Target;
}
}
}
I feel really lost what is wrong in here. Could anyone help me with this error?
Looks like you don't have DSTargets custom settings for some campaign.Apex_Calculator__c values.
Try this:
// Make sure that the campaign record type is not in the map (in the map we keep the ones that we want to exclude)
if (recordTypes.get(campaign.RecordTypeId) == null && String.isNotBlank(campaign.Apex_Calculator__c) == true)
{
if (DSTargets__c.getInstance(campaign.Apex_Calculator__c) == null)
{
System.debug('### A DSTargets named \'' + campaign.Apex_Calculator__c + '\' doesn't exist');
continue;
}
String target = DSTargets__c.getInstance(campaign.Apex_Calculator__c).Target__c;
campaign.DS_Target_Multiplier__c = Target;
}

updating a field on contact whenever a new event is created?

Here is the problem I'm trying to solve:
When a new TASK/EVENT is created, if the user is a certain profile - we want to update a field on CONTACT with the day the T/E was created.
I tried doing a workflow rule and field update- but I couldn't get it to work... I think since this is a Standard -Standard object relationship via lookup, it might have a problem doing a field update. Any other ideas?? I'd prefer to use the platform for this one...
HALP!
Thanks
We have done exactly what you are talking about via an Apex trigger. Something like this...
//I'm sure this doesn't compile, but it gives you the idea
trigger taskTrigger on Task( after insert, after update ){
Task t = trigger.new ;
Contact contact = [Select Id from Contact where Id = :t.whoId] ;
contact.yourfield = t.AcitivityDate ;
update contact ;
}

Update Multi-Picklist on updating custom object

I have custom object KeywordAccountAssociation__c. This object has three fields
Account__c - Master-Detail(Account)
Keyword__c - Master-Detail(Keyword)
Compositecp__c - Text(255) (External ID) (Unique Case Sensitive)
I have a custom field in Account
DD_Segment__c - multi-picklist
Now I want to update (Insert is fine too) values of DD_Segment__c whenever KeywordAccountAssociation__c is updated. I could write trigger for this but I don't know how? I am new to Salesforce Development and my background is ruby (so getting accustomed to apex is bit difficult for me).
KeywordAccountAssociation__c has multiple rows of Account__c which has same account_id and those account_id are related to a record of custom object Keyword__c. I want to get all keywords related to one account and update in its (account's) multi-picklist. How can I achieve this? If you have doubts about this please do ask. Thanks!
One issue is related to learning to work with triggers in general, which can be started with Salesforce Apex Developer Documents on Triggers
but to answer your actual question, you would essentially need to build a trigger against your custom object that would update the related account. It might look something like this:
trigger keywordAccountUpdate on KeywordAccountAssociation__c (after insert, after update){
set<id> = new set<id>();
for (KeywordAccountAssociation__c a : Trigger.new)
accountIds.put(a.Account__c);
map<id,Account> accountMap = new map<id,Account>([select id, DD_Segment__c from Account where id in :accountIds]);
for (KeywordAccountAssociation__c kaa : Trigger.new){
if (AccountMap.containskey(kaa.Account__c)){
Account thisAccount = AccountMap.get(kaa.Account__c);
String s = thisAccount.DD_Segment__c + ';new value'; // Always add value
if ((thisAccount.DD_Segment__c).contains('second value')
s += ';second value';
AccountsToUpdate.add(new Account(id=thisAccount.id, DD_Segment__c = s));
}
}
}
Please keep in mind that I don't have the structure to test this trigger, I just free handded it, so YMMV.

Resources