• Mike Simmen
  • NEWBIE
  • 15 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies
Hello,

I am having issues getting the workitemid.  Please help.  I am trying to recall an approval process whenever a user cancels the request from the object.  Here is my code.
 
trigger FlightRequestRecallwhencancelled on Demo_Request__c (after update)
{

    for (Integer i = 0; i < Trigger.new.size(); i++)
    {
     try
     {
        if(Trigger.old[i].Cancel_this_Request__c != Trigger.new[i].Cancel_this_Request__c)
        {
           recallRecord(Trigger.new[i]);
        }
     }catch(Exception e)
     {
         Trigger.new[i].addError(e.getMessage());
     }
    }
    
    /**
    * Get ProcessInstanceWorkItemId using SOQL
    **/
    public Id getWorkItemId(Id targetObjectId)
    {
        Id retVal = null;

        for(ProcessInstanceWorkitem workItem  : [Select p.Id from ProcessInstanceWorkitem p
            where p.ProcessInstance.TargetObjectId =: targetObjectId])
        {
            retVal  =  workItem.Id;
        }

        return retVal;
    }

    /**
    * This method will Recall the Flight Request Approval
    **/
    public void recallRecord(Demo_Request__c d)
    {
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Approval Recalled due to Flight Request Cancellation');
        req.setAction('Removed');
        Id workItemId = getWorkItemId(d.id); 

        if(workItemId == null)
        {
            d.addError('Recall Approval Error');
        }
        else
        {
            req.setWorkitemId(workItemId);
            // Submit the request for approval
            Approval.ProcessResult result =  Approval.process(req);
        }
    }
}

 
Hello all,

I have been working on writing an apex trigger to update an account date field when a task that meets certain criteria is entered.  The code works great when you have a whatID.  The problem though is when you use email to salesforce, the emails are only attached to the whoID and leaves the whatID null.  I need to find a way to modify my code to look at the whoID and determine what account it is associated with and then update that account.  I need some help.  Take a look and let me know your thoughts.  Thanks.

trigger updateAccountEngagement on Task (before insert, before update) {
    
    List<Id> accountIds=new List<Id>();
    for(Task t:trigger.new){
        if(t.Status=='Completed'){
            if(t.whatId != null && (String.valueOf(t.whatId).startsWith('001')==TRUE || String.valueOf(t.whatId).startsWith('006')==TRUE) && (String.valueOf(t.subject).startsWith('Email: Re:')==TRUE || String.valueOf(t.subject).startsWith('Email: Fw:')==TRUE || t.Engagement__c==TRUE)){//check if the task is associated with an account
                accountIds.add(t.whatId);
            }//if 2
        }//if 1
    }//for
    List<Account> accountsToUpdate=[SELECT Id, Last_Engagement_Date__c FROM Account WHERE Id IN :accountIds];
    For (account a:accountsToUpdate){
        a.Last_Engagement_Date__c=date.today();
    }//for
    
    try{
        update accountsToUpdate;
    }catch(DMLException e){
        system.debug('Accounts were not all properly updated.  Error: '+e);
    }
}//trigger
I am somewhat new to Apex Triggers, but I have been working on a way to update the account associated with an opportunity when the stage is updated and a second requirement is met, the closed date (standard opportunity field) is greater than the prospecting date (custom account field).  Here is my code.  The problem is nothing happens at all and I don't see why.  Could you please help.  Thanks.

trigger updateAccountProspectStatus on opportunity (after insert, after update){
    list<Id> accIds = new list<Id>();
    list<Account> accounts = new list<account>();
    for(opportunity o:trigger.new){
        accIds.add(o.accountId);
    }
    for(account a:[select Id, Prospecting_Date__c, Prospecting_Status__c, Prospecting_Target__c from account where Id IN :accIds]){
        for(opportunity opp:trigger.new){
            if(opp.CloseDate >a.Prospecting_Date__c){
                if(opp.StageName == 'Qualification/Interest'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Aircraft Info Sent'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Qualification/Interest'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Demo Pending'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Product Presentation Complete-L450/L500'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Demo Completed'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Business Points Negotiated'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Proposal Submitted'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Closed Won'){
                   a.Prospecting_Status__c = 'Opportunity Closed Won';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Closed Lost'){
                   a.Prospecting_Status__c = 'Opportunity Closed Lost';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Closed Drop'){
                   a.Prospecting_Status__c = 'Opportunity Closed Drop';
                   a.Prospecting_Target__c = FALSE;
                }
            }else{
               a.Prospecting_Status__c = NULL;
    }
    }
    }
    update accounts;
}
Hello all,

I have been working on writing an apex trigger to update an account date field when a task that meets certain criteria is entered.  The code works great when you have a whatID.  The problem though is when you use email to salesforce, the emails are only attached to the whoID and leaves the whatID null.  I need to find a way to modify my code to look at the whoID and determine what account it is associated with and then update that account.  I need some help.  Take a look and let me know your thoughts.  Thanks.

trigger updateAccountEngagement on Task (before insert, before update) {
    
    List<Id> accountIds=new List<Id>();
    for(Task t:trigger.new){
        if(t.Status=='Completed'){
            if(t.whatId != null && (String.valueOf(t.whatId).startsWith('001')==TRUE || String.valueOf(t.whatId).startsWith('006')==TRUE) && (String.valueOf(t.subject).startsWith('Email: Re:')==TRUE || String.valueOf(t.subject).startsWith('Email: Fw:')==TRUE || t.Engagement__c==TRUE)){//check if the task is associated with an account
                accountIds.add(t.whatId);
            }//if 2
        }//if 1
    }//for
    List<Account> accountsToUpdate=[SELECT Id, Last_Engagement_Date__c FROM Account WHERE Id IN :accountIds];
    For (account a:accountsToUpdate){
        a.Last_Engagement_Date__c=date.today();
    }//for
    
    try{
        update accountsToUpdate;
    }catch(DMLException e){
        system.debug('Accounts were not all properly updated.  Error: '+e);
    }
}//trigger