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
TeddyAbleTeddyAble 

Task Trigger IF Statement for Validation (HELP)

Hello Guys,

 

Any suggestion for solution to this problem will be highly apreciated.

 

What we are going for is IF a task is created and it meets the following Field criteria in Task (Standard Object) then Run trigger which creates an  Interaction (Custom Object)

 

Related To Picklist Equals Adviser

 

[Related To is the Field label for Task, the Field Name is  What]

 

and im confused as to how i can write the trigger to Create an Interaction record during insert Only if the Related To picklist "Adviser" is selected else just save the task and do not create anything.

 

 

The code below is the Trigger that is suppose to be run Only when the Task meets the criteria

 

But the task gets created but doesnt not run the trigger hence the record is not created in the custom Object (Interaction)

 

 ========================================================================

trigger TaskCreate2 on Task (after Insert)
    {
       List<Interaction__c> INTA = new List<Interaction__c>();
      
       ID RecordTypeofInteraction=[SELECT Id,Name FROM RecordType WHERE Name LIKE '07-Sales Process'].id;
        
       for(Task TC :Trigger.new)
            
           {
               if (TC.what.name == 'Adviser')
               {
                   
               INTA.add(new Interaction__c (Subject__c=TC.subject,
                                            Contact__c=TC.whoID,
                                            points__c = TC.point__c,
                                            adviser__c=TC.WhatID,
                                            Log_Interaction_Details__c=TC.description,
                                            recordTypeID = '012Q00000000TqN',
                                            Interaction_Type__c='Sales Activity'));
                  
               }
              
               
    
               
           
           }
         Insert INTA;
                
    }

 =======================================================================

 

Please Let me know what im doing wrong, Because i m confused on how to call the Related To field in Task  so that If statement can execute this trigger if the user select the Related To "Adviser" Adviser is a custom Object Adviser__C

 

Please Help ..

 

 

Regards,


Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,
Try the below code as reference and made changes accordingly:


trigger TaskCreate2 on Task (after Insert)
{
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map<String, Schema.DescribeSObjectResult> descMap=new Map<String, Schema.DescribeSObjectResult>();
for(Schema.SObjectType sot : schemaMap.values())
{
Schema.DescribeSObjectResult descRes=sot.getDescribe();
String kp=descRes.getKeyPrefix();
descMap.put(kp, descRes);
}
Task T = Trigger.New[0];
String str = T.whatId;
String ObjName = '';
String prefix='';
Schema.DescribeSObjectResult descObj;
if(str != NULL)
{
prefix=str.substring(0,3);
descObj=descMap.get(prefix);
}
if(descObj != null)
ObjName = descObj.getName();
List<Interaction__c> INTA = new List<Interaction__c>();
ID RecordTypeofInteraction=[SELECT Id,Name FROM RecordType WHERE Name LIKE '07-Sales Process'].id;
for(Task TC :Trigger.new)
{
system.debug('#################' +ObjName );
if (ObjName == 'Adviser__c')
{
INTA.add(new Interaction__c (Subject__c=TC.subject,
Contact__c=TC.whoID,
points__c = TC.point__c,
adviser__c=TC.WhatID,
Log_Interaction_Details__c=TC.description,
recordTypeID = '012Q00000000TqN',
Interaction_Type__c='Sales Activity'));

}
}
Insert INTA;

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,
Try the below code as reference and made changes accordingly:


trigger TaskCreate2 on Task (after Insert)
{
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map<String, Schema.DescribeSObjectResult> descMap=new Map<String, Schema.DescribeSObjectResult>();
for(Schema.SObjectType sot : schemaMap.values())
{
Schema.DescribeSObjectResult descRes=sot.getDescribe();
String kp=descRes.getKeyPrefix();
descMap.put(kp, descRes);
}
Task T = Trigger.New[0];
String str = T.whatId;
String ObjName = '';
String prefix='';
Schema.DescribeSObjectResult descObj;
if(str != NULL)
{
prefix=str.substring(0,3);
descObj=descMap.get(prefix);
}
if(descObj != null)
ObjName = descObj.getName();
List<Interaction__c> INTA = new List<Interaction__c>();
ID RecordTypeofInteraction=[SELECT Id,Name FROM RecordType WHERE Name LIKE '07-Sales Process'].id;
for(Task TC :Trigger.new)
{
system.debug('#################' +ObjName );
if (ObjName == 'Adviser__c')
{
INTA.add(new Interaction__c (Subject__c=TC.subject,
Contact__c=TC.whoID,
points__c = TC.point__c,
adviser__c=TC.WhatID,
Log_Interaction_Details__c=TC.description,
recordTypeID = '012Q00000000TqN',
Interaction_Type__c='Sales Activity'));

}
}
Insert INTA;

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
TeddyAbleTeddyAble

Hello Navatar,

 

Thank you very much for your detailed help.

 

Very much apreciated,

 

Cannot say thank you enough.

 

Regards,

 

TeddyAbleTeddyAble

Hello Navatar_

 

It comes up with an error message when a new task is created and the Object Organisation is selected... Seems like it only prefers the Adviser to be chosen for some weird reason.

 


Review all error messages below to correct your data.
Apex trigger TaskCreateAdviser caused an unexpected exception, contact your administrator: TaskCreateAdviser: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Adviser: id value of incorrect type: a0RQ0000002GybVMAS: [Adviser__c]: Trigger.TaskCreateAdviser: line 63, column 1

 

I extended the code to include additional IF Statement  of other Custom Object within the Picklist.

 

 

trigger TaskCreateAdviser on Task (after Insert)
    {
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map<String, Schema.DescribeSObjectResult> descMap=new Map<String, Schema.DescribeSObjectResult>();
        
        
        for(Schema.SObjectType sot : schemaMap.values())
    {
        
        Schema.DescribeSObjectResult descRes=sot.getDescribe();
        String kp=descRes.getKeyPrefix();
        descMap.put(kp, descRes);
        }
            Task T = Trigger.New[0];
            String str = T.whatId;
            String ObjName = '';
            String prefix='';
            Schema.DescribeSObjectResult descObj;
            if(str != NULL)
                    {
                        prefix=str.substring(0,3);
                        descObj=descMap.get(prefix);
                    }
        
            if(descObj != null)
                ObjName = descObj.getName();
                List<Interaction__c> INTA = new List<Interaction__c>();
        
                ID RecordTypeofInteraction=[SELECT Id,Name FROM RecordType WHERE Name LIKE '07-Sales Process'].id;
        
    for(Task TC :Trigger.new)
            {
                system.debug('#################' +ObjName );
                
            if (ObjName == 'Adviser__c')
                {
                    INTA.add(new Interaction__c (Subject__c=TC.subject,
                                                Contact__c=TC.whoID,
                                                points__c = TC.point__c,
                                                adviser__c=TC.WhatID,
                                                Log_Interaction_Details__c=TC.description,
                                                recordTypeID = '012Q00000000TqN',
                                                Interaction_Type__c='Sales Activity'));

                }
                
                else {
                
                If (ObjName == 'Organisation__c')
                {
                    INTA.add(new Interaction__c (Subject__c=TC.subject,
                                                Contact__c=TC.whoID,
                                                points__c = TC.point__c,
                                                adviser__c=TC.WhatID,
                                                Log_Interaction_Details__c=TC.description,
                                                recordTypeID = '012Q00000000TqN',
                                                Interaction_Type__c='Sales Activity'));

                }
            }
        
    }
        Insert INTA;

    }

 

 

Please advice on what im doing wrong.

 

Regards,

Ted

TeddyAbleTeddyAble

Hello Navatar_

 

figured it out thanks.

 

for some stupid reason i  was calling the Adviser__c in the wrong section instead of Organisation__c

 

else {
                
                If (ObjName == 'Organisation__c')
                {
                    INTA.add(new Interaction__c (Subject__c=TC.subject,
                                                Contact__c=TC.whoID,
                                                points__c = TC.point__c,
                                                Organisation__c=TC.WhatID,
                                                Log_Interaction_Details__c=TC.description,
                                                recordTypeID = '012Q00000000TqN',
                                                Interaction_Type__c='Sales Activity'));

                }
            }

 

 

Thanks

 

 

I hope this is useful for others.