• plai_highwire
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
I have a junction object called Predecessor Cases that has two lookup fields back to cases.  The two fields are called successor case and predecessor case.
I created a custom button that executes some apex code.  When i click on the custom button, nothing is happening.  Am I not passing the right info to execute the apex code?

Apex Class:
global class AdjustDate {
    WebService static void AdjustDate(Id CaseId){         
        Set<ID> sid = new Set<ID>();     
        Integer DaysToAdd = 1;
        //Get a list of all Predecessor objects that has a match on the  predecessor field to the case ID            
        List<Predecessor_Case__c> successor = [SELECT ID,Predecessor_Case__c, Successor_Case__c, Successor_case__r.id FROM Predecessor_Case__c WHERE Predecessor_Case__r.id =: CaseId ];
        //Add the successor case ids that match those predecessor cases to a set 
        for(Predecessor_Case__c pc : successor){
            sid.add(pc.Successor_Case__r.id);
        }
        //Get a list of all cases that match that of the sid
        List<Case> successcases = [SELECT ID, Estimated_Launch_Date__c, Days_To_Add__c FROM Case WHERE ID IN: sid];
        // Do the calculations for those cases
        for(Case c: successcases){            
            c.Estimated_Launch_Date__c += DaysToAdd;
            c.Actual_Approval_Date__c = date.today();
        }  
    }
}


Custom button:
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}


sforce.apex.execute(
"AdjustDate",
"adjustdate",
{id:"{!Case.Id}"})

I have a trigger that does multiple processes on the case object.  I seem to be hitting my soql limits on this trigger.  Could someone point me in the right direction of where I might be looping too many statements?  Much appreciated.

trigger BeforeCaseInsert on Case (before insert,before update)
{
    List<String> RecordTypeNameList = new List<String>();
    RecordTypeNameList.add('CR');
    RecordTypeNameList.add('Prod Specialist');
    RecordTypeNameList.add('BenchPress');
    RecordTypeNameList.add('Support');
    
    
    List<RecordType> RecordTypeResult = [SELECT id,Name FROM RecordType WHERE name in: RecordTypeNameList];
    
    Map<String,Id> RecordTypeNameIDMap = new Map<String,Id>();
    
    for(RecordType rt : RecordTypeResult)
    {
        RecordTypeNameIDMap.put(rt.Name,rt.id);
    }
    
 
    private List <Sites__c> sitesF;
    List<Spec_History__c> SpecList = new List<Spec_History__c>();
    
   
    sitesF = new List<Sites__c>([SELECT id, Name, Feedback__c, Account__c, Account__r.Publication_Manager2__c
                                 FROM Sites__c LIMIT 50000]);
    
    
    
    //Create lists for the case team member code
    List<CaseTeamMember> ctm = new List<CaseTeamMember>();
    List<CaseTeamMember> ctmdelete = new List<CaseTeamMember>();
    List<CaseTeamRole>ctrole = [SELECT Name, Id FROM CaseTeamRole];
    Map<String,String>caseteamr = new Map<String,String>{};
        for(CaseTeamRole ct:ctrole){
            caseteamr.put(ct.Name, ct.Id);}
    
    //Create Set of Case Ids where status is Closed
    Set<ID>cid= new Set<ID>();
    
    
    for (Case c : trigger.new) {
        
        if (c.Description != null
            && c.Description != ''
            && c.recordTypeID == RecordTypeNameIDMap.get('Support'))
        {
            for(Sites__c s : sitesF) {
                if(c.Description.contains(s.Feedback__c) && s.Feedback__c != null){
                    c.sites__c = s.id;
                    c.accountId = s.account__c;

                    break;}
            }
        }
        
        if (c.recordTypeId == RecordTypeNameIDMap.get('Prod Specialist')
            && c.JCode__c != null
            && c.JCode__c != ''
           ) {
               for(Sites__c s : sitesF) {
                   if (c.JCode__c == s.Name){
                       c.accountId = s.account__c;
                       c.Sites__c = s.id;
                       break;
                   }
               }
               
           }
        
        // Spec History Code
        if (Trigger.isUpdate) {
            if (c.recordTypeId == RecordTypeNameIDMap.get('CR'))      
            {
                Case oldCase = Trigger.oldMap.get(c.ID);
                if (c.Spec_Image__c != oldCase.Spec_Image__c || c.Spec_pages__c != oldCase.Spec_Pages__c || c.Jcode__c != oldCase.JCode__c || c.Spec_description__c != oldCase.Spec_description__c || c.Configuration_Details__c != oldCase.Configuration_Details__c ) {
                    Spec_History__c newspec = new Spec_History__c ();
                    if (c.Spec_Image__c != oldCase.Spec_Image__c) {
                        newspec.Spec_Image_History__c = oldCase.Spec_Image__c;
                    }
                    if (c.Spec_pages__c != oldCase.Spec_Pages__c){
                        newspec.Spec_pages__c = oldCase.Spec_Pages__c;
                    }
                    if (c.Jcode__c != oldCase.JCode__c){
                        newspec.Spec_Jcode__c = oldCase.JCode__c;
                    }
                    if (c.Spec_description__c != oldCase.Spec_description__c){
                        newspec.Spec_description__c = oldCase.Spec_description__c;
                    }
                    if (c.Configuration_Details__c != oldCase.Configuration_Details__c){
                        newspec.Spec_Config_Details__c = oldCase.Configuration_Details__c;
                    }
                    newspec.Case__c = c.ID;
                    SpecList.add(newspec);
                }
                
            }
        }
        
        if (c.recordTypeId == RecordTypeNameIDMap.get('BenchPress'))
        {
            for(Sites__c s : sitesF) {
                
                if (c.Journal_Code__c == s.id){
                    c.Sites__c = s.id;
                    c.accountId = s.account__c;
                    
                    //         accMap.put(s.account__c,s.Account__r);
                    break;
                }
            }
        }
        
        /**
Following code used to insert case team members using the case manager field
**/
        // Get the id of the old case
        if (Trigger.isUpdate) {
            Case oldCase = Trigger.oldMap.get(c.Id);
            //Delete old case manager from case team
            if(oldCase.Case_Manager__c != null || oldCase.Case_Manager__c == null){
                for(CaseTeamMember ctmrem : [SELECT Id FROM CaseTeamMember WHERE MemberID =: c.Case_Manager__c OR MemberID =: oldCase.Case_Manager__c]){
                    
                    ctmdelete.add(ctmrem);}
            }
            // If the old case manager doesn't equal the new case manager
            if(oldCase.Case_Manager__c != c.Case_Manager__c &&	 c.Case_Manager__c != null){
                
                // Create a list of the members in the case team
                
                // For each of the case managers in ctmlist
                
                
                CaseTeamMember ctmadd = new CaseTeamMember();
                ctmadd.ParentId = c.id;
                ctmadd.MemberId = c.Case_Manager__c;
                ctmadd.TeamRoleId = caseteamr.get('Case Manager');
                
                
                // Add the case manager to the list ctm
                ctm.add(ctmadd);
                
            }  
        }

//add to list of only cases that have been cancelled/declined
        if(c.Status=='Cancelled/declined'){
            cid.add(c.id);}
    }
    if(SpecList.isEmpty()==false){
        insert SpecList;    }
    if(ctmdelete.isEmpty()==false){
        delete ctmdelete;}        
    if(ctm.isEmpty() == false){
        insert ctm;}
    
    List < Opportunity > OppUpdateList = [SELECT Id, StageName from Opportunity WHERE Case__c in : cid AND StageName <> 'Closed - Lost'];
    
    //Set those Opportunities to Closed - Lost
    for (Opportunity oppsupd: oppUpdateList) {
        oppsupd.StageName = 'Closed - Lost';
    }
    update oppUpdateList;
    
}

 

I have a junction object called Predecessor Cases that has two lookup fields back to cases.  The two fields are called successor case and predecessor case.
I created a custom button that executes some apex code.  When i click on the custom button, nothing is happening.  Am I not passing the right info to execute the apex code?

Apex Class:
global class AdjustDate {
    WebService static void AdjustDate(Id CaseId){         
        Set<ID> sid = new Set<ID>();     
        Integer DaysToAdd = 1;
        //Get a list of all Predecessor objects that has a match on the  predecessor field to the case ID            
        List<Predecessor_Case__c> successor = [SELECT ID,Predecessor_Case__c, Successor_Case__c, Successor_case__r.id FROM Predecessor_Case__c WHERE Predecessor_Case__r.id =: CaseId ];
        //Add the successor case ids that match those predecessor cases to a set 
        for(Predecessor_Case__c pc : successor){
            sid.add(pc.Successor_Case__r.id);
        }
        //Get a list of all cases that match that of the sid
        List<Case> successcases = [SELECT ID, Estimated_Launch_Date__c, Days_To_Add__c FROM Case WHERE ID IN: sid];
        // Do the calculations for those cases
        for(Case c: successcases){            
            c.Estimated_Launch_Date__c += DaysToAdd;
            c.Actual_Approval_Date__c = date.today();
        }  
    }
}


Custom button:
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}


sforce.apex.execute(
"AdjustDate",
"adjustdate",
{id:"{!Case.Id}"})