• Brandon Wermes
  • NEWBIE
  • 40 Points
  • Member since 2014
  • DOO
  • Focus POS California


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 8
    Questions
  • 4
    Replies
Let me preface this post by saying that I am very new to Apex and somewhat need to OOP, so my apologies if this is a complete rookie post.

I am working on a trigger surrounding Work Orders (parent) and Work Order Line Items (child). Specifically, I want the status of the Work Order to change from it's current status to "Completed" once all Work Order Line Items on the Work Order are status = "Completed".

To accomplish this, I am counting the total number of Work Order Line Items on that Work Order, then I am counting the number of Work Order Line Items that have a status of "Completed". If the numbers match, I am performing the edit on the Work Order.
Code:
trigger WorkOrderLinesComplete on WorkOrderLineItem (after update) {
    for(WorkOrderLineItem woli : trigger.new){
        if (woli.Status == 'Completed'){
            integer completedCount = [Select count() from workorderlineitem where WorkOrderId = :woli.WorkOrderId and Status = 'Completed'];
            integer totalCount = [Select count() from workorderlineitem where WorkOrderId = :woli.WorkOrderId];
            if (completedCount == totalCount){
                WorkOrder wo = [Select id,status from WorkOrder where id = :woli.WorkOrderId];
                if (wo.Status != 'Completed'){
                    wo.status = 'Completed';
                    update wo;
                }
            }
        }
    }
}

Please let me know if this is acceptable, and if not any suggestions on improving.

Thanks as always!
Good evening,

Could any of you fine folks provide some assistance? I have a trigger where I need to find the Object that the ParentId of a Chatter post belongs to. This is intended to be used on the Case Feed to identify FeedItem posts made by a specific user. I've got all of the components of the trigger down except being able to validate that the ParentId of the FeedItem is in the Case object.

My code is below:
 
List<feeditem> FIList = [Select Id,ParentId
                                                 	FROM feeditem fi
                                              		WHERE fi.CreatedById =: '005G0000003qm9v' and ??????????????]

any suggestions please?
Hello all,

I am working to write a trigger that will change the owner of a case to the current user when the case status is changed.

Using trigger code found on this forum (credit to jasonkor) I am able to successfully change the case owner when the status is changed. However, since the Trigger is updating the case owner IF the status is equal to In Progress, anytime the case is edited the owner will change.

Ideally, I'd like the owner to be changed only IF the status is edited (use case = multiple users will work a case, but whoever changes status is the new owner).

Here is the trigger:
trigger CaseOwnerUpdate on Case (before update) {

    if (Trigger.new.size() == 1) {  
        // if a Case has been updated to In Progress
        if (Trigger.new[0].Status == 'In Progress') {  
            // Change Trigger.old[0].OwnerId to Current User ID
            Trigger.new[0].OwnerId = UserInfo.getUserId();
        }
    }
}

I realize that the fault lies in the 
if (Trigger.new[0].Status == 'In Progress') {

line, but as I am new to Apex and mostly a point & click admin, I am unsure how to write the trigger to active the owner change only when the case status is changed.

Help is greatly appreciated.

Thank you!
Hello,

I am working to build a trigger that updates an Opportunity when a Task is added to the opportunity. The trigger is successful in the Sandbox, however my Apex Class to test the trigger is presenting 0% code coverage. I am new to Apex and more of a "Desktop Admin" so any help would be appreciated.

The Trigger:
trigger OptyUpdateonTaskInsert on Task (after insert, after update)
{
    List<Opportunity> lstOpty = new List<Opportunity>();
    for(Task T : Trigger.New)
    {
        if(T.WhatId != null && string.valueof(T.WhatId).startsWith('006'))
        {
            Opportunity opt = new Opportunity(id=T.WhatId);
            lstOpty.add(opt);
        }
    }
    
    update lstOpty;
}

The Class:
 
@isTest(seealldata=true)
public class testTask{
    private static testmethod void testTaskActivity(){
        
        Opportunity o = new Opportunity(
        				AccountId 	= 	'0015500000AByHM',
            			Name 		=	'Test 2',
            			StageName	=	'Proposal Review',
            			CloseDate	=	(System.now()).date(),
            			Type		=	'New System');
        insert o;
                   
        Task t = new Task(
            			WhatId 		= 	o.Id, 
                        Subject 	= 	'Sample Email', 
                        Priority 	= 	'Normal', 
                        Status 		= 	'Completed',
            			OwnerId 	=	UserInfo.getUserId());
        insert t;

    }
}

 
Hello,

I am working to build a trigger that updates an Opportunity when a Task is added to the opportunity. The trigger is successful in the Sandbox, however my Apex Class to test the trigger is presenting 0% code coverage. I am new to Apex and more of a "Desktop Admin" so any help would be appreciated.

The Trigger:
trigger OptyUpdateonTaskInsert on Task (after insert, after update)
{
    List<Opportunity> lstOpty = new List<Opportunity>();
    for(Task T : Trigger.New)
    {
        if(T.WhatId != null && string.valueof(T.WhatId).startsWith('006'))
        {
            Opportunity opt = new Opportunity(id=T.WhatId);
            lstOpty.add(opt);
        }
    }
    
    update lstOpty;
}
The Class:
 
@isTest(seealldata=true)
public class testTask{
    private static testmethod void testTaskActivity(){
        
        Opportunity o = new Opportunity(
        				AccountId 	= 	'0015500000AByHM',
            			Name 		=	'Test 2',
            			StageName	=	'Proposal Review',
            			CloseDate	=	(System.now()).date(),
            			Type		=	'New System');
        insert o;
                   
        Task t = new Task(
            			WhatId 		= 	o.Id, 
                        Subject 	= 	'Sample Email', 
                        Priority 	= 	'Normal', 
                        Status 		= 	'Completed',
            			OwnerId 	=	UserInfo.getUserId());
        insert t;

    }
}

Thank you very much for assistance.
 
Hello all,

I have a fairly simple trigger. When a CaseComment is created, the trigger creates a new FeedItem using the CommentBody as the Body of the FeedItem and the ParentId of the CaseComment as the FeedItem.Parent Id. The trigger executes as expected & designed in Sandbox, however when I run my Test Class to obtain Code Coverage, I'm getting 0%. Trigger & Class below. Any help is appreciated!


Trigger
trigger CaseCommentToFeed on CaseComment (after insert) {
    List<FeedItem> updates = new List<FeedItem>();
    for(CaseComment cc : trigger.new){
		if (cc.ParentId.getSObjectType() == Case.SObjectType) {
			updates.add(new FeedItem(
            	ParentId 		= 	cc.ParentId,
           		CreatedById 	= 	cc.CreatedById,
	          	Title 			= 	'New Comment Post!',
           	 	Body 			= 	cc.CommentBody
			));
             
      }
      insert updates;
	}
}

Test Class
@isTest(SeeAllData=true)
    public class    TestCaseInsert    {
        static testMethod    void    insertNewCase()    {
           
            Case caseToCreate      =    new Case();
            	caseToCreate.Status    =    'New';
            	caseToCreate.Priority  =    'Normal';
            	caseToCreate.Origin    =    'Phone';
            	caseToCreate.Subject   =    'Subject Here';
            	caseToCreate.Description =   'Description Here';
            
            insert caseToCreate;
            
            CaseComment commentToCreate		=	new CaseComment();
            	commentToCreate.CommentBody	=	'This is the body';
            	commentToCreate.ParentId	=	caseToCreate.Id;
            
            insert commentToCreate;
            
    }
}

Thank you!
Hello all,

I have a trigger that one of y'all helped me build (many thanks!). The trigger works perfectly to change the case status when a FeedItem is posted witha  Parent ID of a case where the feed body ends with "Case closed".
As noted, the trigger works fine in the Sandbox, however my Apex Class used for testing is failing due to the following error:
System.QueryException: List has no rows for assignment to SObject

The Apex Trigger is:
trigger UpdateCaseCloseStatus on FeedItem (after insert) {
    
    List<Case> listOfCases = new List<Case>();
    
    for (FeedItem fi : Trigger.new) {
        
        //--- This retrieves the case for which feed has been entered.
        Case c = [select id from Case where Id = :fi.ParentId];

        //--- Now we check if the feed ends with text 'Case Closed'. We are checking case insensitive text.
        if (fi.Body.endsWithIgnoreCase('Case Closed')) {
            c.Status = 'Complete';
        }
        listOfCases.add(c);
    }
    update listOfCases;
}

The Apex Class is:
@isTest
public class TestFeedPost    {
    static testMethod void insertNewFeedPost()     {
        
        FeedItem feedToCreate = new FeedItem();
        
        feedToCreate.ParentId    =       '500q0000001Pavd';
        feedToCreate.Body        =       'Blah blah blah close case';
        feedToCreate.Type        =        'TextPost';
        
        insert feedToCreate;
        }
        }


The full error message is:
<span unselectable="on" "="" style="display: block; padding: 3px 4px; overflow: hidden; margin-left: 0px; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; white-space: nowrap; background-color: rgb(218, 240, 249);">
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateCaseCloseStatus: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.UpdateCaseCloseStatus: line 8, column 1: []



Any assistance would be greatly appreciated.

Thanks!
First off, I'm extremely new to Apex (and programming in general). My company uses Service Cloud and Case Feed quite heavily.
One of the functions that we want to implement is the ability for a Case Feed user to enter "Case Closed" at the end of a Case Feed post and then have a Trigger automatically update the status of the case (marking the case "Complete").

Here is what I have so far:

trigger UpdateCaseCloseStatus on FeedItem (after insert) {
    List<Case> updates = new List<Case>();
    for (FeedItem fi : Trigger.new) {
        if (fi.ParentId.getSObjectType() == Case.SObjectType){
            }
            else if (fi.Body == 'Case closed'){
                updates.add(new Case(
                    Id = fi.ParentId,
                    Status = 'Complete'
                    ));
        }
    }
    update updates;
    }

The Trigger saves, however when entering "Case Closed" into a feed post, the case does not change to "Complete".

Any help is greatly appreciated!
Hello,

I am working to build a trigger that updates an Opportunity when a Task is added to the opportunity. The trigger is successful in the Sandbox, however my Apex Class to test the trigger is presenting 0% code coverage. I am new to Apex and more of a "Desktop Admin" so any help would be appreciated.

The Trigger:
trigger OptyUpdateonTaskInsert on Task (after insert, after update)
{
    List<Opportunity> lstOpty = new List<Opportunity>();
    for(Task T : Trigger.New)
    {
        if(T.WhatId != null && string.valueof(T.WhatId).startsWith('006'))
        {
            Opportunity opt = new Opportunity(id=T.WhatId);
            lstOpty.add(opt);
        }
    }
    
    update lstOpty;
}

The Class:
 
@isTest(seealldata=true)
public class testTask{
    private static testmethod void testTaskActivity(){
        
        Opportunity o = new Opportunity(
        				AccountId 	= 	'0015500000AByHM',
            			Name 		=	'Test 2',
            			StageName	=	'Proposal Review',
            			CloseDate	=	(System.now()).date(),
            			Type		=	'New System');
        insert o;
                   
        Task t = new Task(
            			WhatId 		= 	o.Id, 
                        Subject 	= 	'Sample Email', 
                        Priority 	= 	'Normal', 
                        Status 		= 	'Completed',
            			OwnerId 	=	UserInfo.getUserId());
        insert t;

    }
}

 
Hello all,

I have a fairly simple trigger. When a CaseComment is created, the trigger creates a new FeedItem using the CommentBody as the Body of the FeedItem and the ParentId of the CaseComment as the FeedItem.Parent Id. The trigger executes as expected & designed in Sandbox, however when I run my Test Class to obtain Code Coverage, I'm getting 0%. Trigger & Class below. Any help is appreciated!


Trigger
trigger CaseCommentToFeed on CaseComment (after insert) {
    List<FeedItem> updates = new List<FeedItem>();
    for(CaseComment cc : trigger.new){
		if (cc.ParentId.getSObjectType() == Case.SObjectType) {
			updates.add(new FeedItem(
            	ParentId 		= 	cc.ParentId,
           		CreatedById 	= 	cc.CreatedById,
	          	Title 			= 	'New Comment Post!',
           	 	Body 			= 	cc.CommentBody
			));
             
      }
      insert updates;
	}
}

Test Class
@isTest(SeeAllData=true)
    public class    TestCaseInsert    {
        static testMethod    void    insertNewCase()    {
           
            Case caseToCreate      =    new Case();
            	caseToCreate.Status    =    'New';
            	caseToCreate.Priority  =    'Normal';
            	caseToCreate.Origin    =    'Phone';
            	caseToCreate.Subject   =    'Subject Here';
            	caseToCreate.Description =   'Description Here';
            
            insert caseToCreate;
            
            CaseComment commentToCreate		=	new CaseComment();
            	commentToCreate.CommentBody	=	'This is the body';
            	commentToCreate.ParentId	=	caseToCreate.Id;
            
            insert commentToCreate;
            
    }
}

Thank you!
Hello all,

I have a trigger that one of y'all helped me build (many thanks!). The trigger works perfectly to change the case status when a FeedItem is posted witha  Parent ID of a case where the feed body ends with "Case closed".
As noted, the trigger works fine in the Sandbox, however my Apex Class used for testing is failing due to the following error:
System.QueryException: List has no rows for assignment to SObject

The Apex Trigger is:
trigger UpdateCaseCloseStatus on FeedItem (after insert) {
    
    List<Case> listOfCases = new List<Case>();
    
    for (FeedItem fi : Trigger.new) {
        
        //--- This retrieves the case for which feed has been entered.
        Case c = [select id from Case where Id = :fi.ParentId];

        //--- Now we check if the feed ends with text 'Case Closed'. We are checking case insensitive text.
        if (fi.Body.endsWithIgnoreCase('Case Closed')) {
            c.Status = 'Complete';
        }
        listOfCases.add(c);
    }
    update listOfCases;
}

The Apex Class is:
@isTest
public class TestFeedPost    {
    static testMethod void insertNewFeedPost()     {
        
        FeedItem feedToCreate = new FeedItem();
        
        feedToCreate.ParentId    =       '500q0000001Pavd';
        feedToCreate.Body        =       'Blah blah blah close case';
        feedToCreate.Type        =        'TextPost';
        
        insert feedToCreate;
        }
        }


The full error message is:
<span unselectable="on" "="" style="display: block; padding: 3px 4px; overflow: hidden; margin-left: 0px; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; white-space: nowrap; background-color: rgb(218, 240, 249);">
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateCaseCloseStatus: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.UpdateCaseCloseStatus: line 8, column 1: []



Any assistance would be greatly appreciated.

Thanks!
First off, I'm extremely new to Apex (and programming in general). My company uses Service Cloud and Case Feed quite heavily.
One of the functions that we want to implement is the ability for a Case Feed user to enter "Case Closed" at the end of a Case Feed post and then have a Trigger automatically update the status of the case (marking the case "Complete").

Here is what I have so far:

trigger UpdateCaseCloseStatus on FeedItem (after insert) {
    List<Case> updates = new List<Case>();
    for (FeedItem fi : Trigger.new) {
        if (fi.ParentId.getSObjectType() == Case.SObjectType){
            }
            else if (fi.Body == 'Case closed'){
                updates.add(new Case(
                    Id = fi.ParentId,
                    Status = 'Complete'
                    ));
        }
    }
    update updates;
    }

The Trigger saves, however when entering "Case Closed" into a feed post, the case does not change to "Complete".

Any help is greatly appreciated!