Apex Trigger to update the value of Related to company field if it is null only on the task object based on the related to contact

0 votes
I need to write a trigger to pre-fill the value of "Related to: company" field ( if it is null only) on the task object based on the "related to: contact" field value before submitting the record. Please help me how can I achieve this.
Mar 16, 2022 in SalesForce by surbhi
• 3,810 points
1,583 views

1 answer to this question.

0 votes

trigger TaskTrigger on Task (after update) { for(Task newTask : Trigger.New){ // Task.WhoId != null ----Checks Contact is null or not // ((string)Task.WhoId).startsWith('003') ----Checks Task is related to Contact // Task.WhatId == null ----Checks the related to field is null or not if(Task.WhoId != null && ((string)Task.WhoId).startsWith('003') && Task.WhatId == null ){ // Add your code as per your needs. } } }

answered Mar 17, 2022 by CoolCoder
• 4,400 points

Related Questions In SalesForce

0 votes
1 answer

Salesforce Formula Text Value Based on Custom Field Picklist Value

You should update the fields using the ...READ MORE

answered Mar 2, 2022 in SalesForce by surbhi
• 3,810 points
5,696 views
0 votes
0 answers

Salesforce Formula Text Value Based on Custom Field Picklist Value

on Cases, I have: Department(picklist values=Ecom, B2B, etc.) ...READ MORE

Apr 8, 2022 in SalesForce by surbhi
• 3,810 points
1,384 views
0 votes
0 answers

Salesforce Apex SingleEmailMessage with Template to User not contact

Im trying to send an email in ...READ MORE

Feb 28, 2022 in SalesForce by surbhi
• 3,810 points
2,815 views
+2 votes
2 answers

Salesforce Interview questions

Here are some questions very important for ...READ MORE

answered Jan 11, 2019 in Career Counselling by Suresh
• 720 points
2,773 views
0 votes
1 answer

How to connect to salesforce from tableau?

Hi, follow these steps to connect to Salesforce: 1. ...READ MORE

answered Mar 25, 2019 in Tableau by Cherukuri
• 33,030 points
747 views
0 votes
1 answer

Power BI - Salesforce

Hi, Follow below steps: 1. Go to Data source. 2. ...READ MORE

answered Mar 25, 2019 in Power BI by Cherukuri
• 33,030 points
486 views
0 votes
2 answers

What is the best training for Salesforce ADM-201 Exam?

Hi @Vardhan, I took Edureka's Salesforce Online Training, ...READ MORE

answered Jun 3, 2021 in Others by Jaya
• 140 points

edited Dec 22, 2021 by Soumya 549 views
0 votes
1 answer
0 votes
1 answer

How to Update RecordTypeId field in Lightning record form in salesforce?

A critical action that messes everything up ...READ MORE

answered Mar 3, 2022 in SalesForce by CoolCoder
• 4,400 points
2,920 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP