-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
14Questions
-
15Replies
Adding multiple junction records at the same time
Hi there,
I have a custom object called Skill Set. Within each Skill Set there are 3 junction objects.
1. Expert At
2. Intermediate At
3. Novice At
The junction records are used to link Contact records to the Skill Set, and gives us visibility over which contacts have a speicfic level of skill. (Juntion objects have 2 lookup fields, 1 to Skill Set, 1 to Contact).
The only issue im finding with this is that I cant add more than 1 record at a time. E.g. If i had 10 contacts that were an Expert At Skill_A, i would need to create 10 records, one at a time. Also, if i have a Contact that has 10 skills, i have to create 10 junction records, one at a time. This can be quite time consuming.
Im wondering if its possible to create multiple junction records at the same time.
E.g. 1
Im in a Contact record and want to record that the person is an Expert At 10 skills.
I scroll to the "Expert At" reated list on the Contact and select Add New.
This returns a lookup window listing all Skills, with a checkbox next to each one, allowing me to select multiple skills.
I select 10 skills, and press OK.
This returns me to the Contact record, where i can see 10 junction Expert At related records, one for each Skill.
E.g. 2
Im in a Skill Set record, and want to add many contacts to the Skill as Experts At.
I scroll to the Expert At related list and select "Add New".
This allows me to search for and Add the 10 contacts.
I press Ok.
This returns me to the Skill Set record, where i can see 10 junction "Expert At" related records, one for each Contact.
Is this achieveable? If so, can anyone tell me how?
Many thanks. Much appreciated.
- Wes McCarthy
- June 22, 2016
- Like
- 0
Developing a Resource Gantt Chart - Has anyone done this before?
In our Org, we use treat the Order record like a Project, and assign Resources to the Order Products. Im trying to come up with a way of displaying Report Utilisation in a Gantt chart, however cant seem to find the correct way to do so. Has anyone done something similar to this, or can anyone point me in the right direction of some VF code that will allow me to do this?
Thanks, much appreciated.
- Wes McCarthy
- June 09, 2016
- Like
- 0
Help with Test Class Coverage
Im writing a test class to cover a task trigger. I have 73% code coverage currently. The lines of code that i havent written a Test for are below, as im unsure how to write Tests for this. Im very new to APEX. Any help is much appreciated. Many thanks.
List<Id> OppIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whatId != null && String.valueOf(t.whatId).startsWith('006')==TRUE) //check if the task is associated with an Opp
{
OppIds.add(t.whatId);
//System.debug('In here:' + t.whatid);
}//if 2
}//if 1
}//for
map<id,Opportunity> OpptaskMap = new map<id,Opportunity>([SELECT Id, Situation__c, Forecast_Delivery_Start_Date__c, Forecast_Delivery_End_Date__c, Bypass_Validation_Rule__c, Problem__c, Implication__c, Need_Payoff__c , Involves_Active_Partners__c, Google_Drive_URL__c, StageName, (Select ID From OpportunityLineItems) FROM Opportunity WHERE Id IN :OppIds]);
List<OpportunityContactRole> OCR = [select id from OpportunityContactRole where OpportunityId IN :oppIds];
List<OpportunityPartner> OPartner = [select id from OpportunityPartner where OpportunityId in :oppIds];
- Wes McCarthy
- May 30, 2016
- Like
- 0
Test Class Coverage @ 73% - please help
Im having issues getting my code coverage up to 75% for a Task Trigger. Please see Trigger and Test Class below. My APEX skills are very basic. Any advice on how to boost my coverage is much appreciated. Thanks in advance!!
trigger TaskTrigger on Task (before insert, before update)
{
List<Id> OppIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whatId != null && String.valueOf(t.whatId).startsWith('006')==TRUE) //check if the task is associated with an Opp
{
OppIds.add(t.whatId);
//System.debug('In here:' + t.whatid);
}//if 2
}//if 1
}//for
map<id,Opportunity> OpptaskMap = new map<id,Opportunity>([SELECT Id, Situation__c, Forecast_Delivery_Start_Date__c, Forecast_Delivery_End_Date__c, Bypass_Validation_Rule__c, Problem__c, Implication__c, Need_Payoff__c , Involves_Active_Partners__c, Google_Drive_URL__c, StageName, (Select ID From OpportunityLineItems) FROM Opportunity WHERE Id IN :OppIds]);
List<OpportunityContactRole> OCR = [select id from OpportunityContactRole where OpportunityId IN :oppIds];
List<OpportunityPartner> OPartner = [select id from OpportunityPartner where OpportunityId in :oppIds];
for(Task tsk:trigger.new)
{
if(OpptaskMap.keyset().contains(tsk.whatid))
{
OpptaskMap.get(tsk.whatid).Bypass_Validation_Rule__c = TRUE;
if(tsk.Status == 'Completed' && tsk.Subject == 'Communicate with Client')
{
OpptaskMap.get(tsk.whatid).StageName = '2-Identified';
//System.debug('Communicate with Client:' + OpptaskMap.get(tsk.whatid).StageName);
}
Else if( tsk.Status == 'Completed' && tsk.Subject == 'Validate Opportunity')
{
If (OpptaskMap.get(tsk.whatid).Situation__c == Null || OpptaskMap.get(tsk.whatid).Problem__c == Null || OpptaskMap.get(tsk.whatid).Implication__c == Null || OpptaskMap.get(tsk.whatid).Need_Payoff__c == Null || OpptaskMap.get(tsk.whatid).Involves_Active_Partners__c == Null)
{
tsk.adderror('Task cannot be completed until the following Opportunity fields have been populated: Situation, Problem, Implication, Need & PayOff, Involves Active Partners.');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '3-Validated';
}
}
// System.debug('1 condition:' + LeadtaskMap.get(tsk.whatid).Description == NULL);
// System.debug('2 condition:' + tsk.Status == 'Completed');
// System.debug('3 condition:' + tsk.Subject == 'Make contact with Lead');
// System.debug('task:' + tsk);
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportunity and Identify Contact Roles')
{
If(OpptaskMap.get(tsk.whatid).Forecast_Delivery_Start_Date__c==Null || OpptaskMap.get(tsk.whatid).Forecast_Delivery_End_Date__c==Null)
{
tsk.adderror('Please add the Forecast Delivery Start and End Dates to the "Delivery Details" section of the Opportunity');
}
If(OpptaskMap.get(tsk.whatid).Involves_Active_Partners__c == 'Yes')
{
If (OpptaskMap.get(tsk.whatid).OpportunityLineItems.isEmpty() || OCR.size()==0 || OPartner.size()==0)
{
tsk.adderror('Task cannot be completed until Contact Roles, Products AND Partners have been added to the Opportunity');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '4-Qualified';
}
}
Else If(OpptaskMap.get(tsk.whatid).Involves_Active_Partners__c == 'No')
{
If (OpptaskMap.get(tsk.whatid).OpportunityLineItems.isEmpty() || OCR.size()==0)
{
tsk.adderror('Task cannot be completed until Contact Roles AND Products have been added to the Opportunity');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '4-Qualified';
}
}
Else
{
//OpptaskMap.get(tsk.whatid).StageName = '4-Qualified';
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Create quote and submit for approval')
{
If(OpptaskMap.get(tsk.whatid).Google_Drive_URL__c==Null)
{
tsk.adderror('Task cannot be completed until associated Google Drive URL has been added to the Opportunity');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '5-Proposal';
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Proposal Outcome')
{
If (tsk.Proposal_Outcome__c == 'Commercial Agreement')
{
OpptaskMap.get(tsk.whatid).StageName = '6-Commercial Agreement';
}
Else If (tsk.Proposal_Outcome__c == 'Lost')
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '9-Lost';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
Else
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '8-Abandoned';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Opportunity Outcome')
{
If (tsk.Opportunity_Outcome__c == 'Won')
{
OpptaskMap.get(tsk.whatid).StageName = '7-Won';
}
Else If (tsk.Opportunity_Outcome__c == 'Lost')
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '9-Lost';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
Else
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '8-Abandoned';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
}
Else
{
//OpptaskMap.get(tsk.whatid).StageName = '7-Won';
}
}
}
List<Opportunity> OppList = OpptaskMap.values();
update OppList;
}
- Wes McCarthy
- May 25, 2016
- Like
- 0
VisualForce Email Template - Issue getting related list details
My opportunity has a related object called "Resource Request". A Resource Request record gets automatically created when the Opp reaches a certain stage. When this happens, i would like to send an email to relevant parties, notifying them that a resource record has been created in relation to the associated Opportunity. I want the email to list details about the Opp, Line Items and have a link to the Resource Request object. The issue im having is getting the link to the Resource Request object. My VF html code lis below. I've tried to setup a workflow rule from the Resource Request to write back its ID to the Opp, once created, however this isnt allowed. I am getting an error in my current VF code because it doesnt accept "Resource_Request__c" as a valid association to an Opportunity.
Some more details.
- I wanted Resource Request to be a child of the Opp, but was unable to. Received an error message that could not create a parent child relationship with the Opp, as the Opp object was already in existance.
- Instead i created a LookUp from the Resource Request to the Opp, so that the Resource Request appears as a related list on the Opp. NOTE: There will ever only be a 1:1 relationship between the Opp and Resource Request.
Any help is much appreciated.
Thanks
<messaging:emailTemplate recipientType="User"
relatedToType="Opportunity"
subject="{!relatedTo.Job_Code__c} - Resource Request">
<messaging:htmlEmailBody >
<html>
<body>
<STYLE type="text/css">
TH {font-size: 11px; font-face: arial;background: #CCCCCC; border-width: 1; text-align: left }
TD {font-size: 11px; font-face: verdana }
TABLE {border: solid #CCCCCC; border-width: 1}
TR {border: solid #CCCCCC; border-width: 1}
</STYLE>
<font face="arial" size="2">
<p>Hi team,</p>
<p>I need some resources to fill the below requirements.</p>
<table border="1" >
<tr>
<th align="left">Job Code</th>
<td align="left">{!RelatedTo.Job_Code__c}</td>
</tr>
<tr>
<th align="left">Opportunity Name</th>
<td align="left">{!RelatedTo.Name}</td>
</tr>
<tr>
<th align="left">Account Name</th>
<td align="left">{!RelatedTo.Account.Name}</td>
</tr>
<tr>
<th align="left">Region</th>
<td align="left">{!RelatedTo.Region__c}</td>
</tr>
</table>
<p>Product Details.</p>
<table border="1" >
<tr >
<th align="left">Product Name</th>
<th align="left">Description</th>
<th align="left">No. of Days Needed</th>
<th align="left">Start Date</th>
</tr>
<apex:repeat var="cx" value="{!relatedTo.OpportunityLineItems}">
<tr>
<td align="left">{!cx.PriceBookEntry.name}</td>
<td align="center">{!cx.Description}</td>
<td align="center">{!cx.Quantity}</td>
<td align="left"><apex:outputText value="{0, date, EEE, MMMM d, yyyy}"><apex:param value="{!cx.ServiceDate}"/></apex:outputText></td>
</tr>
</apex:repeat>
<apex:repeat var="RR" value="{!relatedTo.Resource_Request__c}">
</apex:repeat>
</table>
<p>You can navigate to the Opportunity via the following link: <a href="{!LEFT($Api.Partner_Server_URL_290,FIND('services',$Api.Partner_Server_URL_290)-1)}{!RR.Id}">{!relatedTo.Name}</a></p>
</font>
</body>
</html>
</messaging:htmlEmailBody>
<messaging:plainTextEmailBody >
Hi team,
I need some resources to fill the below requirements.
[ Product Name] - [ No of Days Needed ] - [ Start Date]
<apex:repeat var="cx" value="{!relatedTo.OpportunityLineItems}">
[ {!cx.PriceBookEntry.name} ] - [ {!cx.Quantity} ] - [ {!cx.ServiceDate} ]
</apex:repeat>
For more information login to http://www.salesforce.com
</messaging:plainTextEmailBody>
</messaging:emailTemplate>
- Wes McCarthy
- May 10, 2016
- Like
- 0
How to setup validation that only allows a User to update an Opp Stage via Task Workflow
Hi there,
I have an issue that im hoping somebody can help me with re: the Sales process.
I currently have implemented a trigger that will update the Stage field of an Opportunity, based on completing Opportunity tasks. Once the first task is completed, the stage gets updated, and a new task in the workflow is generated....and so on.
The issue is that i cant make the Stage field read-only, as it is a standard object field. Meaning the Sales reps can completely ignore the workflow tasks, and manually update the Stage field.
Ideally i'd like to have some sort of validation that prevents Users from updating the stage field maually, and only allows it to happen via completion of an Opportunity task. Unfortunately, i cant create a validation rule that allows this flexibility.
Does anybody have any suggestions? Any help is much appreciated!
Thanks!
- Wes McCarthy
- May 04, 2016
- Like
- 0
VF Page for Gantt Chart display
Im trying to use some VF code to display a gantt chart in SF, but am having problems getting it to display. Im using code provided from http://dhtmlx.com/docs/products/dhtmlxGantt/. My VF page is saving ok, but it doesnt display anything when rendered.
The full code is outlined below.
Any help is much appreciated! Thanks.
<apex:page standardController="Order" standardStylesheets="false" showHeader="false">
<style>
body {font-size:12px; background: #F3F3EC; padding-top: 0px;}
.{font-family:arial;font-size:12px}
h1 {cursor:hand;font-size:16px;margin-left:10px;line-height:10px}
xmp {color:green;font-size:12px;margin:0px;font-family:courier;background-color:#e6e6fa;padding:2px}
.hdr{
background-color:lightgrey;
margin-bottom:10px;
padding-left:10px;
}
</style>
<head>
<apex:stylesheet value="{!URLFOR($Resource.Gantt, 'codebase/dhtmlxgantt.css')}"/>
<apex:includeScript value="{!URLFOR($Resource.Gantt, 'codebase/dhtmlxcommon.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.Gantt, 'codebase/dhtmlxgantt.js')}"/>
<script language="JavaScript" type="text/javascript">
function createChartControl(htmlDiv1)
{
//project 1
var project1 = new GanttProjectInfo(1, "Institutional Sales", new Date(2010, 5, 11));
var parentTask1 = new GanttTaskInfo(1, "IS Planning Timeline", new Date(2010, 5, 11), 208, 100, "");
parentTask1.addChildTask(new GanttTaskInfo(2, "Plan Development", new Date(2010, 5, 11), 100, 100, ""));
parentTask1.addChildTask(new GanttTaskInfo(3, "Finalize Your Plan", new Date(2010, 5, 21), 24, 100, ""));
parentTask1.addChildTask(new GanttTaskInfo(4, "Manager Discussions", new Date(2010, 5, 24), 80, 100, ""));
parentTask1.addChildTask(new GanttTaskInfo(4, "Final Revisions", new Date(2010, 6, 4), 24, 100, ""));
project1.addTask(parentTask1);
var ganttChartControl = new GanttChart();
ganttChartControl.setImagePath("{!URLFOR($Resource.Gantt, 'codebase/imgs/')}");
ganttChartControl.setEditable(false);
ganttChartControl.addProject(project1);
ganttChartControl.showTooltip(false);
ganttChartControl.create(htmlDiv1);
}
</script>
</head>
<body onload="createChartControl('GanttDiv')">
<div style="width:100%;height:240px;position:absolute;" id="GanttDiv"></div>
</body>
</apex:page>
- Wes McCarthy
- April 27, 2016
- Like
- 0
Task Trigger on Opportunity - trying to reference Opp Product details
Hi there,
I have a task trigger running for Opportunities, that updates the Opp stage based on Task completion. There is some logic to ensure that certain fields have been entered on the Opportunity, before being allowed proceed to the next stage. The issue i'm facing is trying to check that certain fields have been input on the Opportunity Products, before proceeding to the next stage of the Opportunity.
Can somebody please tell me how to go about this. Below is my attempt so far, however i dont know if im on the right track.
Thanks in advance!
trigger TaskTrigger on Task (before insert, before update)
{
List<Id> OppIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whatId != null && String.valueOf(t.whatId).startsWith('006')==TRUE) //check if the task is associated with an Opp
{
OppIds.add(t.whatId);
//System.debug('In here:' + t.whatid);
}//if 2
}//if 1
}//for
map<id,Opportunity> OpptaskMap = new map<id,Opportunity>([SELECT Id, Situation__c, Problem__c, Implication__c, Need_Payoff__c , Involves_Active_Partners__c, StageName, (Select ID, Software_Deal_Type__c, Software_GPP_Code__c From OpportunityLineItems) FROM Opportunity WHERE Id IN :OppIds]);
List<OpportunityContactRole> OCR = [select id from OpportunityContactRole where OpportunityId IN :oppIds];
List<OpportunityPartner> OPartner = [select id from OpportunityPartner where OpportunityId in :oppIds];
for(Task tsk:trigger.new)
{
if(OpptaskMap.keyset().contains(tsk.whatid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Communicate with Client')
{
OpptaskMap.get(tsk.whatid).StageName = '2-Identified';
//System.debug('Communicate with Client:' + OpptaskMap.get(tsk.whatid).StageName);
}
Else if( tsk.Status == 'Completed' && tsk.Subject == 'Validate Opportunity')
{
If (OpptaskMap.get(tsk.whatid).Situation__c == Null || OpptaskMap.get(tsk.whatid).Problem__c == Null || OpptaskMap.get(tsk.whatid).Implication__c == Null || OpptaskMap.get(tsk.whatid).Need_Payoff__c == Null || OpptaskMap.get(tsk.whatid).Involves_Active_Partners__c == Null)
{
tsk.adderror('Task cannot be completed until the following Opportunity fields have been populated: Situation, Problem, Implication, Need & PayOff, Involves Active Partners.');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '3-Validated';
}
}
// System.debug('1 condition:' + LeadtaskMap.get(tsk.whatid).Description == NULL);
// System.debug('2 condition:' + tsk.Status == 'Completed');
// System.debug('3 condition:' + tsk.Subject == 'Make contact with Lead');
// System.debug('task:' + tsk);
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportunity and Identify Contact Roles')
{
If(OpptaskMap.get(tsk.whatid).Involves_Active_Partners__c == 'Yes')
{
If (OpptaskMap.get(tsk.whatid).OpportunityLineItems.isEmpty() || OCR.size()==0 || OPartner.size()==0)
{
tsk.adderror('Task cannot be completed until Contact Roles, Products AND Partners have been added to the Opportunity');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '4-Qualified';
}
}
Else If(OpptaskMap.get(tsk.whatid).Involves_Active_Partners__c == 'No')
{
If (OpptaskMap.get(tsk.whatid).OpportunityLineItems.isEmpty() || OCR.size()==0)
{
tsk.adderror('Task cannot be completed until Contact Roles AND Products have been added to the Opportunity');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '4-Qualified';
}
}
Else
{
//OpptaskMap.get(tsk.whatid).StageName = '4-Qualified';
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Create quote and submit to Client')
{
OpptaskMap.get(tsk.whatid).StageName = '5-Proposal';
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Proposal Outcome')
{
If (tsk.Proposal_Outcome__c == 'Commercial Agreement')
{
List<OpportunityLineItem>OLI = OpptaskMap.get(tsk.whatid).OpportunityLineItems;
for (OpportunityLineItem OppLI : OLI)
{
//Am i correct in doing this?
}
{
OpptaskMap.get(tsk.whatid).StageName = '6-Commercial Agreement';
}
Else If (tsk.Proposal_Outcome__c == 'Lost')
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '9-Lost';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
Else
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '8-Abandoned';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Opportunity Outcome')
{
If (tsk.Opportunity_Outcome__c == 'Won')
{
OpptaskMap.get(tsk.whatid).StageName = '7-Won';
}
Else If (tsk.Opportunity_Outcome__c == 'Lost')
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '9-Lost';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
Else
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '8-Abandoned';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
}
Else
{
//OpptaskMap.get(tsk.whatid).StageName = '7-Won';
}
}
}
List<Opportunity> OppList = OpptaskMap.values();
update OppList;
}
- Wes McCarthy
- January 28, 2016
- Like
- 0
Task Trigger across < 1 Parent Object
Im trying to write a Task Trigger that identifies the parent object type of the task, and then executes some logic depending on the type e.g. If type = Opportunity, do this. If Type = Lead, do that.
When i try to save my code, i get the following error:
Error: Compile Error: Variable does not exist: ObjectTaskMap at line 48 column 17
I dont know why it is saying this. Can anybody help me, or point me in the direction of similar code? My code is below.
Thanks in advance.
trigger TaskTrigger on Task (before insert, before update)
{
string str = '';
List<Id> ObjectIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whatId != null && String.valueOf(t.whatId).startsWith('006')==TRUE) //check if the task is associated with an Opp
{
ObjectIds.add(t.whatId);
str = 'Opportunity';
//System.debug('In here:' + t.whatid);
}//if 2
Else if(t.whoId != null && String.valueOf(t.whoId).startsWith('00Q')==TRUE) //check if the task is associated with an Opp
{
ObjectIds.add(t.whoId);
str = 'Lead';
//System.debug('In here:' + t.whoid);
}//if 2
}//if 1
}//for
if(str == 'Opportunity')
{
map<id,Opportunity> ObjectTaskMap = new map<id,Opportunity>([SELECT Id, Situation__c, Problem__c, Implication__c, Need_Payoff__c , StageName, (Select ID From OpportunityLineItems) FROM Opportunity WHERE Id IN :ObjectIds]);
List<OpportunityContactRole> OCR = [select id from OpportunityContactRole where OpportunityId IN :ObjectIds];
}
Else If(str == 'Lead')
{
map<id,Lead> ObjectTaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :ObjectIds AND IsConverted=FALSE]);
}
for(Task tsk:trigger.new)
{
if(ObjectTaskMap.keyset().contains(tsk.whatid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Communicate with Client')
{
ObjectTaskMap.get(tsk.whatid).StageName = '2-Identified';
//System.debug('Communicate with Client:' + ObjectTaskMap.get(tsk.whatid).StageName);
}
Else if( tsk.Status == 'Completed' && tsk.Subject == 'Validate Opportunity')
{
If (ObjectTaskMap.get(tsk.whatid).Situation__c == Null || ObjectTaskMap.get(tsk.whatid).Problem__c == Null || ObjectTaskMap.get(tsk.whatid).Implication__c == Null || ObjectTaskMap.get(tsk.whatid).Need_Payoff__c == Null)
{
tsk.adderror('Task cannot be completed until the following Opportunity fields have been populated: Situation, Problem, Implication, Need & PayOff');
}
Else
{
ObjectTaskMap.get(tsk.whatid).StageName = '3-Validated';
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportunity and Identify Contact Roles')
{
If (ObjectTaskMap.get(tsk.whatid).OpportunityLineItems.isEmpty() || OCR.size()==0)
{
tsk.adderror('Task cannot be completed until Contact Roles AND Products have been added to the Opportunity');
}
Else
{
ObjectTaskMap.get(tsk.whatid).StageName = '4-Qualified';
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Create quote and submit to Client')
{
ObjectTaskMap.get(tsk.whatid).StageName = '5-Proposal';
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Proposal Outcome')
{
If (tsk.Proposal_Outcome__c == 'Commercial Agreement')
{
ObjectTaskMap.get(tsk.whatid).StageName = '6-Commercial Agreement';
}
Else If (tsk.Proposal_Outcome__c == 'Lost')
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
ObjectTaskMap.get(tsk.whatid).StageName = '9-Lost';
ObjectTaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
ObjectTaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
Else
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
ObjectTaskMap.get(tsk.whatid).StageName = '8-Abandoned';
ObjectTaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
ObjectTaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Opportunity Outcome')
{
If (tsk.Opportunity_Outcome__c == 'Won')
{
ObjectTaskMap.get(tsk.whatid).StageName = '7-Won';
}
Else If (tsk.Opportunity_Outcome__c == 'Lost')
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
ObjectTaskMap.get(tsk.whatid).StageName = '9-Lost';
ObjectTaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
ObjectTaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
Else
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
ObjectTaskMap.get(tsk.whatid).StageName = '8-Abandoned';
ObjectTaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
ObjectTaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
}
Else
{
//ObjectTaskMap.get(tsk.whatid).StageName = '7-Won';
}
}
Else If(ObjectTaskMap.keyset().contains(tsk.whoid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
{
ObjectTaskMap.get(tsk.whoid).Status = 'Contacted';
}
// System.debug('1 condition:' + ObjectTaskMap.get(tsk.whoid).Description == NULL);
// System.debug('2 condition:' + tsk.Status == 'Completed');
// System.debug('3 condition:' + tsk.Subject == 'Make contact with Lead');
// System.debug('task:' + tsk);
Else if(ObjectTaskMap.get(tsk.whoid).Description == Null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Lead')
{
tsk.adderror('Task cannot be completed until the following LEAD fields have been populated: Description');
}
Else
{
ObjectTaskMap.get(tsk.whoid).Status = 'Qualified';
}
}
}
if(str == 'Opportunity')
{
List<Opportunity> ObjectList = ObjectTaskMap.values();
update ObjectList;
}
Else if(str == 'Lead')
{
List<Lead> ObjectList = ObjectTaskMap.values();
update ObjectList;
}
}
- Wes McCarthy
- January 18, 2016
- Like
- 0
Help with Task Trigger across > 1 parent object
Im trying to write a Task trigger which checks the parent object type of the task. Once the type is known, there are steps of logic e.g. If Opportunity Task - do this. If Lead Task - do that.
Im trying to save my code, but keep getting the following error:
Error: Compile Error: Variable does not exist: ObjectTaskMap at line 48 column 17
I dont know why its telling me that the variable doesnt exist. Can anyone help, or point me in the direction of similar code? My code is below.
Thanks in advance!!
trigger TaskTrigger on Task (before insert, before update)
{
string str = '';
List<Id> ObjectIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whatId != null && String.valueOf(t.whatId).startsWith('006')==TRUE) //check if the task is associated with an Opp
{
ObjectIds.add(t.whatId);
str = 'Opportunity';
//System.debug('In here:' + t.whatid);
}//if 2
Else if(t.whoId != null && String.valueOf(t.whoId).startsWith('00Q')==TRUE) //check if the task is associated with an Opp
{
ObjectIds.add(t.whoId);
str = 'Lead';
//System.debug('In here:' + t.whoid);
}//if 2
}//if 1
}//for
if(str == 'Opportunity')
{
map<id,Opportunity> ObjectTaskMap = new map<id,Opportunity>([SELECT Id, Situation__c, Problem__c, Implication__c, Need_Payoff__c , StageName, (Select ID From OpportunityLineItems) FROM Opportunity WHERE Id IN :ObjectIds]);
List<OpportunityContactRole> OCR = [select id from OpportunityContactRole where OpportunityId IN :ObjectIds];
}
Else If(str == 'Lead')
{
map<id,Lead> ObjectTaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :ObjectIds AND IsConverted=FALSE]);
}
for(Task tsk:trigger.new)
{
if(ObjectTaskMap.keyset().contains(tsk.whatid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Communicate with Client')
{
ObjectTaskMap.get(tsk.whatid).StageName = '2-Identified';
//System.debug('Communicate with Client:' + ObjectTaskMap.get(tsk.whatid).StageName);
}
Else if( tsk.Status == 'Completed' && tsk.Subject == 'Validate Opportunity')
{
If (ObjectTaskMap.get(tsk.whatid).Situation__c == Null || ObjectTaskMap.get(tsk.whatid).Problem__c == Null || ObjectTaskMap.get(tsk.whatid).Implication__c == Null || ObjectTaskMap.get(tsk.whatid).Need_Payoff__c == Null)
{
tsk.adderror('Task cannot be completed until the following Opportunity fields have been populated: Situation, Problem, Implication, Need & PayOff');
}
Else
{
ObjectTaskMap.get(tsk.whatid).StageName = '3-Validated';
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportunity and Identify Contact Roles')
{
If (ObjectTaskMap.get(tsk.whatid).OpportunityLineItems.isEmpty() || OCR.size()==0)
{
tsk.adderror('Task cannot be completed until Contact Roles AND Products have been added to the Opportunity');
}
Else
{
ObjectTaskMap.get(tsk.whatid).StageName = '4-Qualified';
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Create quote and submit to Client')
{
ObjectTaskMap.get(tsk.whatid).StageName = '5-Proposal';
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Proposal Outcome')
{
If (tsk.Proposal_Outcome__c == 'Commercial Agreement')
{
ObjectTaskMap.get(tsk.whatid).StageName = '6-Commercial Agreement';
}
Else If (tsk.Proposal_Outcome__c == 'Lost')
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
ObjectTaskMap.get(tsk.whatid).StageName = '9-Lost';
ObjectTaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
ObjectTaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
Else
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
ObjectTaskMap.get(tsk.whatid).StageName = '8-Abandoned';
ObjectTaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
ObjectTaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Opportunity Outcome')
{
If (tsk.Opportunity_Outcome__c == 'Won')
{
ObjectTaskMap.get(tsk.whatid).StageName = '7-Won';
}
Else If (tsk.Opportunity_Outcome__c == 'Lost')
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
ObjectTaskMap.get(tsk.whatid).StageName = '9-Lost';
ObjectTaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
ObjectTaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
Else
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
ObjectTaskMap.get(tsk.whatid).StageName = '8-Abandoned';
ObjectTaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
ObjectTaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
}
Else
{
//ObjectTaskMap.get(tsk.whatid).StageName = '7-Won';
}
}
Else If(ObjectTaskMap.keyset().contains(tsk.whoid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
{
ObjectTaskMap.get(tsk.whoid).Status = 'Contacted';
}
// System.debug('1 condition:' + ObjectTaskMap.get(tsk.whoid).Description == NULL);
// System.debug('2 condition:' + tsk.Status == 'Completed');
// System.debug('3 condition:' + tsk.Subject == 'Make contact with Lead');
// System.debug('task:' + tsk);
Else if(ObjectTaskMap.get(tsk.whoid).Description == Null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Lead')
{
tsk.adderror('Task cannot be completed until the following LEAD fields have been populated: Description');
}
Else
{
ObjectTaskMap.get(tsk.whoid).Status = 'Qualified';
}
}
}
if(str == 'Opportunity')
{
List<Opportunity> ObjectList = ObjectTaskMap.values();
update ObjectList;
}
Else if(str == 'Lead')
{
List<Lead> ObjectList = ObjectTaskMap.values();
update ObjectList;
}
}
- Wes McCarthy
- January 18, 2016
- Like
- 0
Help with Test Coverage
Hi there,
Im relatively new to APEX coding, and have been working on a Task trigger in my sandbox environment. All appears to be working ok there. I deployed the trigger to production, but cannot implement it as i do not have any test coverage. I have no idea how to do this, and am hoping somebody can provide some help. The trigger is as follows:
trigger TaskTrigger on Task (before insert, before update)
{
List<Id> OppIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whatId != null && String.valueOf(t.whatId).startsWith('006')==TRUE) //check if the task is associated with an Opp
{
OppIds.add(t.whatId);
//System.debug('In here:' + t.whatid);
}//if 2
}//if 1
}//for
map<id,Opportunity> OpptaskMap = new map<id,Opportunity>([SELECT Id, Situation__c, Problem__c, Implication__c, Need_Payoff__c , StageName, (Select ID From OpportunityLineItems) FROM Opportunity WHERE Id IN :OppIds]);
List<OpportunityContactRole> OCR = [select id from OpportunityContactRole where OpportunityId IN :oppIds];
for(Task tsk:trigger.new)
{
if(OpptaskMap.keyset().contains(tsk.whatid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Communicate with Client')
{
OpptaskMap.get(tsk.whatid).StageName = '2-Identified';
//System.debug('Communicate with Client:' + OpptaskMap.get(tsk.whatid).StageName);
}
Else if( tsk.Status == 'Completed' && tsk.Subject == 'Validate Opportunity')
{
If (OpptaskMap.get(tsk.whatid).Situation__c == Null || OpptaskMap.get(tsk.whatid).Problem__c == Null || OpptaskMap.get(tsk.whatid).Implication__c == Null || OpptaskMap.get(tsk.whatid).Need_Payoff__c == Null)
{
tsk.adderror('Task cannot be completed until the following Opportunity fields have been populated: Situation, Problem, Implication, Need & PayOff');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '3-Validated';
}
}
// System.debug('1 condition:' + LeadtaskMap.get(tsk.whatid).Description == NULL);
// System.debug('2 condition:' + tsk.Status == 'Completed');
// System.debug('3 condition:' + tsk.Subject == 'Make contact with Lead');
// System.debug('task:' + tsk);
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportunity and Identify Contact Roles')
{
If (OpptaskMap.get(tsk.whatid).OpportunityLineItems.isEmpty() || OCR.size()==0)
{
tsk.adderror('Task cannot be completed until Contact Roles AND Products have been added to the Opportunity');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '4-Qualified';
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Create quote and submit to Client')
{
OpptaskMap.get(tsk.whatid).StageName = '5-Proposal';
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Proposal Outcome')
{
If (tsk.Proposal_Outcome__c == 'Commercial Agreement')
{
OpptaskMap.get(tsk.whatid).StageName = '6-Commercial Agreement';
}
Else If (tsk.Proposal_Outcome__c == 'Lost')
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '9-Lost';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
Else
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '8-Abandoned';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Opportunity Outcome')
{
If (tsk.Opportunity_Outcome__c == 'Won')
{
OpptaskMap.get(tsk.whatid).StageName = '7-Won';
}
Else If (tsk.Opportunity_Outcome__c == 'Lost')
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '9-Lost';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
Else
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '8-Abandoned';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
}
Else
{
//OpptaskMap.get(tsk.whatid).StageName = '7-Won';
}
}
}
List<Opportunity> OppList = OpptaskMap.values();
update OppList;
}
Any help is much appreciated.
Thanks in advance.
Wes
- Wes McCarthy
- January 12, 2016
- Like
- 0
Task trigger updating Lead record incorrectly.
If Task is "A" and Status = Completed
Update Lead Status to 'Contacted.'
If Task is "B" and Status = Completed and Lead Description field is Null
Throw an error - 'Task cannot be completed while description is Null.
Else
Update Lead Status to 'Qualified'.
The issue i've encountered is that when i complete Task "A", the Lead Status directly updates to 'Qualified'. When i complete Task "B" and Lead Description is Null, nothing happens. I dont receive an error message. My code is below.
Any suggestions are very welcome.
Thank you.
trigger LeadTrigger on Task (before insert, before update) {
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new) {
if(t.Status=='Completed') {
if(t.whoId != null && String.valueOf(t.whoId).startsWith('00Q')==TRUE) //check if the task is associated with a lead {
leadIds.add(t.whoId);
}//if 2
}//if 1
}//for
map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
for(Task tsk:trigger.new){
if(LeadtaskMap.keyset().contains(tsk.whoid)) {
if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead') {
LeadtaskMap.get(tsk.whoid).Status = 'Contacted';
}
if(LeadtaskMap.get(tsk.whoid).Description == null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportuntity') {
tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
}
Else {
LeadtaskMap.get(tsk.whoid).Status = 'Qualified';
}
}
}
List<Lead> leadList = LeadtaskMap.values();
update leadList;
}
- Wes McCarthy
- December 15, 2015
- Like
- 0
How can i set a value on a mapped record?
I have code that creates a mapping from a Lead record. if i want to set values back on the Lead record, how can i do this? The code i am using is below, however, nothing seems to happen. Is this line of code possible?
LeadtaskMap.get(tsk.whoid).Status = 'Contacted';
Any suggestions?
Thanks.
trigger LeadTrigger on Task (before insert, before update)
{
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whoId != null && String.valueOf(t.whoId).startsWith('00Q')==TRUE) //check if the task is associated with a lead
{
leadIds.add(t.whoId);
}//if 2
}//if 1
}//for
map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
for(Task tsk:trigger.new)
{
if(LeadtaskMap.keyset().contains(tsk.whoid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
{
LeadtaskMap.get(tsk.whoid).Status = 'Contacted';
}
if(LeadtaskMap.get(tsk.whoid).Description == null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportuntity')
{
tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
}
Else
{
LeadtaskMap.get(tsk.whoid).Status = 'Qualified';
}
}
}
}
- Wes McCarthy
- December 15, 2015
- Like
- 0
Issue with Lead Task Trigger
I am trying to run a Task trigger on a Lead as follows.
If Task Subject is A, then update the Lead Status upon Closure of Task.
If Task Subject is B, then check value of Description field on Lead.
If null, return an error.
If not null, then update the Status of the Lead upon closure of the Task.
Code is below, however it doesnt seem to be working. Would appreciate any advice.
Many thanks
trigger LeadTrigger on Task (before insert, before update)
{
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whoId != null && String.valueOf(t.whoId).startsWith('00Q')==TRUE) //check if the task is associated with a lead
{
leadIds.add(t.whoId);
}//if 2
}//if 1
}//for
map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
for(Task tsk:trigger.new)
{
if(LeadtaskMap.keyset().contains(tsk.whatid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
{
LeadtaskMap.get(tsk.whatid).Status = 'Contacted';
}
if(LeadtaskMap.get(tsk.whatid).Description == null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportuntity')
{
tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
}
Else
{
LeadtaskMap.get(tsk.whatid).Status = 'Qualified';
}
}
}
- Wes McCarthy
- December 14, 2015
- Like
- 0
Help with Test Class Coverage
Im writing a test class to cover a task trigger. I have 73% code coverage currently. The lines of code that i havent written a Test for are below, as im unsure how to write Tests for this. Im very new to APEX. Any help is much appreciated. Many thanks.
List<Id> OppIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whatId != null && String.valueOf(t.whatId).startsWith('006')==TRUE) //check if the task is associated with an Opp
{
OppIds.add(t.whatId);
//System.debug('In here:' + t.whatid);
}//if 2
}//if 1
}//for
map<id,Opportunity> OpptaskMap = new map<id,Opportunity>([SELECT Id, Situation__c, Forecast_Delivery_Start_Date__c, Forecast_Delivery_End_Date__c, Bypass_Validation_Rule__c, Problem__c, Implication__c, Need_Payoff__c , Involves_Active_Partners__c, Google_Drive_URL__c, StageName, (Select ID From OpportunityLineItems) FROM Opportunity WHERE Id IN :OppIds]);
List<OpportunityContactRole> OCR = [select id from OpportunityContactRole where OpportunityId IN :oppIds];
List<OpportunityPartner> OPartner = [select id from OpportunityPartner where OpportunityId in :oppIds];
- Wes McCarthy
- May 30, 2016
- Like
- 0
Test Class Coverage @ 73% - please help
Im having issues getting my code coverage up to 75% for a Task Trigger. Please see Trigger and Test Class below. My APEX skills are very basic. Any advice on how to boost my coverage is much appreciated. Thanks in advance!!
trigger TaskTrigger on Task (before insert, before update)
{
List<Id> OppIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whatId != null && String.valueOf(t.whatId).startsWith('006')==TRUE) //check if the task is associated with an Opp
{
OppIds.add(t.whatId);
//System.debug('In here:' + t.whatid);
}//if 2
}//if 1
}//for
map<id,Opportunity> OpptaskMap = new map<id,Opportunity>([SELECT Id, Situation__c, Forecast_Delivery_Start_Date__c, Forecast_Delivery_End_Date__c, Bypass_Validation_Rule__c, Problem__c, Implication__c, Need_Payoff__c , Involves_Active_Partners__c, Google_Drive_URL__c, StageName, (Select ID From OpportunityLineItems) FROM Opportunity WHERE Id IN :OppIds]);
List<OpportunityContactRole> OCR = [select id from OpportunityContactRole where OpportunityId IN :oppIds];
List<OpportunityPartner> OPartner = [select id from OpportunityPartner where OpportunityId in :oppIds];
for(Task tsk:trigger.new)
{
if(OpptaskMap.keyset().contains(tsk.whatid))
{
OpptaskMap.get(tsk.whatid).Bypass_Validation_Rule__c = TRUE;
if(tsk.Status == 'Completed' && tsk.Subject == 'Communicate with Client')
{
OpptaskMap.get(tsk.whatid).StageName = '2-Identified';
//System.debug('Communicate with Client:' + OpptaskMap.get(tsk.whatid).StageName);
}
Else if( tsk.Status == 'Completed' && tsk.Subject == 'Validate Opportunity')
{
If (OpptaskMap.get(tsk.whatid).Situation__c == Null || OpptaskMap.get(tsk.whatid).Problem__c == Null || OpptaskMap.get(tsk.whatid).Implication__c == Null || OpptaskMap.get(tsk.whatid).Need_Payoff__c == Null || OpptaskMap.get(tsk.whatid).Involves_Active_Partners__c == Null)
{
tsk.adderror('Task cannot be completed until the following Opportunity fields have been populated: Situation, Problem, Implication, Need & PayOff, Involves Active Partners.');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '3-Validated';
}
}
// System.debug('1 condition:' + LeadtaskMap.get(tsk.whatid).Description == NULL);
// System.debug('2 condition:' + tsk.Status == 'Completed');
// System.debug('3 condition:' + tsk.Subject == 'Make contact with Lead');
// System.debug('task:' + tsk);
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportunity and Identify Contact Roles')
{
If(OpptaskMap.get(tsk.whatid).Forecast_Delivery_Start_Date__c==Null || OpptaskMap.get(tsk.whatid).Forecast_Delivery_End_Date__c==Null)
{
tsk.adderror('Please add the Forecast Delivery Start and End Dates to the "Delivery Details" section of the Opportunity');
}
If(OpptaskMap.get(tsk.whatid).Involves_Active_Partners__c == 'Yes')
{
If (OpptaskMap.get(tsk.whatid).OpportunityLineItems.isEmpty() || OCR.size()==0 || OPartner.size()==0)
{
tsk.adderror('Task cannot be completed until Contact Roles, Products AND Partners have been added to the Opportunity');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '4-Qualified';
}
}
Else If(OpptaskMap.get(tsk.whatid).Involves_Active_Partners__c == 'No')
{
If (OpptaskMap.get(tsk.whatid).OpportunityLineItems.isEmpty() || OCR.size()==0)
{
tsk.adderror('Task cannot be completed until Contact Roles AND Products have been added to the Opportunity');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '4-Qualified';
}
}
Else
{
//OpptaskMap.get(tsk.whatid).StageName = '4-Qualified';
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Create quote and submit for approval')
{
If(OpptaskMap.get(tsk.whatid).Google_Drive_URL__c==Null)
{
tsk.adderror('Task cannot be completed until associated Google Drive URL has been added to the Opportunity');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '5-Proposal';
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Proposal Outcome')
{
If (tsk.Proposal_Outcome__c == 'Commercial Agreement')
{
OpptaskMap.get(tsk.whatid).StageName = '6-Commercial Agreement';
}
Else If (tsk.Proposal_Outcome__c == 'Lost')
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '9-Lost';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
Else
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '8-Abandoned';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Opportunity Outcome')
{
If (tsk.Opportunity_Outcome__c == 'Won')
{
OpptaskMap.get(tsk.whatid).StageName = '7-Won';
}
Else If (tsk.Opportunity_Outcome__c == 'Lost')
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '9-Lost';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
Else
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '8-Abandoned';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
}
Else
{
//OpptaskMap.get(tsk.whatid).StageName = '7-Won';
}
}
}
List<Opportunity> OppList = OpptaskMap.values();
update OppList;
}
- Wes McCarthy
- May 25, 2016
- Like
- 0
How to setup validation that only allows a User to update an Opp Stage via Task Workflow
Hi there,
I have an issue that im hoping somebody can help me with re: the Sales process.
I currently have implemented a trigger that will update the Stage field of an Opportunity, based on completing Opportunity tasks. Once the first task is completed, the stage gets updated, and a new task in the workflow is generated....and so on.
The issue is that i cant make the Stage field read-only, as it is a standard object field. Meaning the Sales reps can completely ignore the workflow tasks, and manually update the Stage field.
Ideally i'd like to have some sort of validation that prevents Users from updating the stage field maually, and only allows it to happen via completion of an Opportunity task. Unfortunately, i cant create a validation rule that allows this flexibility.
Does anybody have any suggestions? Any help is much appreciated!
Thanks!
- Wes McCarthy
- May 04, 2016
- Like
- 0
Help with Test Coverage
Hi there,
Im relatively new to APEX coding, and have been working on a Task trigger in my sandbox environment. All appears to be working ok there. I deployed the trigger to production, but cannot implement it as i do not have any test coverage. I have no idea how to do this, and am hoping somebody can provide some help. The trigger is as follows:
trigger TaskTrigger on Task (before insert, before update)
{
List<Id> OppIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whatId != null && String.valueOf(t.whatId).startsWith('006')==TRUE) //check if the task is associated with an Opp
{
OppIds.add(t.whatId);
//System.debug('In here:' + t.whatid);
}//if 2
}//if 1
}//for
map<id,Opportunity> OpptaskMap = new map<id,Opportunity>([SELECT Id, Situation__c, Problem__c, Implication__c, Need_Payoff__c , StageName, (Select ID From OpportunityLineItems) FROM Opportunity WHERE Id IN :OppIds]);
List<OpportunityContactRole> OCR = [select id from OpportunityContactRole where OpportunityId IN :oppIds];
for(Task tsk:trigger.new)
{
if(OpptaskMap.keyset().contains(tsk.whatid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Communicate with Client')
{
OpptaskMap.get(tsk.whatid).StageName = '2-Identified';
//System.debug('Communicate with Client:' + OpptaskMap.get(tsk.whatid).StageName);
}
Else if( tsk.Status == 'Completed' && tsk.Subject == 'Validate Opportunity')
{
If (OpptaskMap.get(tsk.whatid).Situation__c == Null || OpptaskMap.get(tsk.whatid).Problem__c == Null || OpptaskMap.get(tsk.whatid).Implication__c == Null || OpptaskMap.get(tsk.whatid).Need_Payoff__c == Null)
{
tsk.adderror('Task cannot be completed until the following Opportunity fields have been populated: Situation, Problem, Implication, Need & PayOff');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '3-Validated';
}
}
// System.debug('1 condition:' + LeadtaskMap.get(tsk.whatid).Description == NULL);
// System.debug('2 condition:' + tsk.Status == 'Completed');
// System.debug('3 condition:' + tsk.Subject == 'Make contact with Lead');
// System.debug('task:' + tsk);
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportunity and Identify Contact Roles')
{
If (OpptaskMap.get(tsk.whatid).OpportunityLineItems.isEmpty() || OCR.size()==0)
{
tsk.adderror('Task cannot be completed until Contact Roles AND Products have been added to the Opportunity');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '4-Qualified';
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Create quote and submit to Client')
{
OpptaskMap.get(tsk.whatid).StageName = '5-Proposal';
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Proposal Outcome')
{
If (tsk.Proposal_Outcome__c == 'Commercial Agreement')
{
OpptaskMap.get(tsk.whatid).StageName = '6-Commercial Agreement';
}
Else If (tsk.Proposal_Outcome__c == 'Lost')
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '9-Lost';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
Else
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '8-Abandoned';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
}
Else if(tsk.Status == 'Completed' && tsk.Subject == 'Opportunity Outcome')
{
If (tsk.Opportunity_Outcome__c == 'Won')
{
OpptaskMap.get(tsk.whatid).StageName = '7-Won';
}
Else If (tsk.Opportunity_Outcome__c == 'Lost')
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '9-Lost';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
Else
{
If (tsk.Loss_Abandonment_Reason__c == Null)
{
tsk.adderror('Please enter a reason for Loss / Abandonment');
}
Else
{
OpptaskMap.get(tsk.whatid).StageName = '8-Abandoned';
OpptaskMap.get(tsk.whatid).Opportunity_Loss_Abandoned_Reason__c = tsk.Loss_Abandonment_Reason__c;
OpptaskMap.get(tsk.whatid).Opp_Other_Loss_Abandonment_Comments__c = tsk.Other_Loss_Abandonment_Comments__c;
}
}
}
Else
{
//OpptaskMap.get(tsk.whatid).StageName = '7-Won';
}
}
}
List<Opportunity> OppList = OpptaskMap.values();
update OppList;
}
Any help is much appreciated.
Thanks in advance.
Wes
- Wes McCarthy
- January 12, 2016
- Like
- 0
Task trigger updating Lead record incorrectly.
If Task is "A" and Status = Completed
Update Lead Status to 'Contacted.'
If Task is "B" and Status = Completed and Lead Description field is Null
Throw an error - 'Task cannot be completed while description is Null.
Else
Update Lead Status to 'Qualified'.
The issue i've encountered is that when i complete Task "A", the Lead Status directly updates to 'Qualified'. When i complete Task "B" and Lead Description is Null, nothing happens. I dont receive an error message. My code is below.
Any suggestions are very welcome.
Thank you.
trigger LeadTrigger on Task (before insert, before update) {
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new) {
if(t.Status=='Completed') {
if(t.whoId != null && String.valueOf(t.whoId).startsWith('00Q')==TRUE) //check if the task is associated with a lead {
leadIds.add(t.whoId);
}//if 2
}//if 1
}//for
map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
for(Task tsk:trigger.new){
if(LeadtaskMap.keyset().contains(tsk.whoid)) {
if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead') {
LeadtaskMap.get(tsk.whoid).Status = 'Contacted';
}
if(LeadtaskMap.get(tsk.whoid).Description == null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportuntity') {
tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
}
Else {
LeadtaskMap.get(tsk.whoid).Status = 'Qualified';
}
}
}
List<Lead> leadList = LeadtaskMap.values();
update leadList;
}
- Wes McCarthy
- December 15, 2015
- Like
- 0
Issue with Lead Task Trigger
I am trying to run a Task trigger on a Lead as follows.
If Task Subject is A, then update the Lead Status upon Closure of Task.
If Task Subject is B, then check value of Description field on Lead.
If null, return an error.
If not null, then update the Status of the Lead upon closure of the Task.
Code is below, however it doesnt seem to be working. Would appreciate any advice.
Many thanks
trigger LeadTrigger on Task (before insert, before update)
{
List<Id> leadIds=new List<Id>();
for(Task t:trigger.new)
{
if(t.Status=='Completed')
{
if(t.whoId != null && String.valueOf(t.whoId).startsWith('00Q')==TRUE) //check if the task is associated with a lead
{
leadIds.add(t.whoId);
}//if 2
}//if 1
}//for
map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
for(Task tsk:trigger.new)
{
if(LeadtaskMap.keyset().contains(tsk.whatid))
{
if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
{
LeadtaskMap.get(tsk.whatid).Status = 'Contacted';
}
if(LeadtaskMap.get(tsk.whatid).Description == null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportuntity')
{
tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
}
Else
{
LeadtaskMap.get(tsk.whatid).Status = 'Qualified';
}
}
}
- Wes McCarthy
- December 14, 2015
- Like
- 0