• Kumar David
  • NEWBIE
  • 20 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 5
    Replies
I need to put validation on Task so that Task cannot be created for Accounts other than "Sold to Customer" and "Prospect Customer". Currently Task can be created even for "Bill to Customer". There is no option to create a validation rule with task hence I need to have Before insert and before update Trigger. (Type is a picklist in Account that has value: Sold to Customer, Prospect Customer, Bill to Customer)

trigger TaskCanbeCreatedforSoldtoOnly on Task (before insert, before update) {
    for (Task t: Trigger.new){
         if(t.Account.Type== 'Bill To Customer') {
                     t.addError ('Please select the Sold to Customer');
        }
    }
}

Thank you
I have trigger which is working but I need to update it and it is giving me an error ( only bold one is newly added)

I had two fields and based on the combination it was working but now I need to add another checbox called Active__c and if current condition and if Active__c is true then only trigger the trigger. It is giving me "Variable does not exist" error on Line 28

trigger Triggerupdatedirectbill on FX5__Ticket_Item__c (before insert, before update ) {
    if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate) ) {
        List<FX5__Ticket_Item__c> items = Trigger.new;
        Set<String> accountsIds = new Set<String>();
        Set<String> wellsIds = new Set<String>();
        for(FX5__Ticket_Item__c item:items) {
            if(item.Customerforlogiconly__c!= null) {
               accountsIds.add(item.Customerforlogiconly__c);
               System.debug(accountsIds);
            }
            if(item.Disposal_Location_on_Ticket_item__c!= null) {
                wellsIds.add(item.Disposal_Location_on_Ticket_item__c);
                 System.debug(wellsIds);
            }
        }
        Map<String, Obj_Direct_Bill_Customer__c> directInvoicesMap = new Map<String, Obj_Direct_Bill_Customer__c>();
        for(Obj_Direct_Bill_Customer__c di :[Select Direct_Bill_Customer__c, Disposal_Location__c, Active__c from Obj_Direct_Bill_Customer__c where Direct_Bill_Customer__c in :accountsIds and Disposal_Location__c in :wellsIds] ) {
            if(!directInvoicesMap.containsKey(di.Direct_Bill_Customer__c+'_'+di.Disposal_Location__c) ) {
                directInvoicesMap.put(di.Direct_Bill_Customer__c+'_'+di.Disposal_Location__c,di);
            }
            System.debug (di.direct_Bill_Customer__c);
            System.debug (di.Disposal_Location__c);
            System.debug (di.Direct_Bill_Customer__c+'_'+di.Disposal_Location__c);          
        }
        for(FX5__Ticket_Item__c item:items)       
         {
         System.debug (item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c);
           if(directInvoicesMap.containsKey(item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c) && di.Active__c==true){
                item.Direct_Bill_Customer__c = True;
                  }
            else {
                item.Direct_Bill_Customer__c = False;
            }
                
        }
        
    }
}
 
I need to put validation on Task so that Task cannot be created for Accounts other than "Sold to Customer" and "Prospect Customer". Currently Task can be created even for "Bill to Customer". There is no option to create a validation rule with task hence I need to have Before insert and before update Trigger. (Type is a picklist in Account that has value: Sold to Customer, Prospect Customer, Bill to Customer)

trigger TaskCanbeCreatedforSoldtoOnly on Task (before insert, before update) {
    for (Task t: Trigger.new){
         if(t.Account.Type== 'Bill To Customer') {
                     t.addError ('Please select the Sold to Customer');
        }
    }
}

Thank you
I have trigger which is working but I need to update it and it is giving me an error ( only bold one is newly added)

I had two fields and based on the combination it was working but now I need to add another checbox called Active__c and if current condition and if Active__c is true then only trigger the trigger. It is giving me "Variable does not exist" error on Line 28

trigger Triggerupdatedirectbill on FX5__Ticket_Item__c (before insert, before update ) {
    if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate) ) {
        List<FX5__Ticket_Item__c> items = Trigger.new;
        Set<String> accountsIds = new Set<String>();
        Set<String> wellsIds = new Set<String>();
        for(FX5__Ticket_Item__c item:items) {
            if(item.Customerforlogiconly__c!= null) {
               accountsIds.add(item.Customerforlogiconly__c);
               System.debug(accountsIds);
            }
            if(item.Disposal_Location_on_Ticket_item__c!= null) {
                wellsIds.add(item.Disposal_Location_on_Ticket_item__c);
                 System.debug(wellsIds);
            }
        }
        Map<String, Obj_Direct_Bill_Customer__c> directInvoicesMap = new Map<String, Obj_Direct_Bill_Customer__c>();
        for(Obj_Direct_Bill_Customer__c di :[Select Direct_Bill_Customer__c, Disposal_Location__c, Active__c from Obj_Direct_Bill_Customer__c where Direct_Bill_Customer__c in :accountsIds and Disposal_Location__c in :wellsIds] ) {
            if(!directInvoicesMap.containsKey(di.Direct_Bill_Customer__c+'_'+di.Disposal_Location__c) ) {
                directInvoicesMap.put(di.Direct_Bill_Customer__c+'_'+di.Disposal_Location__c,di);
            }
            System.debug (di.direct_Bill_Customer__c);
            System.debug (di.Disposal_Location__c);
            System.debug (di.Direct_Bill_Customer__c+'_'+di.Disposal_Location__c);          
        }
        for(FX5__Ticket_Item__c item:items)       
         {
         System.debug (item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c);
           if(directInvoicesMap.containsKey(item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c) && di.Active__c==true){
                item.Direct_Bill_Customer__c = True;
                  }
            else {
                item.Direct_Bill_Customer__c = False;
            }
                
        }
        
    }
}