+ Start a Discussion
chandra prakash 58chandra prakash 58 

Urjent Need a Trigger on custom object ( target__c)

I have an  cutom object(target__c) in this object we have three field Month__c(picklist), remainigquantity__c(num), baklogquantity__c(num) .
If in January month target has some remaining quantity. thin when I will go to create the February month target. the remaining quantity of January month will come in backlog quantity in February month.

need to write a trigger. please help ASAP
 
Malika Pathak 9Malika Pathak 9
Hi chandra prakash 58,
please find the solution

trigger TargetTrigger on Target__c (After insert) {
    if(Trigger.isAfter && Trigger.isInsert){
        TargetTriggerHandler.targetInsert(Trigger.New);
    }
}
public class TargetTriggerHandler {
    Public static void targetInsert(List<Target__c> targetList){
        try{
            List<Target__c> target_List=new List<Target__c>();
            for(Target__c tarObj:targetList){
                if(tarObj.Month__c=='Jan' && tarObj.remainigquantity__c != null){
                    Target__c tar=new Target__c();
                    tar.Month__c='Feb';
                    tar.baklogquantity__c=tarObj.remainigquantity__c;
                    target_List.add(tar);
                }
            }
            if(!target_List.isEmpty()){
                insert target_List;
                system.debug('target_List-->'+target_List);
            }
        }
        catch(Exception e){
            system.debug('error is =>'+e.getMessage()+' at line =>'+e.getLineNumber());
        }
    }
}

Please upvote it and mark as best answer if this will help you,so that other can take reference from here.
Thanks