• HotCopper_Sales
  • NEWBIE
  • 20 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 4
    Replies
Can Salesforce be customised so that fields that appear a different colour based on a condition (Eg date in the past)?
Also can fields be created to be links to a specific record?

I am trying to replicate similar Opportunity to Management to ConnectWise softare. (screenshot attached)


User-added image
 
I have created a trigger that 

IF an activity is created or updated
 AND that task is related to an opportunity
then fill in 3 custom fields of that related opportunity (next action, next action date, next action id) with the earliest due activity details. 

I have a problem though. 
If i mark an activity as complete, i run in to an error because the SQQL query will return nothing. 

How can i optimize my code so that 
IF there are NO activities associated with that opportunity (No results from SQQL query) then do something else?

 
trigger TaskUpdateTrigger on Task (after insert, after update) {
  
    // When a task inserted/updated
   	For (Task t: Trigger.new){
           
            // if task is related to an opportunity
            String relatedOppID = String.valueOf(t.whatId); 
            if(relatedOppID.substring(0,3)=='006'){
                            
                // get all tasks associated with that opportunity
                Task[] relatedTasks = [SELECT Id, Subject, ActivityDate FROM Task 
                                       WHERE WhatId=:t.WhatId
                                       AND ActivityDate != null
                                       AND Status != 'Completed'
                                       ORDER BY ActivityDate ASC
                                       LIMIT 1];
				               
                // get the related opportunity 
                Opportunity relatedOpp = [SELECT Id, NextStep FROM Opportunity
                                          WHERE Id =:t.WhatId];
                
             
                // update Opportunity SObject with Next Action details
                relatedOpp.Next_Action__c = relatedTasks[0].Subject;
                relatedOpp.Next_Action_Date__c = relatedTasks[0].ActivityDate;
                relatedOpp.Next_Action_ID__c = relatedTasks[0].Id;
                                 
                // update database
                update relatedOpp;    
            }
    }  
}

 
Please critique my code. I am very new at Apex and am not a programmer. 

The object of this code is to update the Opportunity Next Action field with the earliest associated activity. 

Maybe i could have put a join in the SOQL query but i am not confident with that yet. 
 
trigger TaskUpdateTrigger on Task (after insert, after update) {
  
    // When a task inserted/updated
   	For (Task t: Trigger.new){
        
       	// check if task is related to an opportunity
       	String relatedID = String.valueOf(t.whatId); 
        if(relatedID.substring(0,3)=='006'){
            
            // get all tasks associated with that opportunity
            Task[] relatedTasks = [SELECT Id, Subject, ActivityDate FROM Task 
                                   WHERE WhatId=:t.WhatId
                                   AND ActivityDate != null
                                   AND Status != 'Completed'
                                   ORDER BY ActivityDate ASC
                                   LIMIT 1];
            
            // get the related opportunity 
            Opportunity relatedOpp = [SELECT Id, NextStep FROM Opportunity
                                      WHERE Id =:t.WhatId];
            
            // format the string -date very short + next activity
            Datetime fullDateTime = relatedTasks[0].ActivityDate;
            String formatedDate = '[' + fullDateTime.format('d/M') + '] ';
            String NextStepString = String.valueOf(formatedDate + relatedTasks[0].Subject);
            
            // update Opportunity SObject
            relatedOpp.NextStep = NextStepString;
			// update database
            update relatedOpp;          
        }
    }  
}

 
I would to create a trigger. I am very new and am having trouble. 

Could somebody please tell me if this is correct?
 
trigger TaskUpdateTrigger on Task (before insert) {
    For (Task t: Trigger.new){
        if(t.what.type == 'Opportunity'){
            System.debug(t.Subject+ 'is related to Opportunity');
        }
    } 
    
}

 
I have created a trigger that 

IF an activity is created or updated
 AND that task is related to an opportunity
then fill in 3 custom fields of that related opportunity (next action, next action date, next action id) with the earliest due activity details. 

I have a problem though. 
If i mark an activity as complete, i run in to an error because the SQQL query will return nothing. 

How can i optimize my code so that 
IF there are NO activities associated with that opportunity (No results from SQQL query) then do something else?

 
trigger TaskUpdateTrigger on Task (after insert, after update) {
  
    // When a task inserted/updated
   	For (Task t: Trigger.new){
           
            // if task is related to an opportunity
            String relatedOppID = String.valueOf(t.whatId); 
            if(relatedOppID.substring(0,3)=='006'){
                            
                // get all tasks associated with that opportunity
                Task[] relatedTasks = [SELECT Id, Subject, ActivityDate FROM Task 
                                       WHERE WhatId=:t.WhatId
                                       AND ActivityDate != null
                                       AND Status != 'Completed'
                                       ORDER BY ActivityDate ASC
                                       LIMIT 1];
				               
                // get the related opportunity 
                Opportunity relatedOpp = [SELECT Id, NextStep FROM Opportunity
                                          WHERE Id =:t.WhatId];
                
             
                // update Opportunity SObject with Next Action details
                relatedOpp.Next_Action__c = relatedTasks[0].Subject;
                relatedOpp.Next_Action_Date__c = relatedTasks[0].ActivityDate;
                relatedOpp.Next_Action_ID__c = relatedTasks[0].Id;
                                 
                // update database
                update relatedOpp;    
            }
    }  
}

 
Please critique my code. I am very new at Apex and am not a programmer. 

The object of this code is to update the Opportunity Next Action field with the earliest associated activity. 

Maybe i could have put a join in the SOQL query but i am not confident with that yet. 
 
trigger TaskUpdateTrigger on Task (after insert, after update) {
  
    // When a task inserted/updated
   	For (Task t: Trigger.new){
        
       	// check if task is related to an opportunity
       	String relatedID = String.valueOf(t.whatId); 
        if(relatedID.substring(0,3)=='006'){
            
            // get all tasks associated with that opportunity
            Task[] relatedTasks = [SELECT Id, Subject, ActivityDate FROM Task 
                                   WHERE WhatId=:t.WhatId
                                   AND ActivityDate != null
                                   AND Status != 'Completed'
                                   ORDER BY ActivityDate ASC
                                   LIMIT 1];
            
            // get the related opportunity 
            Opportunity relatedOpp = [SELECT Id, NextStep FROM Opportunity
                                      WHERE Id =:t.WhatId];
            
            // format the string -date very short + next activity
            Datetime fullDateTime = relatedTasks[0].ActivityDate;
            String formatedDate = '[' + fullDateTime.format('d/M') + '] ';
            String NextStepString = String.valueOf(formatedDate + relatedTasks[0].Subject);
            
            // update Opportunity SObject
            relatedOpp.NextStep = NextStepString;
			// update database
            update relatedOpp;          
        }
    }  
}

 
I would to create a trigger. I am very new and am having trouble. 

Could somebody please tell me if this is correct?
 
trigger TaskUpdateTrigger on Task (before insert) {
    For (Task t: Trigger.new){
        if(t.what.type == 'Opportunity'){
            System.debug(t.Subject+ 'is related to Opportunity');
        }
    } 
    
}