• Shaun B.
  • NEWBIE
  • 50 Points
  • Member since 2016
  • System Admin
  • defi SOLUTIONS

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 19
    Replies
I'm a newbie for APEX and I'm trying to redeploy a fix for a former APEX trigger we had already deployed to production.  When I try to deploy it, I get it shows Code Coverage as 64% (9/14) and won't let me deploy.  Can someone please let me know what I'm doing wrong?

Apex Trigger:
trigger UpdatePricing on OpportunityLineItem (before insert, before update) {


Map<String, List<Volume_Discount__c>> PricingMap = new Map<String, List<Volume_Discount__c>>();
for(Volume_Discount__c  P : [SELECT ID,Minimum_Quantity__c,Product__c,Price_Book__c  FROM Volume_Discount__c ORDER BY Minimum_Quantity__c])
{
  if(PricingMap.KeySet().contains(P.Product__c+'|'+P.Price_Book__c))
  {   
     PricingMap.get(P.Product__c+'|'+P.Price_Book__c).add(P);
  }
  else
  {
     PricingMap.put(P.Product__c+'|'+P.Price_Book__c,new List<Volume_Discount__c> {P});
  }
}


for(OpportunityLineItem  oli : Trigger.new)
{
oli.Volume_Discount__c = null;
if(PricingMap.KeySet().contains(oli.Product2Id+'|'+oli.Opportuity_PriceBook2__c) && oli.ProductAndPriceBookActive__c)
{
    for(Volume_Discount__c P : PricingMap.get(oli.Product2Id+'|'+oli.Opportuity_PriceBook2__c))
    {
        if(oli.quantity >= P.Minimum_Quantity__c)
        {
           oli.Volume_Discount__c = P.ID;
        }
    }
}

}


}

Apex Class:
@isTest
Public class UpdatePricingTest{


Public static testmethod void testing(){
integer def = 0;
list<OpportunityLineitem  > olitoupdate = new list<OpportunityLineitem  >();
for(OpportunityLineitem  oli : [SELECT ID,Quantity from OpportunityLineItem  order by createddate desc LIMIT 5]){
OLI.Quantity = def +200;
olitoupdate.add(OLI);
}
if(olitoupdate.size()>0){
update olitoupdate;
}


PriceBook2 PB = new PriceBook2();
PB.Name = 'New Pricebook';
PB.isactive = true;
insert PB;

Product2 pro = new Product2();
pro.Name = 'Prod';
pro.Billing_Type__c = 'Financed';
insert pro;


insert new PriceBookEntry(Product2Id=pro.Id, Pricebook2Id=Test.getStandardPricebookId(), UnitPrice=0);


PricebookEntry  PBE = new PricebookEntry();
PBE.Product2ID = Pro.ID;
PBE.PriceBook2ID = PB.ID;
PBE.UnitPrice = 120;
PBE.Isactive = true;
insert PBE;


Volume_Discount__c Pricing = new Volume_Discount__c();
Pricing.Minimum_Quantity__c = 100;
Pricing.Product__c = Pro.ID;
Pricing.Price_Book__c  = PB.ID;
Pricing.Annual_Overage__c = 2.5;
Pricing.Annual_Price__c = 2.5;
Pricing.Quarterly_Overage__c = 2.5;
Pricing.Quarterly_Price__c = 2.5;
Pricing.Monthly_Overage__c = 2.5;
Pricing.Monthly_Price__c = 2.5;
insert Pricing;

Account acc = new Account(Name = 'Test Account1');
acc.BillingState = 'TX';
acc.AccountSource = 'Inbound Call';
acc.Industry = 'Bank';
insert acc;

Opportunity Opp = new Opportunity();
Opp.Name = 'Test Opp';
Opp.StageName = 'Interview';
Opp.PriceBook2ID = PB.ID;
Opp.CloseDate = Date.TODAY()-10;
Opp.AccountID = acc.ID;
Opp.Testing__c = true;
insert Opp;

OpportunityLineItem OLI = new OpportunityLineItem();
OLI.OpportunityID = Opp.ID;
OLI.PriceBookEntryID = PBE.ID;
OLI.Quantity = 100;
OLI.Volume_Discount__c = Pricing.ID;
insert OLI;

OLI.Quantity = 99;
update OLI;

}

}

 
Hi,

I am new to APEX and have modified a trigger to fit my needs, but don't know how to properly modify the test class.  Could someone please help?

APEX Trigger:
trigger JIRATrigger on JIRA__c (after insert, after update) {
    for (JIRA__c j : Trigger.new) {
        if (Trigger.isInsert && Trigger.isAfter && j.Project__c == 'Arkham' && j.Issue_Type__c == 'Bug') {
            JCFS.API.createJiraIssue('10103', '10103');
        }
        if (Trigger.isInsert && Trigger.isAfter && j.Project__c == 'Arkham' && j.Issue_Type__c == 'Story') {
            JCFS.API.createJiraIssue('10103', '10100');
        }
        if (Trigger.isInsert && Trigger.isAfter && j.Project__c == 'Arkham' && j.Issue_Type__c == 'Epic') {
            JCFS.API.createJiraIssue('10103', '10000');
        }
        if (Trigger.isInsert && Trigger.isAfter && j.Project__c == 'Harvey' && j.Issue_Type__c == 'Bug') {
            JCFS.API.createJiraIssue('10300', '10103');
        }
        if (Trigger.isInsert && Trigger.isAfter && j.Project__c == 'Harvey' && j.Issue_Type__c == 'Story') {
            JCFS.API.createJiraIssue('10300', '10100');
        }
        if (Trigger.isInsert && Trigger.isAfter && j.Project__c == 'Harvey' && j.Issue_Type__c == 'Epic') {
            JCFS.API.createJiraIssue('10300', '10000');
        }
        if (Trigger.isUpdate && Trigger.isAfter) {
            JCFS.API.pushUpdatesToJira();
        }
    }
}

Current APEX Class: (0% code coverage)
@isTest public class JIRATriggerTest {
    @isTest static void caseAfterInsertTest() {
        JCFS.JiraTriggerTestHelper.testAfterInsert('JIRA__c');
    }
    @isTest static void caseAfterUpdateTest() {
        JCFS.JiraTriggerTestHelper.testAfterUpdate('JIRA__c');
    }
}

 
Hi Developer World!

I have the following Apex code that I need to write an Apex Test Class for.  Unfortunately for me I am not very good at Apex.  :(  Any help would be greatly appreciated!!
 
trigger UpdateFeedItemFields on FeedComment(after insert, after update){
    List<Case> updates = new List<case>();
    List<id> userList = new List<ID>();
    List<Id> feedItemList = new List<id>();
    for(FeedComment fc: trigger.new){
        feedItemList.add(fc.FeedItemId);
        userList.add(fc.InsertedById);
    }
    Map<Id, FeedItem> feedMap = new Map<id, FeedItem>([select id,InsertedById,Visibility from feedItem where Id IN :feedItemList]);
    Map<Id, User> userMap = new Map<Id, User>([select id, usertype, name from user where ID IN :userList]);
    for(FeedComment fc: trigger.new){
        if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType && userMap.get(fc.InsertedById).userType != 'CPSLitePortal') {
            updates.add(new Case(
                    Id = fc.ParentId,
                    Last_Internal_Comment__c = fc.CommentBody
                    ));
        }
        else if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType && userMap.get(fc.InsertedById).userType == 'CPSLitePortal') {
            updates.add(new Case(
                    Id = fc.ParentId,
                    Last_External_Comment__c = fc.CommentBody
                    ));
        }
    }
    if(updates != null && updates.size() > 0)
    update updates;
}

 
Hello (Developer) World!

I wrote (with the help of the community!) the following Apex code to update a case field anytime someone posts to the case feed.  The issue I have now is that if someone replies to the feed comment instead of the feed item, my fields don't update.  Can someone help me to modify or add to the below to achieve my desired result?
 
trigger UpdateCaseCommentFields on FeedItem (after insert, after update) {
    List<Case> updates = new List<Case>();
    for (FeedItem fi : Trigger.new) {
        if (fi.ParentId.getSObjectType() == Case.SObjectType && fi.Visibility == 'InternalUsers') {
            updates.add(new Case(
                    Id = fi.ParentId,
                    Last_Internal_Comment__c = fi.Body
                    ));
        }
        else if (fi.ParentId.getSObjectType() == Case.SObjectType && fi.Visibility != 'InternalUsers') {
            updates.add(new Case(
                    Id = fi.ParentId,
                    Last_External_Comment__c = fi.Body
                    ));
        }
    }
    update updates;
}
Hi there!

I am new to Apex, but would like to deploy the following Apex code to my org.  The issue however is that I don't know how to write unit tests.  Can anyone assist?
 
trigger UpdateCaseCommentFields on FeedItem (after insert) {
    List<Case> updates = new List<Case>();
    for (FeedItem fi : Trigger.new) {
        if (fi.ParentId.getSObjectType() == Case.SObjectType && fi.Visibility == 'InternalUsers') {
            updates.add(new Case(
                    Id = fi.ParentId,
                    Last_Internal_Comment__c = fi.Body
                    ));
        }
        else if (fi.ParentId.getSObjectType() == Case.SObjectType && fi.Visibility != 'InternalUsers') {
            updates.add(new Case(
                    Id = fi.ParentId,
                    Last_External_Comment__c = fi.Body
                    ));
        }
    }
    update updates;
}

 
I'm a newbie for APEX and I'm trying to redeploy a fix for a former APEX trigger we had already deployed to production.  When I try to deploy it, I get it shows Code Coverage as 64% (9/14) and won't let me deploy.  Can someone please let me know what I'm doing wrong?

Apex Trigger:
trigger UpdatePricing on OpportunityLineItem (before insert, before update) {


Map<String, List<Volume_Discount__c>> PricingMap = new Map<String, List<Volume_Discount__c>>();
for(Volume_Discount__c  P : [SELECT ID,Minimum_Quantity__c,Product__c,Price_Book__c  FROM Volume_Discount__c ORDER BY Minimum_Quantity__c])
{
  if(PricingMap.KeySet().contains(P.Product__c+'|'+P.Price_Book__c))
  {   
     PricingMap.get(P.Product__c+'|'+P.Price_Book__c).add(P);
  }
  else
  {
     PricingMap.put(P.Product__c+'|'+P.Price_Book__c,new List<Volume_Discount__c> {P});
  }
}


for(OpportunityLineItem  oli : Trigger.new)
{
oli.Volume_Discount__c = null;
if(PricingMap.KeySet().contains(oli.Product2Id+'|'+oli.Opportuity_PriceBook2__c) && oli.ProductAndPriceBookActive__c)
{
    for(Volume_Discount__c P : PricingMap.get(oli.Product2Id+'|'+oli.Opportuity_PriceBook2__c))
    {
        if(oli.quantity >= P.Minimum_Quantity__c)
        {
           oli.Volume_Discount__c = P.ID;
        }
    }
}

}


}

Apex Class:
@isTest
Public class UpdatePricingTest{


Public static testmethod void testing(){
integer def = 0;
list<OpportunityLineitem  > olitoupdate = new list<OpportunityLineitem  >();
for(OpportunityLineitem  oli : [SELECT ID,Quantity from OpportunityLineItem  order by createddate desc LIMIT 5]){
OLI.Quantity = def +200;
olitoupdate.add(OLI);
}
if(olitoupdate.size()>0){
update olitoupdate;
}


PriceBook2 PB = new PriceBook2();
PB.Name = 'New Pricebook';
PB.isactive = true;
insert PB;

Product2 pro = new Product2();
pro.Name = 'Prod';
pro.Billing_Type__c = 'Financed';
insert pro;


insert new PriceBookEntry(Product2Id=pro.Id, Pricebook2Id=Test.getStandardPricebookId(), UnitPrice=0);


PricebookEntry  PBE = new PricebookEntry();
PBE.Product2ID = Pro.ID;
PBE.PriceBook2ID = PB.ID;
PBE.UnitPrice = 120;
PBE.Isactive = true;
insert PBE;


Volume_Discount__c Pricing = new Volume_Discount__c();
Pricing.Minimum_Quantity__c = 100;
Pricing.Product__c = Pro.ID;
Pricing.Price_Book__c  = PB.ID;
Pricing.Annual_Overage__c = 2.5;
Pricing.Annual_Price__c = 2.5;
Pricing.Quarterly_Overage__c = 2.5;
Pricing.Quarterly_Price__c = 2.5;
Pricing.Monthly_Overage__c = 2.5;
Pricing.Monthly_Price__c = 2.5;
insert Pricing;

Account acc = new Account(Name = 'Test Account1');
acc.BillingState = 'TX';
acc.AccountSource = 'Inbound Call';
acc.Industry = 'Bank';
insert acc;

Opportunity Opp = new Opportunity();
Opp.Name = 'Test Opp';
Opp.StageName = 'Interview';
Opp.PriceBook2ID = PB.ID;
Opp.CloseDate = Date.TODAY()-10;
Opp.AccountID = acc.ID;
Opp.Testing__c = true;
insert Opp;

OpportunityLineItem OLI = new OpportunityLineItem();
OLI.OpportunityID = Opp.ID;
OLI.PriceBookEntryID = PBE.ID;
OLI.Quantity = 100;
OLI.Volume_Discount__c = Pricing.ID;
insert OLI;

OLI.Quantity = 99;
update OLI;

}

}

 
Hi,

I am new to APEX and have modified a trigger to fit my needs, but don't know how to properly modify the test class.  Could someone please help?

APEX Trigger:
trigger JIRATrigger on JIRA__c (after insert, after update) {
    for (JIRA__c j : Trigger.new) {
        if (Trigger.isInsert && Trigger.isAfter && j.Project__c == 'Arkham' && j.Issue_Type__c == 'Bug') {
            JCFS.API.createJiraIssue('10103', '10103');
        }
        if (Trigger.isInsert && Trigger.isAfter && j.Project__c == 'Arkham' && j.Issue_Type__c == 'Story') {
            JCFS.API.createJiraIssue('10103', '10100');
        }
        if (Trigger.isInsert && Trigger.isAfter && j.Project__c == 'Arkham' && j.Issue_Type__c == 'Epic') {
            JCFS.API.createJiraIssue('10103', '10000');
        }
        if (Trigger.isInsert && Trigger.isAfter && j.Project__c == 'Harvey' && j.Issue_Type__c == 'Bug') {
            JCFS.API.createJiraIssue('10300', '10103');
        }
        if (Trigger.isInsert && Trigger.isAfter && j.Project__c == 'Harvey' && j.Issue_Type__c == 'Story') {
            JCFS.API.createJiraIssue('10300', '10100');
        }
        if (Trigger.isInsert && Trigger.isAfter && j.Project__c == 'Harvey' && j.Issue_Type__c == 'Epic') {
            JCFS.API.createJiraIssue('10300', '10000');
        }
        if (Trigger.isUpdate && Trigger.isAfter) {
            JCFS.API.pushUpdatesToJira();
        }
    }
}

Current APEX Class: (0% code coverage)
@isTest public class JIRATriggerTest {
    @isTest static void caseAfterInsertTest() {
        JCFS.JiraTriggerTestHelper.testAfterInsert('JIRA__c');
    }
    @isTest static void caseAfterUpdateTest() {
        JCFS.JiraTriggerTestHelper.testAfterUpdate('JIRA__c');
    }
}

 
You guys rock!  Hopefully, you can help me with a simple Apex Class and VF page that I'm having trouble getting to work.

It works fine when i try to create one record, but the point of this page is to create multiple records.  Here is the error I am receiving:
Insert failed. First exception on row 0 with id a00410000029Ep0AAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!saveHours}' in component <apex:commandButton> in page addmultiplehours2: Class.AddmultipleHours.saveHours: line 21, column 1

An unexpected error has occurred. Your development organization has been notified.

My Apex Class:
public class AddmultipleHours {
 Hours__c hrs = new Hours__c();

 public list < Hours__c > listHours {
  get;
  set;
 }

 public AddmultipleHours() {
  listHours = new list < Hours__c > ();
  listHours.add(hrs);
 }

 public void addHoursRow() {
  Hours__c hours = new Hours__c();
  listHours.add(hours);
 }
 
 public PageReference saveHours() {
  for (Integer i = 0; i < listHours.size(); i++) {
   insert listHours;}
  
  return Page.AddmultipleHours;
 }

}

My VF Page:
 
<apex:page Controller="AddmultipleHours">
  <apex:form >
    <apex:pageBlock >
      <apex:pageBlockTable value="{!listHours}" var="hours">
        <apex:column headerValue="Client Name">
          <apex:inputField value="{!hours.Client_Name__c}"/>
        </apex:column>
        <apex:column headerValue="Hours">
          <apex:inputField value="{!hours.Hours__c}"/>
        </apex:column>
        <apex:column headerValue="Type">
          <apex:inputField value="{!hours.Type__c}"/>
        </apex:column>
      </apex:pageBlockTable>
      <apex:pageBlockButtons >
        <apex:commandButton value="Add Hours Row" action="{!addHoursRow}"/>
        <apex:commandButton value="Save Hours" action="{!saveHours}"/>
      </apex:pageBlockButtons>
    </apex:pageBlock>
  </apex:form>
</apex:page>

Your help is much appreciated!!
 
Hi Developer World!

I have the following Apex code that I need to write an Apex Test Class for.  Unfortunately for me I am not very good at Apex.  :(  Any help would be greatly appreciated!!
 
trigger UpdateFeedItemFields on FeedComment(after insert, after update){
    List<Case> updates = new List<case>();
    List<id> userList = new List<ID>();
    List<Id> feedItemList = new List<id>();
    for(FeedComment fc: trigger.new){
        feedItemList.add(fc.FeedItemId);
        userList.add(fc.InsertedById);
    }
    Map<Id, FeedItem> feedMap = new Map<id, FeedItem>([select id,InsertedById,Visibility from feedItem where Id IN :feedItemList]);
    Map<Id, User> userMap = new Map<Id, User>([select id, usertype, name from user where ID IN :userList]);
    for(FeedComment fc: trigger.new){
        if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType && userMap.get(fc.InsertedById).userType != 'CPSLitePortal') {
            updates.add(new Case(
                    Id = fc.ParentId,
                    Last_Internal_Comment__c = fc.CommentBody
                    ));
        }
        else if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType && userMap.get(fc.InsertedById).userType == 'CPSLitePortal') {
            updates.add(new Case(
                    Id = fc.ParentId,
                    Last_External_Comment__c = fc.CommentBody
                    ));
        }
    }
    if(updates != null && updates.size() > 0)
    update updates;
}

 
Hello (Developer) World!

I wrote (with the help of the community!) the following Apex code to update a case field anytime someone posts to the case feed.  The issue I have now is that if someone replies to the feed comment instead of the feed item, my fields don't update.  Can someone help me to modify or add to the below to achieve my desired result?
 
trigger UpdateCaseCommentFields on FeedItem (after insert, after update) {
    List<Case> updates = new List<Case>();
    for (FeedItem fi : Trigger.new) {
        if (fi.ParentId.getSObjectType() == Case.SObjectType && fi.Visibility == 'InternalUsers') {
            updates.add(new Case(
                    Id = fi.ParentId,
                    Last_Internal_Comment__c = fi.Body
                    ));
        }
        else if (fi.ParentId.getSObjectType() == Case.SObjectType && fi.Visibility != 'InternalUsers') {
            updates.add(new Case(
                    Id = fi.ParentId,
                    Last_External_Comment__c = fi.Body
                    ));
        }
    }
    update updates;
}
Hi there!

I am new to Apex, but would like to deploy the following Apex code to my org.  The issue however is that I don't know how to write unit tests.  Can anyone assist?
 
trigger UpdateCaseCommentFields on FeedItem (after insert) {
    List<Case> updates = new List<Case>();
    for (FeedItem fi : Trigger.new) {
        if (fi.ParentId.getSObjectType() == Case.SObjectType && fi.Visibility == 'InternalUsers') {
            updates.add(new Case(
                    Id = fi.ParentId,
                    Last_Internal_Comment__c = fi.Body
                    ));
        }
        else if (fi.ParentId.getSObjectType() == Case.SObjectType && fi.Visibility != 'InternalUsers') {
            updates.add(new Case(
                    Id = fi.ParentId,
                    Last_External_Comment__c = fi.Body
                    ));
        }
    }
    update updates;
}

 

I'm trying to create a formula which calculates the business hours (8am - 5pm) between a task being created and closed.

 

I created a custom "ClosedDate" field on the task object.

 

I found an example online: http://help.salesforce.com/apex/HTViewSolution?id=000089863&language=en_US

 

I tested creating a task today (Oct. 28) and closing it with a date of Oct. 30th.  The number of hours shown the formula field displayed 36 hours.  It should only show the business hours between 8am and 5pm and exclude weekends.  Is there a way to adjust the formula in the link above to accommodate those requirements?

 

Thanks for any help!