• Supraja Kallakuri
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 4
    Replies
I am trying to write a trigger to provide roll up summary on lookup relationships. Opportunity has a custom Account look up, in this case. The trigger should tell me how many deals are in expiring, expired stages and in total.

Here is my trigger.

trigger PAO on Opportunity (after delete,after update, after insert, after undelete) {
list<Account> Partneraccountlisttotal = New list<Account>();
list<Account> Partneraccountlistexpiring = New list<Account>();
list<Account> Partneraccountlistexpired = New list<Account>();    
set<id> partneraccountIDs = new set<id>();
List <AggregateResult> aggresultstotal = new List<AggregateResult>();
List <AggregateResult> aggresultsexpiring = new List<AggregateResult>();
List <AggregateResult> aggresultsexpired = new List<AggregateResult>();
    
if(trigger.isInsert){
  for(Opportunity  o : Trigger.new){
      partneraccountIDs.add(o.Partner_Account__r.ID);  
  }
}
else if(trigger.isDelete){
    for(Opportunity  o: Trigger.old){
       partneraccountIDs.add(o.Partner_Account__r.ID);
        }
}
else if(trigger.isUnDelete){
  for(Opportunity  o : Trigger.new){
      partneraccountIDs.add(o.Partner_Account__r.ID);
      
}
}
else if(trigger.isUpdate){
     for(Opportunity  o : trigger.new){
partneraccountIDs.add(o.Partner_Account__c);         
if(trigger.oldmap.get(o.id).Partner_Account__c!=o.Partner_Account__c){
partneraccountIDs.add(trigger.oldmap.get(o.id).Partner_Account__r.ID);

}
}
}
    

aggresultstotal = [SELECT COUNT(Id), Partner_Account__c FROM Opportunity where Partner_Register__C = TRUE AND 
              Partner_Account__c IN :partneraccountIDs GROUP BY Partner_Account__c];
   for(AggregateResult ar:aggresultstotal) {
      Partneraccountlisttotal.add(new Account(
                Id = (ID)ar.get('Partner_Account__c'), 
                PAO_Total__C = (Integer)ar.get('expr0')));
   }
    
    aggresultsexpiring = [SELECT COUNT(Id), Partner_Account__c FROM Opportunity where Partner_Register__C = TRUE AND Deal_Expiring__C = TRUE
                              AND Partner_Account__c IN :partneraccountIDs GROUP BY Partner_Account__c];
   for(AggregateResult ar:aggresultsexpiring) {
       Partneraccountlistexpiring.add(new Account(
                Id = (ID)ar.get('Partner_Account__c'), 
                PAO_Expiring__C = (Integer)ar.get('expr0')));
      }
    
aggresultsexpired = [SELECT COUNT(Id), Partner_Account__c FROM Opportunity where Partner_Register__C = TRUE AND Deal_Expired__C = TRUE 
                             AND Partner_Account__c IN :partneraccountIDs GROUP BY Partner_Account__c];
   for(AggregateResult ar:aggresultsexpired) {
       Partneraccountlistexpired.add(new Account(
                Id = (ID)ar.get('Partner_Account__c'), 
                PAO_Expired__C = (Integer)ar.get('expr0')));
      }
       

      update Partneraccountlisttotal; 
      update Partneraccountlistexpiring;
      update Partneraccountlistexpired;
  
        
     
}

And my test class-

@istest
public class TestPAO {
    Static testmethod void testpao(){
        Account a = New Account();
        a.Name = 'Test Account1';
        a.Account_Type2__c = 'Advisor';
        a.PAO_Expiring__c = 0;
        a.PAO_Total__C = 0;
        a.PAO_Expired__c =0;
       
        Insert a;
        
        Account b = New Account();
        b.Name = 'Test Account2';
        b.Account_Type2__c = 'Advisor';
        b.PAO_Expiring__c = 0;
        b.PAO_Total__C = 0;
        b.PAO_Expired__c =0;
        Insert b;
        
         Account a1 = New Account();
        a1.Name = 'Test Account3';
        a1.Account_Type2__c = 'Advisor';
        a1.PAO_Expiring__c = 0;
        a1.PAO_Total__C = 0;
        a1.PAO_Expired__c =0;
        Insert a1;
        
        Account b1 = New Account();
        b1.Name = 'Test Account4';
        b1.Account_Type2__c = 'Advisor';
        b1.PAO_Expiring__c = 0;
        b1.PAO_Total__C = 0;
        b1.PAO_Expired__c =0;
        Insert b1;
        
     
        
        Opportunity o = New Opportunity();
        o.Name = 'Test opportunity';
        o.Account = a;
        o.Partner_Account__c = a1.ID;
        o.Type = 'New Business';
        o.CloseDate = system.Today().addDays(10);
        o.StageName ='Negotiating';
        o.Partner_Register__C = TRUE;
        o.Deal_Expiring__c = False;
        o.Deal_Expired__C = False;
        Insert o;
        
        Opportunity o2 = New Opportunity();
        o2.Name = 'Test opportunity';
        o2.Account = b;
        o2.Partner_Account__c = b1.ID;
        o2.Type = 'New Business';
        o2.CloseDate = system.Today().addDays(10);
        o2.StageName ='Negotiating';
        o2.Partner_Register__C = TRUE;
        o2.Deal_Expiring__c = True;
        o2.Deal_Expired__c = False;
        Insert o2;
         
        List<Account> test1 = [Select ID FROM Account WHERE PAO_Total__C = 1];
        List<Account> test2 = [Select ID FROM Account WHERE PAO_Expiring__C = 1];
        List<Account> test3 = [Select ID FROM Account WHERE PAO_Expiring__C = 0];
        
        system.assertEquals(test1.size(), 2);
        system.assertEquals(test2.size(), 1);
        system.assertEquals(test3.size(), 3);
        
        
     Test.startTest();
   
        o2.Deal_Expiring__c = False;
        update o2;
        
        List<Account> test4 = [Select ID FROM Account WHERE PAO_Expiring__C = 0];
        List<Account> test5 = [Select ID FROM Account WHERE PAO_Expired__C = 1];
        
        system.assertEquals(test4.size(), 4);
        system.assertEquals(test5.size(), 1);
          
        delete o2;
        
        List<Account> test6 = [Select ID FROM Account WHERE PAO_Expiring__C = 0];
        List<Account> test7 = [Select ID FROM Account WHERE PAO_Expired__C = 1];
        system.assertEquals(test6.size(), 4);
        system.assertEquals(test7.size(), 0);
        
      test.stopTest();
    }

}

Issue - System.AssertException: Assertion Failed

Any suggestions would be highly appreciated. Thanks!

 
Idea - When a contact is created, check if the associated account's primary_contact_sfid__c field is filled or not. If it is empty, put the contact's id in there. When I test the code, there is a stem assetion failure. Please advise. Thanks!

trigger updateacc on Contact (after insert) {
    
   List<ID> accountids = New List<ID>();
   
  
    for(Contact c : trigger.new){
        if(c.Accountid != null){
            accountids.add(c.AccountId);
        }
        }
     
      List<Account> acclist = [SELECT ID, Primary_Contact_SFID__C FROM Account WHERE ID IN: accountids];  
      List<Account> updateacc = New List<Account>(); 

    
    for(Contact c : trigger.new){
        for(Account acc: acclist){
            if(c.AccountId ==acc.id && c.Account.Primary_Contact_SFID__c == null){
                acc.Primary_Contact_SFID__c =''+c.id;
                updateacc.add(acc);
            }
        }
    }

  
    update (updateacc);
}
    
When I try to test this code, it throws an error saying ' System.SObjectException: DML statment cannot operate on trigger.new or trigger.old'

Any ideas why?

Trigger MasterRFSTrigger on Account(before insert, before update){
 Set<String> chaincodes = New Set<String>();
    List<Account> Accslist = New List<Account>();
     
    if(trigger.isinsert){
        for (Account a :trigger.new){
            if (a.Other_Chain__c!=null){
                chaincodes.add(a.Other_Chain__c);
  
            }
        }
    }else if(trigger.isupdate){       
            for(Account a : trigger.new){
                String oldchaincode = trigger.oldmap.get(a.id).Chaincode__c;
                String newchaincode = a.Chaincode__c;
                if(newchaincode != oldchaincode){
                    chaincodes.add(newchaincode);
                }
            }
        }
    Map<String, Chain__c> chainconfig = new Map<String, Chain__c>();
    List<Chain__c> chains = [SELECT Name, Chain_Name__c, Company__c FROM Chain__c WHERE Name in : chaincodes];
    for(Chain__c c : chains){
        chainconfig.put(c.Name, c);
    }
     
    for(Account a :trigger.new){
        Chain__c Chain =chainconfig.get(a.Chaincode__c);
        if(Chain != null){
            a.Chainname__c = Chain.Chain_Name__c;
            a.Company__c =Chain.Company__c;
        }Accslist.add(a);
        }
     Insert Accslist;
        
}
I have an aggregate function in my class

Public List<AggregateResult> getGroupedResults() {
      return  [SELECT SUM(MTD_Revenue_Combined__c)Total,  Owner.ManagerId  FROM Account WHERE Owner.ManagerId = '00500000006pSSm' AND MTD_Revenue_Combined__c != 0.00  GROUP BY Owner.ManagerId LIMIT 50000];
    }

IT still throws the 50000 Limit error. Any ideas would be helpful. Thanks!
code coverage is 73% What can I do to improve it?I was trying o deploy  my code to the production org and failed as the code coverage is below 75%. Can someone suggest a way to improve my code coverage? Thanks!


Controller 


public with sharing class contrller {
Public id Current_Acc_Id;

    public contrller(ApexPages.StandardController controller) {
    Current_Acc_Id = controller.getRecord().id;
    }

    public List<Contact> getrelatedContacts(){
        List <contact> conList = New List<Contact>();
        for(Account acc:[select id,name,(select name,email, phone, LastModifiedDate from contacts) from account where id=:Current_Acc_Id]){
           for(contact con:acc.contacts)
               conList.add(con); 
        }
        return conList;
    }
}


Test Class


@isTest(seeAllData=False)
private class Contrller_Test2{
  static void setupData(){
    
    Account testAccount = new Account(Name = 'Test', Chainname__c = '123',  Chain_Code__c ='456', Company__c = '789');
    insert testAccount;
      
    Contact testcontact = new Contact ( LastName = 'Test', Account = testAccount);
    insert testcontact;
    
  }
 
  static testMethod void getrelatedContacts(){
   
    Account myAccount = [SELECT Id, Name FROM Account WHERE Name = 'Test' ];
  
    
     List <contact> myconList = New List<Contact>();
        for(Account acc:[select id,name,(select name,email, phone, LastModifiedDate from contacts) from account where id=:myAccount.id]){
           for(contact con:acc.contacts)
               myconList.add(con);
               }
               system.assertequals(1, myconlist.size());
               }  
           }
Can someone help me in integrating sendgrid into my own org? I need to send bulk email(for campaigns) to a specified list of recepients. 

I took some help from here. https://github.com/sendgrid/sendgrid-apex

But when I tried implementing it in my or, I always had a compiling error
Error Error: Compile Error: Illegal assignment from SendGrid.SendGridResponse to String

Please suggest. Thanks for your time.
I am trying to write a trigger to provide roll up summary on lookup relationships. Opportunity has a custom Account look up, in this case. The trigger should tell me how many deals are in expiring, expired stages and in total.

Here is my trigger.

trigger PAO on Opportunity (after delete,after update, after insert, after undelete) {
list<Account> Partneraccountlisttotal = New list<Account>();
list<Account> Partneraccountlistexpiring = New list<Account>();
list<Account> Partneraccountlistexpired = New list<Account>();    
set<id> partneraccountIDs = new set<id>();
List <AggregateResult> aggresultstotal = new List<AggregateResult>();
List <AggregateResult> aggresultsexpiring = new List<AggregateResult>();
List <AggregateResult> aggresultsexpired = new List<AggregateResult>();
    
if(trigger.isInsert){
  for(Opportunity  o : Trigger.new){
      partneraccountIDs.add(o.Partner_Account__r.ID);  
  }
}
else if(trigger.isDelete){
    for(Opportunity  o: Trigger.old){
       partneraccountIDs.add(o.Partner_Account__r.ID);
        }
}
else if(trigger.isUnDelete){
  for(Opportunity  o : Trigger.new){
      partneraccountIDs.add(o.Partner_Account__r.ID);
      
}
}
else if(trigger.isUpdate){
     for(Opportunity  o : trigger.new){
partneraccountIDs.add(o.Partner_Account__c);         
if(trigger.oldmap.get(o.id).Partner_Account__c!=o.Partner_Account__c){
partneraccountIDs.add(trigger.oldmap.get(o.id).Partner_Account__r.ID);

}
}
}
    

aggresultstotal = [SELECT COUNT(Id), Partner_Account__c FROM Opportunity where Partner_Register__C = TRUE AND 
              Partner_Account__c IN :partneraccountIDs GROUP BY Partner_Account__c];
   for(AggregateResult ar:aggresultstotal) {
      Partneraccountlisttotal.add(new Account(
                Id = (ID)ar.get('Partner_Account__c'), 
                PAO_Total__C = (Integer)ar.get('expr0')));
   }
    
    aggresultsexpiring = [SELECT COUNT(Id), Partner_Account__c FROM Opportunity where Partner_Register__C = TRUE AND Deal_Expiring__C = TRUE
                              AND Partner_Account__c IN :partneraccountIDs GROUP BY Partner_Account__c];
   for(AggregateResult ar:aggresultsexpiring) {
       Partneraccountlistexpiring.add(new Account(
                Id = (ID)ar.get('Partner_Account__c'), 
                PAO_Expiring__C = (Integer)ar.get('expr0')));
      }
    
aggresultsexpired = [SELECT COUNT(Id), Partner_Account__c FROM Opportunity where Partner_Register__C = TRUE AND Deal_Expired__C = TRUE 
                             AND Partner_Account__c IN :partneraccountIDs GROUP BY Partner_Account__c];
   for(AggregateResult ar:aggresultsexpired) {
       Partneraccountlistexpired.add(new Account(
                Id = (ID)ar.get('Partner_Account__c'), 
                PAO_Expired__C = (Integer)ar.get('expr0')));
      }
       

      update Partneraccountlisttotal; 
      update Partneraccountlistexpiring;
      update Partneraccountlistexpired;
  
        
     
}

And my test class-

@istest
public class TestPAO {
    Static testmethod void testpao(){
        Account a = New Account();
        a.Name = 'Test Account1';
        a.Account_Type2__c = 'Advisor';
        a.PAO_Expiring__c = 0;
        a.PAO_Total__C = 0;
        a.PAO_Expired__c =0;
       
        Insert a;
        
        Account b = New Account();
        b.Name = 'Test Account2';
        b.Account_Type2__c = 'Advisor';
        b.PAO_Expiring__c = 0;
        b.PAO_Total__C = 0;
        b.PAO_Expired__c =0;
        Insert b;
        
         Account a1 = New Account();
        a1.Name = 'Test Account3';
        a1.Account_Type2__c = 'Advisor';
        a1.PAO_Expiring__c = 0;
        a1.PAO_Total__C = 0;
        a1.PAO_Expired__c =0;
        Insert a1;
        
        Account b1 = New Account();
        b1.Name = 'Test Account4';
        b1.Account_Type2__c = 'Advisor';
        b1.PAO_Expiring__c = 0;
        b1.PAO_Total__C = 0;
        b1.PAO_Expired__c =0;
        Insert b1;
        
     
        
        Opportunity o = New Opportunity();
        o.Name = 'Test opportunity';
        o.Account = a;
        o.Partner_Account__c = a1.ID;
        o.Type = 'New Business';
        o.CloseDate = system.Today().addDays(10);
        o.StageName ='Negotiating';
        o.Partner_Register__C = TRUE;
        o.Deal_Expiring__c = False;
        o.Deal_Expired__C = False;
        Insert o;
        
        Opportunity o2 = New Opportunity();
        o2.Name = 'Test opportunity';
        o2.Account = b;
        o2.Partner_Account__c = b1.ID;
        o2.Type = 'New Business';
        o2.CloseDate = system.Today().addDays(10);
        o2.StageName ='Negotiating';
        o2.Partner_Register__C = TRUE;
        o2.Deal_Expiring__c = True;
        o2.Deal_Expired__c = False;
        Insert o2;
         
        List<Account> test1 = [Select ID FROM Account WHERE PAO_Total__C = 1];
        List<Account> test2 = [Select ID FROM Account WHERE PAO_Expiring__C = 1];
        List<Account> test3 = [Select ID FROM Account WHERE PAO_Expiring__C = 0];
        
        system.assertEquals(test1.size(), 2);
        system.assertEquals(test2.size(), 1);
        system.assertEquals(test3.size(), 3);
        
        
     Test.startTest();
   
        o2.Deal_Expiring__c = False;
        update o2;
        
        List<Account> test4 = [Select ID FROM Account WHERE PAO_Expiring__C = 0];
        List<Account> test5 = [Select ID FROM Account WHERE PAO_Expired__C = 1];
        
        system.assertEquals(test4.size(), 4);
        system.assertEquals(test5.size(), 1);
          
        delete o2;
        
        List<Account> test6 = [Select ID FROM Account WHERE PAO_Expiring__C = 0];
        List<Account> test7 = [Select ID FROM Account WHERE PAO_Expired__C = 1];
        system.assertEquals(test6.size(), 4);
        system.assertEquals(test7.size(), 0);
        
      test.stopTest();
    }

}

Issue - System.AssertException: Assertion Failed

Any suggestions would be highly appreciated. Thanks!

 
Idea - When a contact is created, check if the associated account's primary_contact_sfid__c field is filled or not. If it is empty, put the contact's id in there. When I test the code, there is a stem assetion failure. Please advise. Thanks!

trigger updateacc on Contact (after insert) {
    
   List<ID> accountids = New List<ID>();
   
  
    for(Contact c : trigger.new){
        if(c.Accountid != null){
            accountids.add(c.AccountId);
        }
        }
     
      List<Account> acclist = [SELECT ID, Primary_Contact_SFID__C FROM Account WHERE ID IN: accountids];  
      List<Account> updateacc = New List<Account>(); 

    
    for(Contact c : trigger.new){
        for(Account acc: acclist){
            if(c.AccountId ==acc.id && c.Account.Primary_Contact_SFID__c == null){
                acc.Primary_Contact_SFID__c =''+c.id;
                updateacc.add(acc);
            }
        }
    }

  
    update (updateacc);
}
    
I have been asked to create an Email Alert for the Opportunity Owner and his/her boss when an opportunity has remained in a stage for 30 days, then another alert, when time in stage = 60, and so on. Can you please help or advise a workaround.

I have tried to specify the ISCHANGED function, but the picklist would not accept this function. 

Could you let me know about this please.

Thanks