• Harsh Vardhan Agrawal 6
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies
I have to send email to contacts from an APEX batch class using a Custom Email template.

I have a custom email template. I have created multiple custom labels to create the email template as ours is a multi country org.

The requirement is to acheive the ability to send email to Contacts based upon a custom field called language__c. I have translations in custom label for countries - German, English.

If the language__c has de - the German translation of email template should be sent to the contact.
If the language__c has en - the English translation of email template should be sent to the contact.

How can I map the custom label that I have created and its translations to the custom field language__c on Contact so that the system understands which contact should receive which language in the email.
 
I am getting the above error for my code below. Can someone please explain what the issue might be ?



<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId">
    <aura:attribute name="buttonlabel1" type="String" default="Search Button"/>
    <aura:attribute name="buttonlabel2" type="String" default="New Button"/>
    <aura:attribute name="isNewAvailable" type="Boolean" default="false"/>
    
    <aura:attribute name="carTypes" type="String[]" default="['Hatchback','Sedan','Sports','SUV']"/>
    
    <lightning:layout horizontalAlign="center">
        <lightning:layoutItem padding="around-medium">
            <lightning:select name="selectItem" aura:id = "carType" label="Select Car Type" variant="label-hidden" class="label-hidden" value="1">
                 <option value ="1" text = "All Types"/>
                <aura:iteration items="{!v.carTypes}" var="carType">                    
                    <option value ="{!v.carType}" text="{!v.carType}"/>
                </aura:iteration>           
            </lightning:select>           
        </lightning:layoutItem>      
        
        <lightning:layoutItem padding="around-medium">
            <lightning:button label="{!v.buttonlabel1}" variant="brand"/>       
            <!-- display new button on availibility-->
            <aura:if isTrue="{!v.isNewAvailable}">
                <lightning:button label = "{!v.buttonlabel2}" variant="neutral"/>
                <aura:set attribute = "else">
                    New Button cannot be added here
                </aura:set>
            </aura:if>
            
            
        </lightning:layoutItem>        
    </lightning:layout>
</aura:component>

 
I have this code below - After inserting a new record, the values are getting overridden with another value as an after update is fired as soon as an after insert operation is complete.

As seen in the log 

trigger TPI_Update_MCCP_Prod_GoalEdit_on_MCCP on MC_Cycle_Plan_Product_vod__c (after update, after insert) {
    
    set<Id> prod_id=new set<Id>();
    set<Id> channel_id=new set<Id>();
    List<Id> target_id=new List<Id>();
    set<Id> target_id2=new set<Id>();   
    set<Id> plan_id=new set<Id>();
    set<Id> channel_id1=new set<Id>();
    set<Id> prod_id1=new set<Id>();
    decimal before_val;
    decimal after_val;
    string prod_name;
    AggregateResult[] groupedResults;
    integer aggregated_goal_val;
    decimal old_goal_val;
    decimal new_goal_val;
    set<Id> sum = new Set<Id>();
    boolean flag;
   
    List<MC_Cycle_Plan_vod__c> mcptoUpdate = New List<MC_Cycle_Plan_vod__c>();
    //    
    //hold value of products edited in set 'prod_id'
    //    
    for (MC_Cycle_Plan_Product_vod__c p: trigger.new)
    {      
        if(trigger.isInsert)
        {
            prod_name = Trigger.newMap.get(p.id).Product_Name_vod__c;          
            new_goal_val = Trigger.newMap.get(p.Id).Product_Activity_Goal_vod__c;  
            flag = true;
        }
        else if(trigger.isUpdate){
            old_goal_val = Trigger.oldMap.get(p.Id).Product_Activity_Goal_vod__c;
            prod_name = Trigger.oldMap.get(p.id).Product_Name_vod__c;
            new_goal_val = Trigger.newMap.get(p.Id).Product_Activity_Goal_vod__c;
            flag=false;
        }
        //        
        prod_id.add(p.id);        
        //      
        system.debug('MCCP product - ' + prod_id);
        System.debug('Old value of MCCP product - '+old_goal_val);
        //
        //hold values of channels related to products in set 'channel_id' where channel is 'Interaction'
        //      
        if(p.Channel_vod__c=='Interaction')
        {
            channel_id.add(p.Cycle_Plan_Channel_vod__c);
            system.debug('MCCP channel - ' + channel_id); 
        }
        //
        //hold values of target in 'target_id'
        //       
        List<MC_Cycle_Plan_Channel_vod__c> t= [select Cycle_Plan_Target_vod__c from MC_Cycle_Plan_Channel_vod__c where Id IN: channel_id];
        for (MC_Cycle_Plan_Channel_vod__c mc : t)
        {
            target_id.add(mc.Cycle_Plan_Target_vod__c);
        }
        system.debug('MCCP Target - ' + target_id);
        //
        //hold values of plan in 'plan_id'
        //      
        List<MC_Cycle_Plan_Target_vod__c> t2 = [select id,Cycle_Plan_vod__r.id from MC_Cycle_Plan_Target_vod__c where  Id IN : target_id];        
        for (MC_Cycle_Plan_Target_vod__c mc2 : t2)
        {
            plan_id.add(mc2.Cycle_Plan_vod__r.id);  
        }
        system.debug('MC Cycle Plan - ' + plan_id); 
        //
        //hold values of all target in 'target_id2'
        // 
        List<MC_Cycle_Plan_Target_vod__c> plan_1 = [select id from MC_Cycle_Plan_Target_vod__c where Cycle_Plan_vod__c in : plan_id];        
        for (MC_Cycle_Plan_Target_vod__c mc3 : plan_1)
        {
            target_id2.add(mc3.id);  
        }
        //
        //hold values of all channels of above targets where channel is 'Interaction' in 'channel_id1'
        // 
        system.debug('All MCCP Targets of above MC Cycle Plan - ' + target_id2);         
        List<MC_Cycle_Plan_Channel_vod__c> t3 = [select id,Channel_vod__c from MC_Cycle_Plan_Channel_vod__c where  Cycle_Plan_Target_vod__c IN : target_id2];       
        for (MC_Cycle_Plan_Channel_vod__c mc4 : t3)
        {
            if(mc4.Channel_vod__c=='Interaction'){
                channel_id1.add(mc4.id);  
            }            
        }
        system.debug('All MCCP Channels of above MCCP Target - ' + channel_id1);
        //    
        //hold value of products edited in set 'prod_id'
        //        
        List<MC_Cycle_Plan_Product_vod__c>  p1  = [select id,Product_Activity_Goal_vod__c from MC_Cycle_Plan_Product_vod__c where Cycle_Plan_Channel_vod__c in : channel_id1];
        
        for (MC_Cycle_Plan_Product_vod__c mc5 : p1)
        {
            prod_id1.add(mc5.id);
        }       
        system.debug('All MCCP Products of above MCCP Channels - '+ prod_id1);
        Set<id> result = prod_id1.clone();       
        system.debug('Removing the product where the value of goal is changed - ' + result.removeall(prod_id));
        system.debug('Resulting set of products where goal is not changed -'+result);
        List<MC_Cycle_Plan_Product_vod__c> mcp1 =[select Product_Name_vod__c from MC_Cycle_Plan_Product_vod__c where Id in: result];
        System.debug(mcp1.size());        
        //
        //     Aggregation of goals grouped by product with no change is stored in 'groupedResults'   
        //                               
        for(MC_Cycle_Plan_Product_vod__c aq : mcp1)
        {        
            if(aq.Product_Name_vod__c ==prod_name)
            {
                sum.add(aq.id);                
            }
        }    
        System.debug('Sum'+sum);
        groupedResults = [SELECT SUM(Product_Activity_Goal_vod__c)aver FROM MC_Cycle_Plan_Product_vod__c where Id in : sum]; 
        system.debug('Sum of goals grouped by product with no change - ' + integer.valueOf(groupedResults[0].get('aver')) );       
        //
        // typecasting the result into integer data type (aggregated_goal_val = total of all goals which are not edited)
        //         
        aggregated_goal_val  = integer.valueOf(groupedResults[0].get('aver')); 
        System.debug('Aggregated_goal_val in Insert - '+aggregated_goal_val);
        //
        //Old sum of goals (edited and non edited) grouped by product
        //
        system.debug('xxx'+trigger.isInsert);
        system.debug('xxx'+trigger.isUpdate);
        if(flag)
        {          
            before_val = aggregated_goal_val; 
            System.debug('Before Value in Insert - '+before_val);
            System.debug('Aggregated_goal_val in Insert - '+aggregated_goal_val);
        }
        else
        {
            before_val = aggregated_goal_val + old_goal_val;
            system.debug('Old value of edit goals -'+before_val);
        }
        //
        //New sum of goals (edited and non edited) grouped by product
        //
        after_val= aggregated_goal_val + new_goal_val;
        system.debug('New value of edit goals -'+after_val);
    }   
    List<MC_Cycle_Plan_vod__c> mcp =[select Product_1_TPI__c,Product_2_TPI__c,Product_3_TPI__c,Product_4_TPI__c,HQ_Target_1_TPI__c,HQ_Target_2_TPI__c,HQ_Target_3_TPI__c,HQ_Target_4_TPI__c,Proposed_Target_1_TPI__c,Proposed_Target_2_TPI__c,Proposed_Target_3_TPI__c,Proposed_Target_4_TPI__c from MC_Cycle_Plan_vod__c Where Id IN :plan_id];       
    for(MC_Cycle_Plan_vod__c mc:mcp){     
        for(MC_Cycle_Plan_Product_vod__c cyclprod : [Select Product_Name_vod__c from MC_Cycle_Plan_Product_vod__c WHERE ID =:prod_id]){                       
            if (mc.Product_1_TPI__c ==  ''){
                System.debug('Inserting in 1st prod');
                mc.Product_1_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_1_TPI__c = before_val;
                mc.Proposed_Target_1_TPI__c = after_val;
            }
            else if (mc.Product_1_TPI__c != '' && mc.Product_1_TPI__c == cyclprod.Product_Name_vod__c) 
            {
                System.debug('Updating in 1st prod');
                mc.Product_1_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_1_TPI__c = before_val;
                mc.Proposed_Target_1_TPI__c = after_val;
                system.debug('satisfy 1');
            }               
            else if (mc.Product_2_TPI__c == '')
            {
                System.debug('Inserting in 2nd prod');
                mc.Product_2_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_2_TPI__c = before_val;
                mc.Proposed_Target_2_TPI__c = after_val;
                system.debug('satisfy 2');
            }           
            else if (mc.Product_2_TPI__c != '' && mc.Product_2_TPI__c == cyclprod.Product_Name_vod__c)
            {
                System.debug('Updating in 2nd prod');         
                mc.Product_2_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_2_TPI__c = before_val;
                mc.Proposed_Target_2_TPI__c = after_val;
            }           
            else if (mc.Product_3_TPI__c == '')
            {
                System.debug('Inserting in 3rd prod');
                mc.Product_3_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_3_TPI__c = before_val;
                mc.Proposed_Target_3_TPI__c = after_val;                   
            }               
            else if (mc.Product_3_TPI__c != '' && mc.Product_3_TPI__c == cyclprod.Product_Name_vod__c)
            {
                System.debug('Updating in 3rd prod');
                mc.Product_3_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_3_TPI__c = before_val;
                mc.Proposed_Target_3_TPI__c = after_val;
            } 
            
            else if (mc.Product_4_TPI__c == '')
            {
                System.debug('Inserting in 4th prod');
                mc.Product_4_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_4_TPI__c = before_val;
                mc.Proposed_Target_4_TPI__c = after_val;                    
            }   
            else if (mc.Product_4_TPI__c != '' && mc.Product_4_TPI__c == cyclprod.Product_Name_vod__c)
            {
                System.debug('Inserting in 4th prod');
                mc.Product_4_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_4_TPI__c = before_val;
                mc.Proposed_Target_4_TPI__c = after_val;   
            }           
        }
        mcptoUpdate.add(mc);
    }
    try{
        update mcptoUpdate;
        system.debug('\n mc cycle plan after update -> '+mcptoUpdate);
    }
    catch(Exception ex){
        System.debug('\n exception aa => '+ex.getMessage());
    }      
}
I have this code below - After inserting a new record, the values are getting overridden with another value as an after update is fired as soon as an after insert operation is complete.

As seen in the log 

trigger TPI_Update_MCCP_Prod_GoalEdit_on_MCCP on MC_Cycle_Plan_Product_vod__c (after update, after insert) {
    
    set<Id> prod_id=new set<Id>();
    set<Id> channel_id=new set<Id>();
    List<Id> target_id=new List<Id>();
    set<Id> target_id2=new set<Id>();   
    set<Id> plan_id=new set<Id>();
    set<Id> channel_id1=new set<Id>();
    set<Id> prod_id1=new set<Id>();
    decimal before_val;
    decimal after_val;
    string prod_name;
    AggregateResult[] groupedResults;
    integer aggregated_goal_val;
    decimal old_goal_val;
    decimal new_goal_val;
    set<Id> sum = new Set<Id>();
    boolean flag;
   
    List<MC_Cycle_Plan_vod__c> mcptoUpdate = New List<MC_Cycle_Plan_vod__c>();
    //    
    //hold value of products edited in set 'prod_id'
    //    
    for (MC_Cycle_Plan_Product_vod__c p: trigger.new)
    {      
        if(trigger.isInsert)
        {
            prod_name = Trigger.newMap.get(p.id).Product_Name_vod__c;          
            new_goal_val = Trigger.newMap.get(p.Id).Product_Activity_Goal_vod__c;  
            flag = true;
        }
        else if(trigger.isUpdate){
            old_goal_val = Trigger.oldMap.get(p.Id).Product_Activity_Goal_vod__c;
            prod_name = Trigger.oldMap.get(p.id).Product_Name_vod__c;
            new_goal_val = Trigger.newMap.get(p.Id).Product_Activity_Goal_vod__c;
            flag=false;
        }
        //        
        prod_id.add(p.id);        
        //      
        system.debug('MCCP product - ' + prod_id);
        System.debug('Old value of MCCP product - '+old_goal_val);
        //
        //hold values of channels related to products in set 'channel_id' where channel is 'Interaction'
        //      
        if(p.Channel_vod__c=='Interaction')
        {
            channel_id.add(p.Cycle_Plan_Channel_vod__c);
            system.debug('MCCP channel - ' + channel_id); 
        }
        //
        //hold values of target in 'target_id'
        //       
        List<MC_Cycle_Plan_Channel_vod__c> t= [select Cycle_Plan_Target_vod__c from MC_Cycle_Plan_Channel_vod__c where Id IN: channel_id];
        for (MC_Cycle_Plan_Channel_vod__c mc : t)
        {
            target_id.add(mc.Cycle_Plan_Target_vod__c);
        }
        system.debug('MCCP Target - ' + target_id);
        //
        //hold values of plan in 'plan_id'
        //      
        List<MC_Cycle_Plan_Target_vod__c> t2 = [select id,Cycle_Plan_vod__r.id from MC_Cycle_Plan_Target_vod__c where  Id IN : target_id];        
        for (MC_Cycle_Plan_Target_vod__c mc2 : t2)
        {
            plan_id.add(mc2.Cycle_Plan_vod__r.id);  
        }
        system.debug('MC Cycle Plan - ' + plan_id); 
        //
        //hold values of all target in 'target_id2'
        // 
        List<MC_Cycle_Plan_Target_vod__c> plan_1 = [select id from MC_Cycle_Plan_Target_vod__c where Cycle_Plan_vod__c in : plan_id];        
        for (MC_Cycle_Plan_Target_vod__c mc3 : plan_1)
        {
            target_id2.add(mc3.id);  
        }
        //
        //hold values of all channels of above targets where channel is 'Interaction' in 'channel_id1'
        // 
        system.debug('All MCCP Targets of above MC Cycle Plan - ' + target_id2);         
        List<MC_Cycle_Plan_Channel_vod__c> t3 = [select id,Channel_vod__c from MC_Cycle_Plan_Channel_vod__c where  Cycle_Plan_Target_vod__c IN : target_id2];       
        for (MC_Cycle_Plan_Channel_vod__c mc4 : t3)
        {
            if(mc4.Channel_vod__c=='Interaction'){
                channel_id1.add(mc4.id);  
            }            
        }
        system.debug('All MCCP Channels of above MCCP Target - ' + channel_id1);
        //    
        //hold value of products edited in set 'prod_id'
        //        
        List<MC_Cycle_Plan_Product_vod__c>  p1  = [select id,Product_Activity_Goal_vod__c from MC_Cycle_Plan_Product_vod__c where Cycle_Plan_Channel_vod__c in : channel_id1];
        
        for (MC_Cycle_Plan_Product_vod__c mc5 : p1)
        {
            prod_id1.add(mc5.id);
        }       
        system.debug('All MCCP Products of above MCCP Channels - '+ prod_id1);
        Set<id> result = prod_id1.clone();       
        system.debug('Removing the product where the value of goal is changed - ' + result.removeall(prod_id));
        system.debug('Resulting set of products where goal is not changed -'+result);
        List<MC_Cycle_Plan_Product_vod__c> mcp1 =[select Product_Name_vod__c from MC_Cycle_Plan_Product_vod__c where Id in: result];
        System.debug(mcp1.size());        
        //
        //     Aggregation of goals grouped by product with no change is stored in 'groupedResults'   
        //                               
        for(MC_Cycle_Plan_Product_vod__c aq : mcp1)
        {        
            if(aq.Product_Name_vod__c ==prod_name)
            {
                sum.add(aq.id);                
            }
        }    
        System.debug('Sum'+sum);
        groupedResults = [SELECT SUM(Product_Activity_Goal_vod__c)aver FROM MC_Cycle_Plan_Product_vod__c where Id in : sum]; 
        system.debug('Sum of goals grouped by product with no change - ' + integer.valueOf(groupedResults[0].get('aver')) );       
        //
        // typecasting the result into integer data type (aggregated_goal_val = total of all goals which are not edited)
        //         
        aggregated_goal_val  = integer.valueOf(groupedResults[0].get('aver')); 
        System.debug('Aggregated_goal_val in Insert - '+aggregated_goal_val);
        //
        //Old sum of goals (edited and non edited) grouped by product
        //
        system.debug('xxx'+trigger.isInsert);
        system.debug('xxx'+trigger.isUpdate);
        if(flag)
        {          
            before_val = aggregated_goal_val; 
            System.debug('Before Value in Insert - '+before_val);
            System.debug('Aggregated_goal_val in Insert - '+aggregated_goal_val);
        }
        else
        {
            before_val = aggregated_goal_val + old_goal_val;
            system.debug('Old value of edit goals -'+before_val);
        }
        //
        //New sum of goals (edited and non edited) grouped by product
        //
        after_val= aggregated_goal_val + new_goal_val;
        system.debug('New value of edit goals -'+after_val);
    }   
    List<MC_Cycle_Plan_vod__c> mcp =[select Product_1_TPI__c,Product_2_TPI__c,Product_3_TPI__c,Product_4_TPI__c,HQ_Target_1_TPI__c,HQ_Target_2_TPI__c,HQ_Target_3_TPI__c,HQ_Target_4_TPI__c,Proposed_Target_1_TPI__c,Proposed_Target_2_TPI__c,Proposed_Target_3_TPI__c,Proposed_Target_4_TPI__c from MC_Cycle_Plan_vod__c Where Id IN :plan_id];       
    for(MC_Cycle_Plan_vod__c mc:mcp){     
        for(MC_Cycle_Plan_Product_vod__c cyclprod : [Select Product_Name_vod__c from MC_Cycle_Plan_Product_vod__c WHERE ID =:prod_id]){                       
            if (mc.Product_1_TPI__c ==  ''){
                System.debug('Inserting in 1st prod');
                mc.Product_1_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_1_TPI__c = before_val;
                mc.Proposed_Target_1_TPI__c = after_val;
            }
            else if (mc.Product_1_TPI__c != '' && mc.Product_1_TPI__c == cyclprod.Product_Name_vod__c) 
            {
                System.debug('Updating in 1st prod');
                mc.Product_1_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_1_TPI__c = before_val;
                mc.Proposed_Target_1_TPI__c = after_val;
                system.debug('satisfy 1');
            }               
            else if (mc.Product_2_TPI__c == '')
            {
                System.debug('Inserting in 2nd prod');
                mc.Product_2_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_2_TPI__c = before_val;
                mc.Proposed_Target_2_TPI__c = after_val;
                system.debug('satisfy 2');
            }           
            else if (mc.Product_2_TPI__c != '' && mc.Product_2_TPI__c == cyclprod.Product_Name_vod__c)
            {
                System.debug('Updating in 2nd prod');         
                mc.Product_2_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_2_TPI__c = before_val;
                mc.Proposed_Target_2_TPI__c = after_val;
            }           
            else if (mc.Product_3_TPI__c == '')
            {
                System.debug('Inserting in 3rd prod');
                mc.Product_3_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_3_TPI__c = before_val;
                mc.Proposed_Target_3_TPI__c = after_val;                   
            }               
            else if (mc.Product_3_TPI__c != '' && mc.Product_3_TPI__c == cyclprod.Product_Name_vod__c)
            {
                System.debug('Updating in 3rd prod');
                mc.Product_3_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_3_TPI__c = before_val;
                mc.Proposed_Target_3_TPI__c = after_val;
            } 
            
            else if (mc.Product_4_TPI__c == '')
            {
                System.debug('Inserting in 4th prod');
                mc.Product_4_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_4_TPI__c = before_val;
                mc.Proposed_Target_4_TPI__c = after_val;                    
            }   
            else if (mc.Product_4_TPI__c != '' && mc.Product_4_TPI__c == cyclprod.Product_Name_vod__c)
            {
                System.debug('Inserting in 4th prod');
                mc.Product_4_TPI__c = cyclprod.Product_Name_vod__c;
                mc.HQ_Target_4_TPI__c = before_val;
                mc.Proposed_Target_4_TPI__c = after_val;   
            }           
        }
        mcptoUpdate.add(mc);
    }
    try{
        update mcptoUpdate;
        system.debug('\n mc cycle plan after update -> '+mcptoUpdate);
    }
    catch(Exception ex){
        System.debug('\n exception aa => '+ex.getMessage());
    }      
}