function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BennettTranBennettTran 

Trigger to create task

Hi All,

 

I am new to apex coding but took a shot at writing an opportunity trigger to create a case based off of certain criteria.  The issue I am running into is that a requirement of the trigger is to create a task instead of a case if a field on the associated account called "Closed"(checkbox) is not checked.  I am not sure how to code that logic, currently I have the trigger creating a case if the field "Closed" is checked but not sure how to do it the other way around.  The trigger I have written is below and any help would be greatly appreciated.

 

trigger ImplementationCaseforIntegrationProducts on Opportunity (after insert, after update) {
string recordtype = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Implementation').getRecordTypeId();
/* List<Task> tasks = new List<Task>();*/
List<Case> cases = new List<Case>();
for (Opportunity opp: Trigger.New)
{ case newcase= new case();
if((trigger.isInsert && (opp.StageName.toLowerCase().equals('closed won'))&&((opp.Type.toLowerCase().equals('adjustment'))|| (opp.Type.toLowerCase().equals('additional sales')))&& opp.Integration_Product_Attached__c == True)||
(trigger.isUpdate && (opp.StageName != Trigger.oldMap.get(opp.Id).StageName && opp.StageName.toLowerCase().equals('closed won'))&&((opp.Type.toLowerCase().equals('additional sales'))||(opp.Type.toLowerCase().equals('adjustment'))) && opp.Integration_Product_Attached__c == True))
{
list<Account> acc = [select id, Closed__c from Account where ID=:opp.AccountId];
if (acc[0].Closed__c == TRUE)
{
system.debug('----------opp.AccountId---->'+opp.AccountId);
newcase.AccountId=opp.AccountId;
newcase.Opportunity__c=opp.Id;
newcase.Subject='New Implementation case for '+opp.Account_Name__c;
newcase.Status='New';
newcase.Origin='Sign Up Form';
newcase.Priority='Medium';
newcase.RecordTypeId=recordtype;
newcase.Billing_Email__c = opp.Billing_Email__c;
cases.add(newcase);
}

}
else
{
}
}

if(cases.size()!=0 && cases.size()!=null)
{
system.debug('----------cases.size()---->'+cases.size());
insert cases;
}

 

Thanks in advance!

Best Answer chosen by Admin (Salesforce Developers) 
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Bennettran,

 

I have modified your code to insert Tasks as well as Cases based on the Closed__c checkbox conditions that apply.

Try the following trigger now.

 

trigger ImplementationCaseforIntegrationProducts on Opportunity (after insert, after update) {

    string recordtype = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Implementation').getRecordTypeId();    
    List<Case> cases = new List<Case>();
    List<Task> tasksList = new List<Task>();
    
    for (Opportunity opp: Trigger.New){
      case newcase= new case();
      if(opp.AccountId != null){
            Account a = [select id,name,Closed__c from Account where id =: opp.AccountId limit 1];  //This line more than enough to check in common, because if account id in opportunity is not null means only one value going to be there.
            
            if((trigger.isInsert && (opp.StageName.toLowerCase().equals('closed won'))&&((opp.Type.toLowerCase().equals('adjustment'))|| (opp.Type.toLowerCase().equals('additional sales')))&& opp.Integration_Product_Attached__c == True)||
                (trigger.isUpdate && (opp.StageName != Trigger.oldMap.get(opp.Id).StageName && opp.StageName.toLowerCase().equals('closed won'))&&((opp.Type.toLowerCase().equals('additional sales'))||(opp.Type.toLowerCase().equals('adjustment'))) && opp.Integration_Product_Attached__c == True))
             {
                    if (a.Closed__c == TRUE){
                        newcase.AccountId=opp.AccountId;
                        newcase.Opportunity__c=opp.Id;
                        newcase.Subject='New Implementation case for '+opp.Account_Name__c;
                        newcase.Status='New';
                        newcase.Origin='Sign Up Form';
                        newcase.Priority='Medium';
                        newcase.RecordTypeId=recordtype;
                        newcase.Billing_Email__c = opp.Billing_Email__c;
                        cases.add(newcase);
                   }
                   else{
                       user u = [select id,name from user where id = UserInfo.getUserId() limit 1]; //select user to give as owner of task
                       Task t = new Task(ownerId = u.id, Subject = 'Follow up', status = 'Not Started',
                                          Priority = 'High', whatID = opp.id);  //You can change values accordingly
                       taskList.add(t);
                   }
              }
          }        
      }   
    
    if(cases.size() >  0){
        insert cases;
    }
    if(taskList.size() > 0){
       insert taskList;
    }
}

 

Hope so this helps you...!

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.