• Chinna Sfdc
  • NEWBIE
  • 25 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 3
    Likes Received
  • 0
    Likes Given
  • 20
    Questions
  • 37
    Replies
Hi All,

Facing the issues like getting a System.LimitException: Too many SOQL queries: 101 when trying to update my opportunities. Can someone please help me on this code, so we don't run into this error anymore and how to avoid it again with future Triggers.
Please check my below Trigger and class:
Trigger:
Trigger AccountPartnerType on Account (after update){
    AccountPartnerTypecls acpt = new AccountPartnerTypecls();
    if(AccountPartnerTypecls.Aptcalc)
    acpt.AccountPartnerTypecalc(Trigger.new,trigger.newmap,trigger.oldmap);
}
Class:
public class AccountPartnerTypecls{
                public static Boolean Aptcalc = true;
                public static Boolean Aptcalc1 = false; 
                public static Boolean Aptcalc2 = false;  
                public void AccountPartnerTypecalc(List<Account> aclist,map<id,Account> newmap,map<id,Account> oldmap){
                                set<id> accidset = new set<id>(); 
                                set<string> ulprntidset = new set<string>();   
                                string dulprnt;
                                map<string,List<Account>>  accmap = new  map<string,List<Account>>();
                                system.debug('*********'+aclist.size());           
                                for(Account acc: aclist){
                                                accidset.add(acc.id);
                                                // if(acc.GE_Global_Ultimate_Reporting_Account__c!=Null)  //GE_Digital_Ultimate_Parent_ID__c
                                                //Ultimate_Parent_ID__c
                                                ulprntidset.add(acc.GE_Digital_Ultimate_Parent_ID__c);
                                                dulprnt= acc.GE_Digital_Ultimate_Parent_ID__c;
                                                system.debug('*********'+dulprnt);           
                                }
                                system.debug('*********'+dulprnt); 
                                List<Account> hiracclist = new List<Account>();
                                List<Account> aclist1 = [select id, name,Partner_Status__c,Is_Partner__c,Account_Type__c,Parentid,GE_Digital_Ultimate_Parent_ID__c from Account where  GE_Digital_Ultimate_Parent_ID__c  =:dulprnt  AND Parentid!=null AND id NOT IN:accidset ];
                                system.debug('*********'+aclist1.size()); 
                                for(Account a : aclist1 ){
                                                if(accmap.containskey(a.GE_Digital_Ultimate_Parent_ID__c))
                                                accmap.get(a.GE_Digital_Ultimate_Parent_ID__c).add(a);
                                                else{
                                                                List<Account> alist = new List<Account>();
                                                                alist.add(a);
                                                                accmap.put(a.GE_Digital_Ultimate_Parent_ID__c,alist);
                                                }
                                }
                                for(Account acc: [select id, name,Partner_Status__c,Is_Partner__c,Account_Type__c,Parentid,GE_Digital_Ultimate_Parent_ID__c from Account where ID IN :accidset ]){
                                                string acctype='';
                                                if(accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c)!=null){
                                                                /* if(accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c).contains(acc)){
                                                                accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c).set(0, acc);
                                                                accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c).remove(0);
                                                                }*/
                                                                for(Account acchr : accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c)){
                                                                                //acchr.Is_Partner__c= acc.Is_Partner__c;
                                                                                if(acc.Is_Partner__c)
                                                                                acchr.Account_Type__c = 'Partner';
                                                                                else
                                                                                acchr.Account_Type__c= '';
                                                                                hiracclist.add(acchr);
                                                                }
                                                                if(acc.Is_Partner__c){
                                                                                if(acc.parentid!=null)
                                                                                Aptcalc1 =true;
                                                                                acc.Account_Type__c= 'Partner';
                                                                                hiracclist.add(acc);
                                                                }
                                                                else{
                                                                                if(acc.parentid!=null)
                                                                                Aptcalc2 =true;
                                                                                acc.Account_Type__c= '';
                                                                                hiracclist.add(acc);
                                                                }
                                                }
                                                else{
                                                                if(acc.Is_Partner__c){
                                                                                acc.Account_Type__c= 'Partner';
                                                                                hiracclist.add(acc);
                                                                }
                                                                else{
                                                                                acc.Account_Type__c= ' ';
                                                                                hiracclist.add(acc);
                                                                }
                                                }
                                }
                                Account a=  [select id, name,Partner_Status__c,Is_Partner__c,Account_Type__c,Parentid,GE_Digital_Ultimate_Parent_ID__c from Account where ID  =:dulprnt];
                                if(Aptcalc1){
                                                a.Account_Type__c= 'Partner';
                                                hiracclist.add(a);
                                }
                                else if(Aptcalc2){
                                                a.Account_Type__c= '';
                                                hiracclist.add(a);
                                }
                                Aptcalc = false;         
                                update  hiracclist;
                }
}
 
Thanks
 
Hi All,

We have requirement as mentioend below senario:

How to create/generate a new Opportunity with Related Lists to be populated on new Opportunity from Old Opportunity based on the closure of  ContractEnd date  of a existing opportunity.

Can any one please help us to create a new trigger.

Thanks in Advance
 
Hi All,

We have one requirement when Account having OLD,NEW and OLD&NEW partner accounts that time "License Type" should be automatically changed based on below scenario’s.
We have one Picklist field called "License Type" with the values like 
1. OLD Partner
2. NEW Partner
3. OLD & NEW Partners

Scenarios:

1. If Account "is partner"(Checkbox) is enabled or updated, and any existing accounts in the hierarchy enabled or they all are “OLD Partners" then that time (License Type) picklist value should be automatically set the "OLD Partner".
2. There are no existing accounts in the hierarchy then (License Type) picklist value should be automatically set to "NEW Partners".
3. There are no OLD Partners and there is no "NEW Partner Account" then picklist value should be set the "OLD&NEW".

Please help me to create new trigger for above requirement that would be great help.

Thanks
Hi All,

I want to know bypass the Logic in my Batch apex.Problem is when we are running the Batch apex  some records are updating some are not.Because we have custom settings are firing in my logic.Can any one help us how to bypass the custom setting logic in my Batch apex.Please check my Below Batch Apex Logic which we are using Currently.

global class OpportunityDays implements Database.Batchable<Sobject>{
    Public List<Opportunity> lstOpp;
   
    global Database.QueryLocator start(Database.BatchableContext BC){
      
         String query = 'Select id, Name, StageName, Software_Product_Count__c, CW_in_Last_30_Days_Formula__c, CW_in_Last_30_Days_Workflow__c, CW_in_Last_730_Days_Formula__c, CW_in_Last_730_Days_Workflow__c from Opportunity Where StageName = \'S8- Closed Won\' AND Software_Product_Count__c > 0 AND ( ((CW_in_Last_30_Days_Formula__c = TRUE AND CW_in_Last_30_Days_Workflow__c = False) OR (CW_in_Last_30_Days_Formula__c = False AND CW_in_Last_30_Days_Workflow__c = True)) OR ((CW_in_Last_730_Days_Formula__c = TRUE AND CW_in_Last_730_Days_Workflow__c = False) OR (CW_in_Last_730_Days_Formula__c = False AND CW_in_Last_730_Days_Workflow__c = True )))';
         
        system.debug('START Method...');       
            return Database.getQueryLocator(query);
            }
    
    /* EXECUTE Method */
    global void execute(Database.BatchableContext BC, List<Opportunity> batch){
        system.debug('EXECUTE Method...');    
        lstOpp = new List<Opportunity>();
        for(Opportunity objOpp : batch){
             boolean shouldUpdate = false;
            if(objOpp.CW_in_Last_30_Days_Formula__c == True){
                objOpp.CW_in_Last_30_Days_Workflow__c = True;
                shouldUpdate = true;
                system.debug('CW 30 WF True...'+ objOpp.CW_in_Last_30_Days_Workflow__c);
            }
            else if(objOpp.CW_in_Last_30_Days_Formula__c == False){
                    objOpp.CW_in_Last_30_Days_Workflow__c = False;
                    shouldUpdate = true;
                    system.debug('CW 30 WF False...'+ objOpp.CW_in_Last_30_Days_Workflow__c);
                }
            if(objOpp.CW_in_Last_730_Days_Formula__c == True){
                objOpp.CW_in_Last_730_Days_Workflow__c = True;
                shouldUpdate = true;
                system.debug('CW 730 WF True...'+ objOpp.CW_in_Last_730_Days_Workflow__c);
            }
            else if(objOpp.CW_in_Last_730_Days_Formula__c == False){
                    objOpp.CW_in_Last_730_Days_Workflow__c = False;
                      shouldUpdate = true;
                    system.debug('CW 730 WF False...'+ objOpp.CW_in_Last_730_Days_Workflow__c);
                }
                 if(shouldUpdate==true){
                                lstOpp.add(objOpp);
                            }
           //update lstOpp;  
        }
             if(lstOpp.size()>0)
                    List<Database.SaveResult> resultList  = Database.update(lstOpp, false);
    
    }
    /* FINISH Method */
    global void finish(Database.BatchableContext BC){
        system.debug('EXECUTE Method...'); 
       
    }  
}

Please help us on this.

Thanks in Advance
Hello All,

I want to know the update the checkboxes in opportunity object using process builder.Please help me on below conditions

The Conditions are:
when  Last 30 Days (Formula) is True, in Last 30 Days (Workflow) is TRUE (Viceversa)
when  Last 730 Days (Formula) is True, in Last 730 Days (Workflow) is TRUE (Viceversa)

Thanks in Advance
Hi Team,

We have an requirement as mentioend below
1.When Phone # field is updated in Account , the same number has to get updated in Contact -> Phone # field and if the Phone # field in Contact is updated and the same Phone # has to be populated in Account -> Phone # field.

Through Trigger how we can acheive this,Can any one please Help me on this.

Thanks
Hi Team,

Facing the issues on Trigger test class code coverage. as of now code coverage is showing only 20% Let me know the test class written is correct or not Please help me to get 100% code coverage Any help very much appreciated : Below is the Trigger and Test class.

Trigger:

trigger TaskEmailNotification on Task(after update){
 if(TaskEmailNotification.someValue){
    TaskEmailNotification.someValue = false; 
    Set<Id> setopportunityids = new Set<Id>();
    for(Task tsk: Trigger.New){    
        string taskwhatid = tsk.Whatid;
        system.debug('tsk.whatid___'+taskwhatid);        
        if(tsk.Status=='Completed'&&Trigger.oldMap.get(tsk.Id).Status != 'Completed' && tsk.OwnerId != tsk.CreatedById && tsk.Whatid != null && taskwhatid.startsWith('009') && !setopportunityids.contains(tsk.WhatId)){
            setopportunityids.add(tsk.whatid);
        }   
    }
   
    Map<Id,opportunity> MapofidandOpportunity = new Map<Id,opportunity>([select Name,id,ownerid from opportunity where Id in :setopportunityids ]);
    Map<Id,user> Mapofoppidanduser = new Map<Id,user>();
    if(MapofidandOpportunity != null && MapofidandOpportunity.size() > 0){
       set<id> ownerids = new set<id>();
        for(opportunity objopp : MapofidandOpportunity.values()){
            ownerids.add(objopp.ownerid);
        }
        if(ownerids != null && ownerids.size() > 0){
           
            Map<Id,User> Mapofidanduser = new Map<Id,User>([select id, name, email, managerid, manager.email, UserRole.Name from user where id in:ownerids AND UserRole.Name LIKE 'Test%' ]); 
            if(Mapofidanduser != null && Mapofidanduser.size() > 0){
                for(opportunity objopp : MapofidandOpportunity.values()){
                    if(Mapofidanduser.get(objopp.ownerid) != null)
                    Mapofoppidanduser.put(objopp.id,Mapofidanduser.get(objopp.ownerid));
                }
            }
        }
    }
    if(Mapofoppidanduser != null && Mapofoppidanduser.size() > 0){
        List<Messaging.SingleEmailMessage> emailMsglist=new List<Messaging.SingleEmailMessage>();
        for(Task tsk : Trigger.New){
            //system.debug('tsk.whatid___'+tsk.what.type);
            if(tsk.Status == 'Completed' && Trigger.oldMap.get(tsk.Id).Status != 'Completed' &&  tsk.whatid != null &&    Mapofoppidanduser.get(tsk.whatid) != null){                
                User theUser = Mapofoppidanduser.get(tsk.whatid);               
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                list<String> toAddresses = new list<String>(); 
                toAddresses.add(theUser.email);
                //if(theUser.Managerid != null)
                if(theUser.Managerid != null && tsk.Subject.startsWith('Help'))   
                toAddresses.add(theUser.Manager.email);
                mail.setToAddresses(toAddresses);    // Set the TO addresses 
                mail.setSubject('A Task has been updated');   // Set the subject 
                //Next, create a string template. Specify {0}, {1} etc. in place of actual values.
                String template = 'Hello {0}, \n\nYour Task has been Completed. Here are the details - \n\n';
                //template+='Subject: '+tsk.Subject+' \n\n';
                template+= 'Subject: '+'"'+tsk.Subject+'"'+' \n';
                template+='Status: '+tsk.Status +' \n';
                template+='Priority: '+tsk.Priority +' \n';
                template+='DueDate: '+tsk.ActivityDate+'\n';
                template+='Comments: '+tsk.Description +'\n';
                template+='Related To: '+tsk.WhatID+' \n'; 
                template+='\n\nFor more details, click the following link: \n';
                template+=URL.getSalesforceBaseUrl().toExternalForm() + '/' + tsk.Id;
                List<String> args = new List<String>();     
                args.add(theUser.Name);
                // Here's the String.format()
                String formattedHtml = String.format(template, args);
                mail.setPlainTextBody(formattedHtml);
                emailMsglist.add(mail);
            }
        }
        if(emailMsglist != null && emailMsglist.size() > 0)
        system.debug('emailMsglist'+emailMsglist.size());
        Messaging.SendEmail(emailMsglist);
    }
  }
}

Test Class :

@isTest(seeAllData=true)
public class TaskEmailNotificationTest{    
    static testMethod void doEmailNotificationTest(){        
        //Test.startTest();
        Account acc = new Account();            
        acc.Name='TestAcc';                    
        acc.salesAreaList__c = '';            
        acc.BillingCountry = 'SLOVAKIA';            
        acc.BillingCity = 'test city';            
        acc.BillingStreet = 'test street';            
        acc.ShippingCountry = 'SLOVAKIA';                     
        insert acc; 
        
        Profile ps = [SELECT Id FROM Profile WHERE Name='Test']; 
        list<User> lstuser = [SELECT Id,Name FROM User where UserRole.name LIKE 'Test%'];

        Opportunity Opp1 = Test.getOpportunity();
        Opp1.AccountId = acc.Id;
        Opp1.ownerid = lstuser[0].id;        
        insert Opp1;
        
       Test.startTest();  
        Task t = new task();
        //task.Owner = 'test';
        t.Subject = 'Message Sent';
        t.ActivityDate=system.Today();
        t.status = 'In progress';
        t.Priority = 'Low'; 
        t.OwnerId = lstuser[0].id;
        t.WhatId=Opp1.id;
        //task.WhoId=Opp.id;
        insert t;
        t.status = 'Completed';
        update t;        
        System.assertEquals (t.Status,'Completed');

    Test.stopTest();        
    }
}

Thanks
Hi Team,
I have created a batch apex class and executing that code using "Anonymous Block". I am monitoring the batch operations in debug logs and noticed the operation name as "SerialBatchApexRangeChunkHandler" with status "Internal Salesforce.com Error". Can any one please help me to acheive this error.

Here is my Batch Apex logic:

global class OpportunityCWDays implements Database.Batchable <Sobject>{    
    Public List<Opportunity> lstOpp;    
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'Select id, Name, StageName, Software_Product_Count__c, CW_in_Last_30_Days_Formula__c, CW_in_Last_30_Days_Workflow__c, CW_in_Last_730_Days_Formula__c, CW_in_Last_730_Days_Workflow__c from Opportunity Where StageName = \'S8- Closed Won\' AND Software_Product_Count__c > 0 AND ( ((CW_in_Last_30_Days_Formula__c = True AND CW_in_Last_30_Days_Workflow__c = False) OR (CW_in_Last_30_Days_Formula__c = False AND CW_in_Last_30_Days_Workflow__c = True)) OR ((CW_in_Last_730_Days_Formula__c = True AND CW_in_Last_730_Days_Workflow__c = False) OR (CW_in_Last_730_Days_Formula__c = False AND CW_in_Last_730_Days_Workflow__c = True))) ';       
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Opportunity> batch){ 
    lstOpp = new List<Opportunity>();
    for(Opportunity objOpp : batch){
        if(objOpp.CW_in_Last_30_Days_Formula__c == True){
            objOpp.CW_in_Last_30_Days_Workflow__c = True;
        }
        else{//if(objOpp.CW_in_Last_30_Days_Formula__c == False){
            objOpp.CW_in_Last_30_Days_Workflow__c = False;
            }
        if(objOpp.CW_in_Last_730_Days_Formula__c == True){
            objOpp.CW_in_Last_730_Days_Workflow__c = True;
        }
        else{//if(objOpp.CW_in_Last_730_Days_Formula__c == False){
            objOpp.CW_in_Last_730_Days_Workflow__c = False;
        }
        lstOpp.add(objOpp);
    }
    Database.SaveResult[] dbOppList = Database.Update(lstOpp,false);   
    //System.debug('Update CW ' + dbOppList[].Id);
    for(Database.SaveResult sr : dbOppList){
        if(sr.isSuccess()){
            // Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully Updated OPP ID: ' + sr.getId());
        }       
        else{
            // Operation failed, so get all errors                
            for(Database.Error err : sr.getErrors()){
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Users fields that affected this error: ' + err.getFields());
            }
        }
    }
  }
    global void finish(Database.BatchableContext BC){
        
    }  
}

Thanks
Hi Team,

We have created Two batch classes using different objects(Account and Opportunity) for updating the Teammembrs which are working fine.But my problem is i want to include onebatch class instead of two batch class.Can any one please help on this.

Thanks
Hi Team,

When we are running the below batch apex  we are facing the erros like "FIELD_CUSTOM_VALIDATION_EXCEPTION, When the Customer Connect Request Status is Pending or Approved, the Account Name and Address cannot be edited ".Actually there is a validation rules are firing.How we can avoid the validation rules using my code?Please find my below code which we are trying and help us to avoiding the errors.

Apex Class:

global class OpportunityDays implements Database.Batchable<Sobject>{
    global OpportunityCWDays(){
    }    
    global Database.QueryLocator start(Database.BatchableContext BC){  
     
        String query = 'Select id, StageName, Software_Product_Count__c, CW_in_Last_30_Days_Formula__c, CW_in_Last_30_Days_Workflow__c, CW_in_Last_730_Days_Formula__c, CW_in_Last_730_Days_Workflow__c from Opportunity Where  Software_Product_Count__c > 0 AND StageName = \'S8- Closed Won\'
        ;
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC,List<Opportunity> lstOpp){ 
        for(Opportunity objOpp : lstOpp){
            if(objOpp.CW_in_Last_30_Days_Formula__c && !objOpp.CW_in_Last_30_Days_Workflow__c) {
                objOpp.CW_in_Last_30_Days_Workflow__c = TRUE;
            }   
            else if(!objOpp.CW_in_Last_30_Days_Formula__c && objOpp.CW_in_Last_30_Days_Formula__c ) {
                objOpp.CW_in_Last_30_Days_Workflow__c = False;
            }
            
            if(objOpp.CW_in_Last_730_Days_Formula__c  && !objOpp.CW_in_Last_730_Days_Workflow__c) {
                objOpp.CW_in_Last_730_Days_Workflow__c = True;
            }   
            else if (!objOpp.CW_in_Last_730_Days_Formula__c && !objOpp.CW_in_Last_730_Days_Workflow__c){
                objOpp.CW_in_Last_730_Days_Workflow__c = False;
            }                                    
        }
        update lstOpp;    
    }    
    
    global void finish(Database.BatchableContext BC){        
    }  
}

Thanks
 
Hi Team,

Can any one please help me to create a trigger for below senario.

Object: Task
if Task.Support Team Request = AE Support Request
then{
if Task.created by.Pole = NA or North America then set Task.project= AE NA or Project id= xxxx
if Task.created by.Pole = China then set Task.project= AE China or Project id= xxxx1
if Task.created by.Pole = APAC then set Task.project= AE APAC or Project id= xxxx2
if Task.created by.Pole = EMEA or MEA then set Task.project= AE EMEA or Project id= xxxx3
if Task.created by.Pole = LATAM then set Task.project= AE LATAM or Project id= xxxx4
}

Thanks in Advance
Hi Team,

Is there any possible to send task completion email notification to owner using workflows.If it possible please update the steps hear.

Thanks in Advance

 
Hi Team,

Can any one please help me to create test class for below trigger.

rigger Task on Opportunity (after insert,after update){    
    List<Opportunity> listOpp = Trigger.new;
    List<Task> listTask = new List<Task>();    
       if(Trigger.isInsert){
        for(Opportunity opp:listOpp){              
           if(opp.Help__c != null){
                Task t = new Task();
                t.OwnerId = opp.OwnerId;
                t.WhatId = opp.Id;
                t.Subject ='Help' + ': ' +opp.Help__c ; 
                t.ActivityDate = System.Today()+7;  
                t.Description = opp.Description__c;
                t.Status = 'In Progress';
                t.Priority = 'Normal';
                listTask.add(t);
            } 
        }
    }   

     if(Trigger.isUpdate){
         Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
        for(Opportunity opp:listOpp){
            if(Opp.Help__c != oldOppMap.get(opp.Id).Help__c || Opp.Description__c!= oldOppMap.get(opp.Id).Description__c ){
            Task t = new Task();
            t.WhatId = opp.Id;   
            t.Subject = 'Help' + ': ' +opp.Help__c ; 
            t.Description = opp.Description__c;  
            t.ActivityDate = System.Today()+7;
            t.Status = 'In Progress';
            t.Priority = 'Normal';        
            listTask.add(t);
         }
        }   
     }  
     insert listTask;
 }

Thanks in Advance
Hi Team,

I have a two fields in opportunity object those are "Help(Picklist)" and "Description(Text)". If i select "help" pick list value is Manager and entering in the "description" field  is Test,that time automatic task has been creating along with opportunity name based on below trigger.But my problem is whenever i removed the specific values then that time also task is creating with Null values.How to include that logic in my existing trigger.And also If i change the Description value in the task object it should change the same in Opty field also.Please help us.

Trigger UpdateTask on Opportunity(after insert,after update){    
    List<Opportunity> listOpp = Trigger.new;
    List<Task> listTask = new List<Task>();    
    /* New Task */
    if(Trigger.isInsert){
        for(Opportunity opp:listOpp){    
           if(opp.Help__c != null){
                Task t = new Task();
                t.OwnerId = opp.OwnerId;
                t.WhatId = opp.Id;
                t.Subject ='Help' + ': ' +opp.Help__c + ': '  + opp.Name; 
                t.ActivityDate = System.Today()+7;  
                t.Description = opp.Description__c;
                t.Status = 'In Progress';
                t.Priority = 'Normal';
                listTask.add(t);
            } 
        }
    }   
    // UPDATE the Same Task 
     if(Trigger.isUpdate){
        Set<Id> setOppId = new Set<Id>();
         Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
        for(Opportunity opp:listOpp){
            if(Opp.Help__c != oldOppMap.get(opp.Id).Help__c || Opp.Description__c!= oldOppMap.get(opp.Id).Description__c){
            Task t = new Task();
            t.WhatId = opp.Id;   
            t.Subject = 'Help' + ': ' +opp.Help__c + ': '  + opp.Name; 
            t.Description = opp.Description__c;  
            t.ActivityDate = System.Today()+7;
            listTask.add(t);
         }
        }
    } 
        insert listTask;
}

Thanks in Advance
 
Hi Team,

Whenever update the Opty fields create a new brand task along with opportunity name. for that we have written a trigger working as expected. But once create a task if we update the description field it should creat a new task not overriding the exting task. 

Every time Step(OptyField) or Description(OptyField) changes, New task should be created

Example: Step = Legal,Description = test 

If I again select Step = Legal and Description = test1234  new task should be created. Currently its updating test to test1234.

Please Find my below trigger and let us know where we made the mistakes.

Trigger Task on Opportunity(after insert,after update){
Set<Id> opIds = new Set<Id>();
List<Task> taskList = new List<Task>();
List<Opportunity> Opps = Trigger.new;
List<Opportunity> taskOps = [Select Id,Steps__c,Description__c from Opportunity where Id in :opIds];
                            
if(Trigger.isInsert){
   for (Opportunity Opp: Opps){
        Task t = new Task();
        t.WhatId = opp.Id;
        t.Subject = 'Help' + ': ' +opp.Steps__c + ': '  + opp.Name; 
        t.Description = opp.Description__c;  
         t.ActivityDate = System.Today()+7;     
        taskList.add(t);
    }
}
//Update 
if(Trigger.isUpdate){
    Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
     for (Opportunity Opp: Opps){
         // description got changed
         if(Opp.Steps__c != oldOppMap.get(opp.Id).Steps__c ){
            Task t = new Task();
            t.WhatId = opp.Id;       
             t.Subject = 'Help' + ': ' +opp.Steps__c + ': '  + opp.Name; 
             t.Description = opp.Description__c;  
             t.ActivityDate = System.Today()+7;
        
            taskList.add(t);
         }
    }
   }
insert taskList;
}

Thanks in Advance
Hi Team,

We have created one related list under Opportunity.We want to display the task completion details in to new related list. Is there any posibility to create a trigger? Please help us.

Thanks in Advance
Hi Team,

I have created a trigger to sending task email notification.Can some one give me the test coverage class for this.

trigger EmailNotification  on Task (after update ){
    Set<Id> ownerIds = new Set<Id>();
    for(Task tsk: Trigger.New){
        if(tsk.Status=='Completed'&&Trigger.oldMap.get(tsk.Id).Status != 'Completed' && tsk.OwnerId != tsk.CreatedById ){
           ownerIds.add(tsk.CreatedById);    
        }
    }
    Map<Id,User> userMap = new Map<Id,User>([select Name, Email, Manager.Email from User where Id in :ownerIds]);
    List<Messaging.SingleEmailMessage> emailMsglist=new List<Messaging.SingleEmailMessage>();   
     for(Task tsk : Trigger.New){
        if(tsk.Status == 'Completed' && Trigger.oldMap.get(tsk.Id).Status != 'Completed' && tsk.OwnerId != tsk.CreatedById ){ 
        User theUser = userMap.get(tsk.CreatedById);
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {theUser.Email,theUser.Manager.email};
                mail.setToAddresses(toAddresses);   
                mail.setSubject('A Task has been updated'); 
                String template = 'Hello {0}, \n\nYour Task has been Completed. Here are the details - \n\n';
                template+= 'Subject: '+'"'+tsk.Subject+'"'+' \n';
                template+='Status: '+tsk.Status +' \n';
                template+='Priority: '+tsk.Priority +' \n';
                template+='DueDate: '+tsk.ActivityDate+'\n';
                template+='Comments: '+tsk.Description +'\n';
                template+='\n\nFor more details, click the following link: \n';
                template+=URL.getSalesforceBaseUrl().toExternalForm() + '/' + tsk.Id; 
            List<String> args = new List<String>();     
            args.add(theUser.Name);
            String formattedHtml = String.format(template, args);
            mail.setPlainTextBody(formattedHtml);
            emailMsglist.add(mail);  
        }
    } 
    Messaging.SendEmail(emailMsglist);
}

Thanks in Advance

 
Hi Team,

Opprtunity owners and thier managers should get an email after task completion.we are trying to below trigger code, logic is working only for opportunity owner getting completion mail but not for their managers ,we need to send opoortunity owner and their managers also .Please help us for this.

Bit of code:

trigger TaskEmailNotification  on Task (after update ){
    Set<Id> ownerIds = new Set<Id>();
    for(Task tsk: Trigger.New){
        if(tsk.Status=='Completed'&&Trigger.oldMap.get(tsk.Id).Status != 'Completed' && tsk.OwnerId != tsk.CreatedById ){
            ownerIds.add(tsk.CreatedById);      
        }       
    }   
    Map<Id,User> userMap = new Map<Id,User>([select Name,Email from User where Id in :ownerIds]);
    List<Messaging.SingleEmailMessage> emailMsglist=new List<Messaging.SingleEmailMessage>();    
    for(Task tsk : Trigger.New){
               if(tsk.Status=='Completed' && Trigger.oldMap.get(tsk.Id).Status != 'Completed' && tsk.OwnerId != tsk.CreatedById){
          
                 User theUser = userMap.get(tsk.CreatedById);
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses = new String[] {theUser.Email};
Thanks in Advance
Hi Team,

We have created one Tracking history section Related List Under Opportunity.We want to show the  task details once task is completed. How to do?

Please Help us.

Thanks in Advance.
 
Hi Team,

How to populate the opty field value in the Task object comments field.Let's say we have a field called  Description,whenerver we are entered in the description field same comments it should be populate in the Task comment field.

How to acheive this one.

Thanks in Advance
Hi Team,

We have created Two batch classes using different objects(Account and Opportunity) for updating the Teammembrs which are working fine.But my problem is i want to include onebatch class instead of two batch class.Can any one please help on this.

Thanks
Hi Team,

How to populate the opty field value in the Task object comments field.Let's say we have a field called  Description,whenerver we are entered in the description field same comments it should be populate in the Task comment field.

How to acheive this one.

Thanks in Advance
Hi All,

Facing the issues like getting a System.LimitException: Too many SOQL queries: 101 when trying to update my opportunities. Can someone please help me on this code, so we don't run into this error anymore and how to avoid it again with future Triggers.
Please check my below Trigger and class:
Trigger:
Trigger AccountPartnerType on Account (after update){
    AccountPartnerTypecls acpt = new AccountPartnerTypecls();
    if(AccountPartnerTypecls.Aptcalc)
    acpt.AccountPartnerTypecalc(Trigger.new,trigger.newmap,trigger.oldmap);
}
Class:
public class AccountPartnerTypecls{
                public static Boolean Aptcalc = true;
                public static Boolean Aptcalc1 = false; 
                public static Boolean Aptcalc2 = false;  
                public void AccountPartnerTypecalc(List<Account> aclist,map<id,Account> newmap,map<id,Account> oldmap){
                                set<id> accidset = new set<id>(); 
                                set<string> ulprntidset = new set<string>();   
                                string dulprnt;
                                map<string,List<Account>>  accmap = new  map<string,List<Account>>();
                                system.debug('*********'+aclist.size());           
                                for(Account acc: aclist){
                                                accidset.add(acc.id);
                                                // if(acc.GE_Global_Ultimate_Reporting_Account__c!=Null)  //GE_Digital_Ultimate_Parent_ID__c
                                                //Ultimate_Parent_ID__c
                                                ulprntidset.add(acc.GE_Digital_Ultimate_Parent_ID__c);
                                                dulprnt= acc.GE_Digital_Ultimate_Parent_ID__c;
                                                system.debug('*********'+dulprnt);           
                                }
                                system.debug('*********'+dulprnt); 
                                List<Account> hiracclist = new List<Account>();
                                List<Account> aclist1 = [select id, name,Partner_Status__c,Is_Partner__c,Account_Type__c,Parentid,GE_Digital_Ultimate_Parent_ID__c from Account where  GE_Digital_Ultimate_Parent_ID__c  =:dulprnt  AND Parentid!=null AND id NOT IN:accidset ];
                                system.debug('*********'+aclist1.size()); 
                                for(Account a : aclist1 ){
                                                if(accmap.containskey(a.GE_Digital_Ultimate_Parent_ID__c))
                                                accmap.get(a.GE_Digital_Ultimate_Parent_ID__c).add(a);
                                                else{
                                                                List<Account> alist = new List<Account>();
                                                                alist.add(a);
                                                                accmap.put(a.GE_Digital_Ultimate_Parent_ID__c,alist);
                                                }
                                }
                                for(Account acc: [select id, name,Partner_Status__c,Is_Partner__c,Account_Type__c,Parentid,GE_Digital_Ultimate_Parent_ID__c from Account where ID IN :accidset ]){
                                                string acctype='';
                                                if(accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c)!=null){
                                                                /* if(accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c).contains(acc)){
                                                                accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c).set(0, acc);
                                                                accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c).remove(0);
                                                                }*/
                                                                for(Account acchr : accmap.get(acc.GE_Digital_Ultimate_Parent_ID__c)){
                                                                                //acchr.Is_Partner__c= acc.Is_Partner__c;
                                                                                if(acc.Is_Partner__c)
                                                                                acchr.Account_Type__c = 'Partner';
                                                                                else
                                                                                acchr.Account_Type__c= '';
                                                                                hiracclist.add(acchr);
                                                                }
                                                                if(acc.Is_Partner__c){
                                                                                if(acc.parentid!=null)
                                                                                Aptcalc1 =true;
                                                                                acc.Account_Type__c= 'Partner';
                                                                                hiracclist.add(acc);
                                                                }
                                                                else{
                                                                                if(acc.parentid!=null)
                                                                                Aptcalc2 =true;
                                                                                acc.Account_Type__c= '';
                                                                                hiracclist.add(acc);
                                                                }
                                                }
                                                else{
                                                                if(acc.Is_Partner__c){
                                                                                acc.Account_Type__c= 'Partner';
                                                                                hiracclist.add(acc);
                                                                }
                                                                else{
                                                                                acc.Account_Type__c= ' ';
                                                                                hiracclist.add(acc);
                                                                }
                                                }
                                }
                                Account a=  [select id, name,Partner_Status__c,Is_Partner__c,Account_Type__c,Parentid,GE_Digital_Ultimate_Parent_ID__c from Account where ID  =:dulprnt];
                                if(Aptcalc1){
                                                a.Account_Type__c= 'Partner';
                                                hiracclist.add(a);
                                }
                                else if(Aptcalc2){
                                                a.Account_Type__c= '';
                                                hiracclist.add(a);
                                }
                                Aptcalc = false;         
                                update  hiracclist;
                }
}
 
Thanks
 
Hi All,

We have requirement as mentioend below senario:

How to create/generate a new Opportunity with Related Lists to be populated on new Opportunity from Old Opportunity based on the closure of  ContractEnd date  of a existing opportunity.

Can any one please help us to create a new trigger.

Thanks in Advance
 
Hi All,

I want to know bypass the Logic in my Batch apex.Problem is when we are running the Batch apex  some records are updating some are not.Because we have custom settings are firing in my logic.Can any one help us how to bypass the custom setting logic in my Batch apex.Please check my Below Batch Apex Logic which we are using Currently.

global class OpportunityDays implements Database.Batchable<Sobject>{
    Public List<Opportunity> lstOpp;
   
    global Database.QueryLocator start(Database.BatchableContext BC){
      
         String query = 'Select id, Name, StageName, Software_Product_Count__c, CW_in_Last_30_Days_Formula__c, CW_in_Last_30_Days_Workflow__c, CW_in_Last_730_Days_Formula__c, CW_in_Last_730_Days_Workflow__c from Opportunity Where StageName = \'S8- Closed Won\' AND Software_Product_Count__c > 0 AND ( ((CW_in_Last_30_Days_Formula__c = TRUE AND CW_in_Last_30_Days_Workflow__c = False) OR (CW_in_Last_30_Days_Formula__c = False AND CW_in_Last_30_Days_Workflow__c = True)) OR ((CW_in_Last_730_Days_Formula__c = TRUE AND CW_in_Last_730_Days_Workflow__c = False) OR (CW_in_Last_730_Days_Formula__c = False AND CW_in_Last_730_Days_Workflow__c = True )))';
         
        system.debug('START Method...');       
            return Database.getQueryLocator(query);
            }
    
    /* EXECUTE Method */
    global void execute(Database.BatchableContext BC, List<Opportunity> batch){
        system.debug('EXECUTE Method...');    
        lstOpp = new List<Opportunity>();
        for(Opportunity objOpp : batch){
             boolean shouldUpdate = false;
            if(objOpp.CW_in_Last_30_Days_Formula__c == True){
                objOpp.CW_in_Last_30_Days_Workflow__c = True;
                shouldUpdate = true;
                system.debug('CW 30 WF True...'+ objOpp.CW_in_Last_30_Days_Workflow__c);
            }
            else if(objOpp.CW_in_Last_30_Days_Formula__c == False){
                    objOpp.CW_in_Last_30_Days_Workflow__c = False;
                    shouldUpdate = true;
                    system.debug('CW 30 WF False...'+ objOpp.CW_in_Last_30_Days_Workflow__c);
                }
            if(objOpp.CW_in_Last_730_Days_Formula__c == True){
                objOpp.CW_in_Last_730_Days_Workflow__c = True;
                shouldUpdate = true;
                system.debug('CW 730 WF True...'+ objOpp.CW_in_Last_730_Days_Workflow__c);
            }
            else if(objOpp.CW_in_Last_730_Days_Formula__c == False){
                    objOpp.CW_in_Last_730_Days_Workflow__c = False;
                      shouldUpdate = true;
                    system.debug('CW 730 WF False...'+ objOpp.CW_in_Last_730_Days_Workflow__c);
                }
                 if(shouldUpdate==true){
                                lstOpp.add(objOpp);
                            }
           //update lstOpp;  
        }
             if(lstOpp.size()>0)
                    List<Database.SaveResult> resultList  = Database.update(lstOpp, false);
    
    }
    /* FINISH Method */
    global void finish(Database.BatchableContext BC){
        system.debug('EXECUTE Method...'); 
       
    }  
}

Please help us on this.

Thanks in Advance
Hi Team,

We have an requirement as mentioend below
1.When Phone # field is updated in Account , the same number has to get updated in Contact -> Phone # field and if the Phone # field in Contact is updated and the same Phone # has to be populated in Account -> Phone # field.

Through Trigger how we can acheive this,Can any one please Help me on this.

Thanks
Hi Team,

Facing the issues on Trigger test class code coverage. as of now code coverage is showing only 20% Let me know the test class written is correct or not Please help me to get 100% code coverage Any help very much appreciated : Below is the Trigger and Test class.

Trigger:

trigger TaskEmailNotification on Task(after update){
 if(TaskEmailNotification.someValue){
    TaskEmailNotification.someValue = false; 
    Set<Id> setopportunityids = new Set<Id>();
    for(Task tsk: Trigger.New){    
        string taskwhatid = tsk.Whatid;
        system.debug('tsk.whatid___'+taskwhatid);        
        if(tsk.Status=='Completed'&&Trigger.oldMap.get(tsk.Id).Status != 'Completed' && tsk.OwnerId != tsk.CreatedById && tsk.Whatid != null && taskwhatid.startsWith('009') && !setopportunityids.contains(tsk.WhatId)){
            setopportunityids.add(tsk.whatid);
        }   
    }
   
    Map<Id,opportunity> MapofidandOpportunity = new Map<Id,opportunity>([select Name,id,ownerid from opportunity where Id in :setopportunityids ]);
    Map<Id,user> Mapofoppidanduser = new Map<Id,user>();
    if(MapofidandOpportunity != null && MapofidandOpportunity.size() > 0){
       set<id> ownerids = new set<id>();
        for(opportunity objopp : MapofidandOpportunity.values()){
            ownerids.add(objopp.ownerid);
        }
        if(ownerids != null && ownerids.size() > 0){
           
            Map<Id,User> Mapofidanduser = new Map<Id,User>([select id, name, email, managerid, manager.email, UserRole.Name from user where id in:ownerids AND UserRole.Name LIKE 'Test%' ]); 
            if(Mapofidanduser != null && Mapofidanduser.size() > 0){
                for(opportunity objopp : MapofidandOpportunity.values()){
                    if(Mapofidanduser.get(objopp.ownerid) != null)
                    Mapofoppidanduser.put(objopp.id,Mapofidanduser.get(objopp.ownerid));
                }
            }
        }
    }
    if(Mapofoppidanduser != null && Mapofoppidanduser.size() > 0){
        List<Messaging.SingleEmailMessage> emailMsglist=new List<Messaging.SingleEmailMessage>();
        for(Task tsk : Trigger.New){
            //system.debug('tsk.whatid___'+tsk.what.type);
            if(tsk.Status == 'Completed' && Trigger.oldMap.get(tsk.Id).Status != 'Completed' &&  tsk.whatid != null &&    Mapofoppidanduser.get(tsk.whatid) != null){                
                User theUser = Mapofoppidanduser.get(tsk.whatid);               
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                list<String> toAddresses = new list<String>(); 
                toAddresses.add(theUser.email);
                //if(theUser.Managerid != null)
                if(theUser.Managerid != null && tsk.Subject.startsWith('Help'))   
                toAddresses.add(theUser.Manager.email);
                mail.setToAddresses(toAddresses);    // Set the TO addresses 
                mail.setSubject('A Task has been updated');   // Set the subject 
                //Next, create a string template. Specify {0}, {1} etc. in place of actual values.
                String template = 'Hello {0}, \n\nYour Task has been Completed. Here are the details - \n\n';
                //template+='Subject: '+tsk.Subject+' \n\n';
                template+= 'Subject: '+'"'+tsk.Subject+'"'+' \n';
                template+='Status: '+tsk.Status +' \n';
                template+='Priority: '+tsk.Priority +' \n';
                template+='DueDate: '+tsk.ActivityDate+'\n';
                template+='Comments: '+tsk.Description +'\n';
                template+='Related To: '+tsk.WhatID+' \n'; 
                template+='\n\nFor more details, click the following link: \n';
                template+=URL.getSalesforceBaseUrl().toExternalForm() + '/' + tsk.Id;
                List<String> args = new List<String>();     
                args.add(theUser.Name);
                // Here's the String.format()
                String formattedHtml = String.format(template, args);
                mail.setPlainTextBody(formattedHtml);
                emailMsglist.add(mail);
            }
        }
        if(emailMsglist != null && emailMsglist.size() > 0)
        system.debug('emailMsglist'+emailMsglist.size());
        Messaging.SendEmail(emailMsglist);
    }
  }
}

Test Class :

@isTest(seeAllData=true)
public class TaskEmailNotificationTest{    
    static testMethod void doEmailNotificationTest(){        
        //Test.startTest();
        Account acc = new Account();            
        acc.Name='TestAcc';                    
        acc.salesAreaList__c = '';            
        acc.BillingCountry = 'SLOVAKIA';            
        acc.BillingCity = 'test city';            
        acc.BillingStreet = 'test street';            
        acc.ShippingCountry = 'SLOVAKIA';                     
        insert acc; 
        
        Profile ps = [SELECT Id FROM Profile WHERE Name='Test']; 
        list<User> lstuser = [SELECT Id,Name FROM User where UserRole.name LIKE 'Test%'];

        Opportunity Opp1 = Test.getOpportunity();
        Opp1.AccountId = acc.Id;
        Opp1.ownerid = lstuser[0].id;        
        insert Opp1;
        
       Test.startTest();  
        Task t = new task();
        //task.Owner = 'test';
        t.Subject = 'Message Sent';
        t.ActivityDate=system.Today();
        t.status = 'In progress';
        t.Priority = 'Low'; 
        t.OwnerId = lstuser[0].id;
        t.WhatId=Opp1.id;
        //task.WhoId=Opp.id;
        insert t;
        t.status = 'Completed';
        update t;        
        System.assertEquals (t.Status,'Completed');

    Test.stopTest();        
    }
}

Thanks
Hi Team,
I have created a batch apex class and executing that code using "Anonymous Block". I am monitoring the batch operations in debug logs and noticed the operation name as "SerialBatchApexRangeChunkHandler" with status "Internal Salesforce.com Error". Can any one please help me to acheive this error.

Here is my Batch Apex logic:

global class OpportunityCWDays implements Database.Batchable <Sobject>{    
    Public List<Opportunity> lstOpp;    
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'Select id, Name, StageName, Software_Product_Count__c, CW_in_Last_30_Days_Formula__c, CW_in_Last_30_Days_Workflow__c, CW_in_Last_730_Days_Formula__c, CW_in_Last_730_Days_Workflow__c from Opportunity Where StageName = \'S8- Closed Won\' AND Software_Product_Count__c > 0 AND ( ((CW_in_Last_30_Days_Formula__c = True AND CW_in_Last_30_Days_Workflow__c = False) OR (CW_in_Last_30_Days_Formula__c = False AND CW_in_Last_30_Days_Workflow__c = True)) OR ((CW_in_Last_730_Days_Formula__c = True AND CW_in_Last_730_Days_Workflow__c = False) OR (CW_in_Last_730_Days_Formula__c = False AND CW_in_Last_730_Days_Workflow__c = True))) ';       
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Opportunity> batch){ 
    lstOpp = new List<Opportunity>();
    for(Opportunity objOpp : batch){
        if(objOpp.CW_in_Last_30_Days_Formula__c == True){
            objOpp.CW_in_Last_30_Days_Workflow__c = True;
        }
        else{//if(objOpp.CW_in_Last_30_Days_Formula__c == False){
            objOpp.CW_in_Last_30_Days_Workflow__c = False;
            }
        if(objOpp.CW_in_Last_730_Days_Formula__c == True){
            objOpp.CW_in_Last_730_Days_Workflow__c = True;
        }
        else{//if(objOpp.CW_in_Last_730_Days_Formula__c == False){
            objOpp.CW_in_Last_730_Days_Workflow__c = False;
        }
        lstOpp.add(objOpp);
    }
    Database.SaveResult[] dbOppList = Database.Update(lstOpp,false);   
    //System.debug('Update CW ' + dbOppList[].Id);
    for(Database.SaveResult sr : dbOppList){
        if(sr.isSuccess()){
            // Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully Updated OPP ID: ' + sr.getId());
        }       
        else{
            // Operation failed, so get all errors                
            for(Database.Error err : sr.getErrors()){
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Users fields that affected this error: ' + err.getFields());
            }
        }
    }
  }
    global void finish(Database.BatchableContext BC){
        
    }  
}

Thanks
Hi Team,

Can any one please help me to create a trigger for below senario.

Object: Task
if Task.Support Team Request = AE Support Request
then{
if Task.created by.Pole = NA or North America then set Task.project= AE NA or Project id= xxxx
if Task.created by.Pole = China then set Task.project= AE China or Project id= xxxx1
if Task.created by.Pole = APAC then set Task.project= AE APAC or Project id= xxxx2
if Task.created by.Pole = EMEA or MEA then set Task.project= AE EMEA or Project id= xxxx3
if Task.created by.Pole = LATAM then set Task.project= AE LATAM or Project id= xxxx4
}

Thanks in Advance