-3

in account object, if field 'Type' picklist value is "Prospect", log a call action field 'Subject' picklist value must show "Call with" and for other 'Type' picklist values log a call action field 'Subject' picklist value must show "Call ". I write a trigger but its didn't work.  My Trigger is:

trigger tasksubupdate on Account (before insert) {

     List<Task> tas= new List<Task>();
     for(Account a : trigger.new){
         if(a.type=='Gold' && a.Name!=null && a.Id!=null  ){

            tas.add(new Task(whatId=a.Id, Subject='Call With'));
         }
     }
        update tas;
 }

enter image description here

1
  • 2
    This is anyways not going to work because your trigger is before insert and Id value is always going to be null so the code logic is not going to work in any case. Try to change the trigger context and you should insert the task instead of update
    – RCS
    Jun 28, 2017 at 12:23

2 Answers 2

0

Sujay,

Try to change the trigger context in this way and check it once.

trigger PhoneUpdate on Account (before update) {
    Map<id,account> amap = new Map<id,account>();
    for(account acc: Trigger.new) {
     if(acc.type=='Prospect') {
        amap.put(acc.id,acc);
      }
    }
    List<Task> conlist  = [select id,WhatId,Subject from Task where WhatId in: amap.keyset()];
    for(Task c:conlist) {
        if(amap.get(c.WhatId).type=='Prospect')
        {
        c.Subject='Call';
        }
    }
    if(!conlist.isempty()){
        update conlist;
    }
}
2
  • 1
    Answers which are mostly just a block of code generally aren't too helpful. This answer would be improved if you would explain how it is different from the code that the person asking the question (the original poster, or OP) already has, and how you think your code addresses the issues OP is running into.
    – Derek F
    Jun 28, 2017 at 13:06
  • 1
    I also changed the formatting of your code from 'inline code' (surrounded by backticks) to a 'code block' (each line has 4 or more blank spaces at the start of the line). For the future, the "{}" (open and close curly braces) button in the text editing window, or pressing ctrl + k, takes care of that for you. Write your code, highlight it, click that button, and your code is formatted.
    – Derek F
    Jun 28, 2017 at 13:09
0

Like the comments mentioned, using a 'before insert' will not work in your scenario. I made some changes which will hopefully get you on the right track:

//Would it make sense to add a 'after update' as well?
trigger tasksubupdate on Account (after insert) {
    List<Task> tas = new List<Task>();
    for(Account a : trigger.new){
        if(a.type == 'Gold') { //Did you mean to add 'Prospect' here?
            tas.add(new Task(whatId = a.Id, Subject = 'Call With'));
        }
        else {
            tas.add(new Task(whatId = a.Id, Subject = 'Call '));
        }
    }

    update tas;
}

You don't need the Id and Name checks since Account requires these fields to save.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .