• Richa Paliwal 18
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 5
    Replies
I am getting on 50% coverage on this trigger below:
Note: Sales order is a child object of opportunity and all I am odoing is updating a custom field on parent when a child is inserted or updated.

trigger ContractSourceUpdateOnOpp on Sales_Order__c (after insert, after update) {
    if(Trigger.isInsert){
            try {
                for (Sales_Order__c SO : Trigger.new){
                    Opportunity Op = [SELECT Id, Contract_Source__c  FROM Opportunity WHERE Id = :SO.Opportunity__c];
                    
                    List<Sales_Order__c> l_so = [SELECT Id, Contract_Source__c FROM Sales_Order__c WHERE Opportunity__c = :Op.Id];
                    for(Sales_Order__c cs_so : l_so) {
                        Op.Contract_Source__c = cs_so.Contract_Source__c;  
                    }
                    //Op.Contract_Source__c = ContractSource;

                    update Op;
                }
            } catch (Exception e) {
                System.debug(e);
            }
        }


        if(Trigger.isUpdate){
            try {
                for (Sales_Order__c SO : Trigger.old){
                    Opportunity Op = [SELECT Id, Contract_Source__c  FROM Opportunity WHERE Id = :SO.Opportunity__c];
                    system.debug('Contract Source on opp##' +Op.Contract_Source__c);
                    
                    system.debug('Id@@' +Op.Id);
                    List<Sales_Order__c> l_so = [SELECT Id, Contract_Source__c FROM Sales_Order__c WHERE Opportunity__c = :Op.Id];
                    for(Sales_Order__c cs_so : l_so) {
                    
                        
                       Op.Contract_Source__c = cs_so.Contract_Source__c;
                        
                        system.debug('Contract Source##' +Op.Contract_Source__c);
                    }
                   
                    system.debug('contract source updated');
                    update Op;
                   system.debug('Opp##');
                }
            } catch (Exception e) {
                System.debug(e);
            }
        }


}



Here is my test class:
@istest
public class TestContractSourceUpdateOnOpp {
    static testmethod void validateContractSource()
    {
        
        Test_SharedNewAccount.createAccount('Test Account');
                        // Test Creation of the Shared Account
        Account acct = [SELECT Id FROM Account WHERE Name = 'Test Account' LIMIT 1];
        System.Assert(acct != null, 'Account was not inserted properly!');
        
                  //Create a Opportunity
 
        Opportunity Op = new Opportunity(ACCOUNTID = acct.Id, Name = 'TestOp',Anticipatged_1st_Test_Season__c = 'Fall 2017',ForecastCategoryName = 'Prospect & Discovery',Imp_Case_Created__c = null ,Closed_Won_Reason__c = 'Relationships', Closed_Won_Detail__c = 'Relationship with Influencer', Region__c = 'East Enrollments' , Type = 'Renewal',Test_Alignment_Option__c = 'Other (Specify in Notes)' , StageName = 'Prospect', License_Start_Date__c = date.newInstance(2018,03,01), License_End_Date__c = date.newInstance(2018,03,31), CloseDate = Date.newInstance(2018,03,30), MAP_Growth_MAP_Skills_Implementation__c = 'Standard');
        insert Op;
        system.debug('Insert opportunity' + Op.Id);
        system.debug('Insert opportunity' + Op.contract_Source__c);  
        
        Sales_Order__c SO = new Sales_Order__c(Name = 'Test Sales Order', Opportunity__c = Op.Id, Account__c = acct.id, Contract_Source__c = 'Test Contract Source');
        insert SO;
              
        system.debug('Sales order inserted' +SO);
        Test.StartTest();   
        
        {        
            
            
            SO.Contract_Source__c = 'Test Source';
           update SO;
                                        system.debug('Try updating Sales order with Contract Status'+SO.Contract_Source__c);
            
             system.debug('opp contract source' + Op.Contract_Source__c);
        
           System.assert(Op.Contract_Source__c == 'Test Source');            
                                system.debug('Catch the validation'+Op);
        }
 
        Test.StopTest();
    }

}




this debug line shows 'Null'.

Can someone please suggest what am I doing wrong?
 
 I need to create a trigger that can send email off of opportunity and send the template that has opportunity merge fields.I get the email from this trigger but it is missing the merge field. My trigger looks like below:

trigger ImplementationProject_EmailAlert on Opportunity (after update) {

      List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();

    for(Opportunity newItem : trigger.new) {


        
        if (newItem.StageName == 'Closed Won ')
        system.debug('stagename@@' +newItem.stageName);
        {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
//List <Contact> Con= [select id, name from Contact where name =:'Richa Paliwal']; 
            
            //system.debug('contact id@@' +Con);
            
//mail.setTargetObjectId(Con[0].id); 
mail.setSenderDisplayName('Salesforce Support'); 
mail.setUseSignature(false); 
mail.setBccSender(false); 
mail.setSaveAsActivity(false); 
EmailTemplate et=[Select id, Subject, Body from EmailTemplate where Name=:'ZSYSGEN_EXPEDITED Close Won Notification']; 
             system.debug('Template id@@' +et);
mail.setTemplateId(et.id); 
          mails.add(mail);

        }
        }

Messaging.SendEmailResult [] r = Messaging.sendEmail(mails);

}

and out of the box template looks like:
The following Opportunity includes Expedited Implementation as a product and has reached Closed Won stage: 

Date Closed Won: {!Opportunity.CloseDate} 
Opportunity: {!Opportunity.Name} 
Opportunity Type: {!Opportunity.Type} 

Account: {!Opportunity.Account} 
Agency Code: {!Opportunity.Agency_Code__c} 
Shipping State: {!Opportunity.Shipping_State__c} 

Implementation Note: {!Opportunity.Implementation_Notes__c} 

Opportunity Description: {!Opportunity.Description} 


Opportunity Link: {!Opportunity.Link}

 
I need to send a HTML Email template when opportunity is marked closed won and 2 custom fields on opportunity products are set to 'yes'. Can someone please suggest me how can I achieve this? 
I am getting on 50% coverage on this trigger below:
Note: Sales order is a child object of opportunity and all I am odoing is updating a custom field on parent when a child is inserted or updated.

trigger ContractSourceUpdateOnOpp on Sales_Order__c (after insert, after update) {
    if(Trigger.isInsert){
            try {
                for (Sales_Order__c SO : Trigger.new){
                    Opportunity Op = [SELECT Id, Contract_Source__c  FROM Opportunity WHERE Id = :SO.Opportunity__c];
                    
                    List<Sales_Order__c> l_so = [SELECT Id, Contract_Source__c FROM Sales_Order__c WHERE Opportunity__c = :Op.Id];
                    for(Sales_Order__c cs_so : l_so) {
                        Op.Contract_Source__c = cs_so.Contract_Source__c;  
                    }
                    //Op.Contract_Source__c = ContractSource;

                    update Op;
                }
            } catch (Exception e) {
                System.debug(e);
            }
        }


        if(Trigger.isUpdate){
            try {
                for (Sales_Order__c SO : Trigger.old){
                    Opportunity Op = [SELECT Id, Contract_Source__c  FROM Opportunity WHERE Id = :SO.Opportunity__c];
                    system.debug('Contract Source on opp##' +Op.Contract_Source__c);
                    
                    system.debug('Id@@' +Op.Id);
                    List<Sales_Order__c> l_so = [SELECT Id, Contract_Source__c FROM Sales_Order__c WHERE Opportunity__c = :Op.Id];
                    for(Sales_Order__c cs_so : l_so) {
                    
                        
                       Op.Contract_Source__c = cs_so.Contract_Source__c;
                        
                        system.debug('Contract Source##' +Op.Contract_Source__c);
                    }
                   
                    system.debug('contract source updated');
                    update Op;
                   system.debug('Opp##');
                }
            } catch (Exception e) {
                System.debug(e);
            }
        }


}



Here is my test class:
@istest
public class TestContractSourceUpdateOnOpp {
    static testmethod void validateContractSource()
    {
        
        Test_SharedNewAccount.createAccount('Test Account');
                        // Test Creation of the Shared Account
        Account acct = [SELECT Id FROM Account WHERE Name = 'Test Account' LIMIT 1];
        System.Assert(acct != null, 'Account was not inserted properly!');
        
                  //Create a Opportunity
 
        Opportunity Op = new Opportunity(ACCOUNTID = acct.Id, Name = 'TestOp',Anticipatged_1st_Test_Season__c = 'Fall 2017',ForecastCategoryName = 'Prospect & Discovery',Imp_Case_Created__c = null ,Closed_Won_Reason__c = 'Relationships', Closed_Won_Detail__c = 'Relationship with Influencer', Region__c = 'East Enrollments' , Type = 'Renewal',Test_Alignment_Option__c = 'Other (Specify in Notes)' , StageName = 'Prospect', License_Start_Date__c = date.newInstance(2018,03,01), License_End_Date__c = date.newInstance(2018,03,31), CloseDate = Date.newInstance(2018,03,30), MAP_Growth_MAP_Skills_Implementation__c = 'Standard');
        insert Op;
        system.debug('Insert opportunity' + Op.Id);
        system.debug('Insert opportunity' + Op.contract_Source__c);  
        
        Sales_Order__c SO = new Sales_Order__c(Name = 'Test Sales Order', Opportunity__c = Op.Id, Account__c = acct.id, Contract_Source__c = 'Test Contract Source');
        insert SO;
              
        system.debug('Sales order inserted' +SO);
        Test.StartTest();   
        
        {        
            
            
            SO.Contract_Source__c = 'Test Source';
           update SO;
                                        system.debug('Try updating Sales order with Contract Status'+SO.Contract_Source__c);
            
             system.debug('opp contract source' + Op.Contract_Source__c);
        
           System.assert(Op.Contract_Source__c == 'Test Source');            
                                system.debug('Catch the validation'+Op);
        }
 
        Test.StopTest();
    }

}




this debug line shows 'Null'.

Can someone please suggest what am I doing wrong?
 
 I need to create a trigger that can send email off of opportunity and send the template that has opportunity merge fields.I get the email from this trigger but it is missing the merge field. My trigger looks like below:

trigger ImplementationProject_EmailAlert on Opportunity (after update) {

      List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();

    for(Opportunity newItem : trigger.new) {


        
        if (newItem.StageName == 'Closed Won ')
        system.debug('stagename@@' +newItem.stageName);
        {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
//List <Contact> Con= [select id, name from Contact where name =:'Richa Paliwal']; 
            
            //system.debug('contact id@@' +Con);
            
//mail.setTargetObjectId(Con[0].id); 
mail.setSenderDisplayName('Salesforce Support'); 
mail.setUseSignature(false); 
mail.setBccSender(false); 
mail.setSaveAsActivity(false); 
EmailTemplate et=[Select id, Subject, Body from EmailTemplate where Name=:'ZSYSGEN_EXPEDITED Close Won Notification']; 
             system.debug('Template id@@' +et);
mail.setTemplateId(et.id); 
          mails.add(mail);

        }
        }

Messaging.SendEmailResult [] r = Messaging.sendEmail(mails);

}

and out of the box template looks like:
The following Opportunity includes Expedited Implementation as a product and has reached Closed Won stage: 

Date Closed Won: {!Opportunity.CloseDate} 
Opportunity: {!Opportunity.Name} 
Opportunity Type: {!Opportunity.Type} 

Account: {!Opportunity.Account} 
Agency Code: {!Opportunity.Agency_Code__c} 
Shipping State: {!Opportunity.Shipping_State__c} 

Implementation Note: {!Opportunity.Implementation_Notes__c} 

Opportunity Description: {!Opportunity.Description} 


Opportunity Link: {!Opportunity.Link}

 
I need to send a HTML Email template when opportunity is marked closed won and 2 custom fields on opportunity products are set to 'yes'. Can someone please suggest me how can I achieve this? 
So the structure I have is this:

Record of Custom Object Type A has a related list of multiple records of Custom Object Type B.


And this is what I'm looking to do:

Object Type B has a Currency field, and Object Type A has a field that is inteded to be the Sum of that Currency field for all records in the related list. Whenever any record of Object Type B is updated, I need the Sum field on Object Type A to calculate the new sum. (Essentially it is a rollup sum, but I cannot use a rollup field because these do not have a Master-Detail relationship.)


Example:

Initial State:
  • Object Type A Record SumField = 100
    • Related ObjectB Record 1 CurrencyField = 50
    • Related ObjectB Record 2 CurrencyField = 30
    • Related ObjectB Record 3 CurrencyField = 20
User Action:
  • Related ObjectB Record 2 CurrencyField is changed from 30 to 80
Desired End State:
  • Object Type A Record SumField = 150
    • Related ObjectB Record 1 CurrencyField = 50
    • Related ObjectB Record 2 CurrencyField = 80
    • Related ObjectB Record 3 CurrencyField = 20