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
gjblajiangjblajian 

create task from field in parent object in a trigger

I have the below code which is attempting to create a task based on the Id set in the EWM_Contact__c lookup field in the parent object of the trigger's object but I keep getting the following error

Assigned To: Assigned To ID: owner cannot be blank

The problem is that when I run the SOQL [select name, Household_Name__r.EWM_Contact__r.Id from Investment_Profile__c where name = 'Test Original Contact Entry'] I can retrieve the id for this field so I know it is not blank. Can anyone tell me what I am doing wrong?

...
if((ipOld != null && ipNew.Account_Status__c == 'Client Onboarded' && ipOld.Account_Status__c != ipNew.Account_Status__c)){
Id id = ipNew.Household_Name__r.EWM_Contact__r.Id;
if(al.StringUtils.isNotBlank(''+ id)){
Task t = new Task();
t.OwnerId = id;
t.Subject = 'EWM Client Welcome Call';
t.ActivityDate = date.today().addDays(7);
t.Priority = 'Normal';
t.Status = 'Not Started';
t.WhatId = ipNew.Household_Name__r.Id;
insert t;
}
}
...
Best Answer chosen by Admin (Salesforce Developers) 
gjblajiangjblajian

Anup, thankyou. Your solution wasn't exactly what I needed but it definitely put me on the right track. The solution was as follows:

  if((ipOld != null && ipNew.Account_Status__c == 'Client Onboarded' && ipOld.Account_Status__c != ipNew.Account_Status__c)){
  	Id id = ipNew.Household_Name__r.Id;
  	Account acc = [select EWM_Contact__c from Account where id = :ipNew.Household_Name__c];
		Task t = new Task();
		t.OwnerId = acc.EWM_Contact__c;
		system.debug('the owner id is:::'+t.OwnerId);
		t.Subject = 'EWM Client Welcome Call';
		t.ActivityDate = date.today().addDays(7);
		t.Priority = 'Normal';
		t.Status = 'Not Started';
		t.WhatId = ipNew.Household_Name__c;
  	insert t;
  }

Again, thank you for your help.

All Answers

Anup JadhavAnup Jadhav

Try changing the SOQL to:

 

[select name, Household_Name__c, Household_Name__r.EWM_Contact__c, Household_Name__r.EWM_Contact__r.Id from Investment_Profile__c where name = 'Test Original Contact Entry']

 and set the t.OwnerId to:

 

t.OwnerId = Household_Name__r.EWM_Contact__c;

system.debug('the owner id is:::'+t.OwnerId);

This should work!

 

- Anup

gjblajiangjblajian
Anup, Thank you for your response, I made the change you suggested but that did not quite do it. I am still getting the same error telling me that the ownerid is blank.
Anup JadhavAnup Jadhav

Hmm, try this soql instead, it checks for nullability and blank values:

 

[select name, Household_Name__c, Household_Name__r.EWM_Contact__c, Household_Name__r.EWM_Contact__r.Id from Investment_Profile__c where name = 'Test Original Contact Entry' AND Household_Name__r.EWM_Contact__c != null AND Household_Name__r.EWM_Contact__r.Id != null];

 Regards,

Anup

gjblajiangjblajian

Anup, thankyou. Your solution wasn't exactly what I needed but it definitely put me on the right track. The solution was as follows:

  if((ipOld != null && ipNew.Account_Status__c == 'Client Onboarded' && ipOld.Account_Status__c != ipNew.Account_Status__c)){
  	Id id = ipNew.Household_Name__r.Id;
  	Account acc = [select EWM_Contact__c from Account where id = :ipNew.Household_Name__c];
		Task t = new Task();
		t.OwnerId = acc.EWM_Contact__c;
		system.debug('the owner id is:::'+t.OwnerId);
		t.Subject = 'EWM Client Welcome Call';
		t.ActivityDate = date.today().addDays(7);
		t.Priority = 'Normal';
		t.Status = 'Not Started';
		t.WhatId = ipNew.Household_Name__c;
  	insert t;
  }

Again, thank you for your help.

This was selected as the best answer
Anup JadhavAnup Jadhav

No problem at all! Glad I could be of service to you! :)

 

Have a nice day!

 

- Anup