• Ajith Selvaraj 2
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 2
    Replies
I want to execute trigger whenever an email is send from Activity tab. I have a trigger which is executing whenever Log a call, New Event and Email is created. But I want to execute only for Email. Is it possible, anyone help me in this.
public static workOrderRecordFormApex SAPgetPicklistValues(string recordTypeId, string objectName,string fieldApiName){
        Map<String, String>  piclistValues=new Map<String, String>();
        workOrderRecordFormApex objs = new workOrderRecordFormApex();
        Httprequest req = new HttpRequest();
        String SESSION_ID_START = 'SESSION_ID_START';
        String SESSION_ID_END = 'SESSION_ID_END';
        String pageContent = Page.SessionId.getContent().toString();
        Integer startIndex = pageContent.indexOf(SESSION_ID_START) + SESSION_ID_START.length();
        Integer endIndex = pageContent.indexOf(SESSION_ID_END);
        String sessionId =pageContent.substring(startIndex, endIndex);
          req.setEndpoint(
            URL.getSalesforceBaseUrl().toExternalForm() +
            '/services/data/v50.0/ui-api/object-info/' +
            objectName +
            '/picklist-values/' +
            recordTypeId +
            '/' +
            fieldApiName
        );
        req.setMethod('GET');
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
           req.setHeader('Authorization', 'Bearer ' + sessionId);
        Http http = new Http();
        httpresponse response=http.send(req);
        picklistWrapper obj=new picklistWrapper();
        obj=(picklistWrapper)JSON.deserialize(response.getBody(), picklistWrapper.class);
        String JSONstr = response.getBody();
        JSONParser parser = JSON.createParser(JSONstr);
        Map<String, Object> defaultValueMap = (Map<String, Object>)JSON.deserializeUntyped(JSONstr);
        for(piclistValues pickVal:obj.values){
            
            piclistValues.put(pickVal.value, pickVal.label);
        }
        objs.defaultSAPOrderType = defaultValueMap.get('defaultValue');
        objs.sapOrderTypeValues = piclistValues;
        system.debug('objs' + objs);
        return objs;
    }


    private static List<PicklistEntryWrapper> wrapPicklistEntries(List<Schema.PicklistEntry> PLEs) {
        return (List<PicklistEntryWrapper>)
            JSON.deserialize(JSON.serialize(PLEs), List<PicklistEntryWrapper>.class);
    }
    public class picklistWrapper{
        @AuraEnabled public piclistValues[] values;
    }
    public class piclistValues {
        @AuraEnabled public String label;    
        @AuraEnabled public String value;
    }
    public class PicklistEntryWrapper{
        public String active {get;set;}
        public String defaultValue {get;set;}
        public String label {get;set;}
        public String value {get;set;}
        public String validFor {get;set;}
        public PicklistEntryWrapper(){            
        }
        
    }
    @AuraEnabled
    public object defaultSAPOrderType {get; set;}
    @AuraEnabled
    public Map<String, String> sapOrderTypeValues {get; set;}
    public class SObJectResult {
        @AuraEnabled
        public String recName;
        @AuraEnabled
        public Id recId;
        
        public SObJectResult(String recNameTemp, Id recIdTemp) {
            recName = recNameTemp;
            recId = recIdTemp;
        }
    }


In this above code Im getting the picklist value for field, where the values is based on record type, the code is working fine, But for the test class Im calling that method like 
QuoteComponentCtrl.SAPgetPicklistValues(salesRecordTypeId, 'WorkOrder', 'SAP_Order_Type__c');
But Im getting error as Methods defined as TestMethod do not support getContent call. I have created on vf page name as SessionId to get the end point. Kindly help me to resolve the error in test class
can anyone explain me how to cover the code after the ctach block?
and my code is 
 public static List<Category_Target__c> saveCategoryTargets(List<Category_Target__c> categoryTargets) {
        String brandTargetId = categoryTargets[0].Brand_Target__c;
            Brand_Target1__c brandTarget = [SELECT Id,Period__c FROM Brand_Target1__c WHERE Id =:  brandTargetId];
            Range__c[] ranges = [SELECT Id FROM Range__c WHERE Period__c = : brandTarget.Period__c];
        if(categoryTargets != null && categoryTargets.size() > 0) {
            String error='';
          try{
                insert categoryTargets;
            }
            catch(DmlException e){             
                for (Integer i = 0; i < e.getNumDml(); i++) {
                    error =+ e.getDmlMessage(i) +  '\n' ;
                }
                throw new AuraHandledException(error);             
            }catch(Exception e){
                throw new AuraHandledException(e.getMessage());
            }          
             if(ranges.size() > 0) {
                List<Category_Range_Target__c> categoryRangeTargets = new List<Category_Range_Target__c>();
                for(Category_Target__c categoryTarget : categoryTargets)  {
                    for(Range__c range : ranges) {
                        Category_Range_Target__c categoryRangeTarget = new Category_Range_Target__c();
                        categoryRangeTarget.Category_Target__c = categoryTarget.Id;
                        categoryRangeTarget.Range__c = range.Id;
                        categoryRangeTargets.add(categoryRangeTarget);
                    }
                }
                String error1='';
                try{
                    insert categoryRangeTargets;
                }
                catch(DmlException e){             
                    for (Integer i = 0; i < e.getNumDml(); i++) {
                        error1 =+ e.getDmlMessage(i) +  '\n' ;
                    }
                    throw new AuraHandledException(error1);             
                }catch(Exception e){
                    throw new AuraHandledException(e.getMessage());
                } 
            }
            categoryTargets = [SELECT Id,Target__c,Category__c, Brand_Target__c,Brand_Target__r.Target__c,  (SELECT Id, Target__c FROM Category_Range_Targets__r) FROM Category_Target__c WHERE Id IN :categoryTargets];            
        }
        return categoryTargets;
    }
I have more than 500 records in invoice which is in related list to one account and the name of the account is "Account1"(Invoice- Account: Lookup relationship). Now Im try to insert more than 10 records into Invoice for the same account using dataloader. In that time only Im getting an error like "System.LimitException: Too many SOQL queries: 101". 
And my code is public class CommercientSF21InvoiceTriggerHandler1 {
    public static void accountUpdate(List<Invoice__c> currentRecordId){
        List<Account> accUpdate = new List<Account>();
        List<Account> accountWithoutInvoice = new List<Account>();
        Id accountId;
        for(Invoice__c acc : currentRecordId ){
            accountId = acc.Account__c;
        }
        List<Invoice__c> invoiceList = [select Id,CommercientSF21_IVM_InvoiceDate__c from Invoice__c where Account__c=: accountId];
        System.debug(invoiceList.size());
         for(Account acc : [select id,Current_Year_Revenue__c,Last_Order_Date__c,
                           Previous_Year_Revenue__c from Account where Id =:accountId]){
                               if(invoiceList.size() == 0){
                                   acc.Current_Year_Revenue__c = NULL;
                                   acc.Lifetime_Value_LTV__c = NULL;
                                   acc.Previous_Year_Revenue__c = NULL;
                                   accountWithoutInvoice.add(acc);
                               }
                           }
        map<id,account> accmap = new map<id,account>();
        accmap.putall(accountWithoutInvoice);
        if(accmap.size()>0){
            update accmap.values();
        }
        Set<Id> currentYearRecordIds = new Set<Id>();
        Set<Id> previousYearRecordIds = new Set<Id>();
        for(integer i=0; i<invoiceList.size(); i++){ 
            Datetime record = invoiceList[i].CommercientSF21_IVM_InvoiceDate__c;
            if(record != NULL){ 
                String yearoftherecord = String.valueOf(record.Year());
                Integer curentYear = Date.Today().Year();
                String currentYearInteger = String.valueOf(curentYear);
                Integer previousyear = curentYear -1;
                String previousyearInteger = String.valueOf(previousyear);
                if(currentYearInteger == yearoftherecord ){
                    currentYearRecordIds.add(invoiceList[i].Id);
                }
                else if(previousyearInteger == yearoftherecord){
                    previousYearRecordIds.add(invoiceList[i].Id);
                }
            }
        }
        List<AggregateResult> currentYearAmountList = [SELECT SUM(CommercientSF21_IVM_InvoiceAmt_c__c )tot FROM Invoice__c WHERE Id= :currentYearRecordIds];
        List<AggregateResult> previousYearAmountList = [SELECT SUM(CommercientSF21_IVM_InvoiceAmt_c__c )tot FROM Invoice__c WHERE Id= :previousYearRecordIds];
        List<AggregateResult> totalAmountList = [SELECT SUM(CommercientSF21_IVM_InvoiceAmt_c__c )tot FROM Invoice__c WHERE Account__c =: accountId ];
        System.debug(totalAmountList);
        Object sumAmountCurrentYear;
        Object sumAmountPreviousYear; 
        Object sumAmountTotalYear;
        for(AggregateResult acc : currentYearAmountList){
            sumAmountCurrentYear = acc.get('tot');  
        }
        for(AggregateResult acc : previousYearAmountList){
            sumAmountPreviousYear = acc.get('tot');  
        }
        
        for(AggregateResult acc : totalAmountList){
            sumAmountTotalYear = acc.get('tot');  
        }
        for(Account acc: [select id,Current_Year_Revenue__c,Last_Order_Date__c,
                          Previous_Year_Revenue__c from Account where Id =: accountId] ){
                              acc.Previous_Year_Revenue__c = Integer.valueOf(sumAmountPreviousYear);
                              acc.Current_Year_Revenue__c = Integer.valueOf(sumAmountCurrentYear);
                              acc.Lifetime_Value_LTV__c = Integer.valueOf(sumAmountTotalYear);
                              accUpdate.add(acc);
                          }
        map<id,account> accMapToUpdate = new map<id,account>();
        accMapToUpdate.putall(accUpdate);
        if(accMapToUpdate.size()>0){
            update accMapToUpdate.values();
        } 
    } 
}
When I try to update more than 100 records in CommercientsF21__Invoice__c I'm getting error like this Error: System.LimitException: Too many SOQL queries: 101
 and my code is 
public class CommercientSF21InvoiceTriggerHandler1 {
    public static void accountUpdate(List<CommercientSF21__Invoice__c> currentRecordId){
        System.debug(currentRecordId);
        List<Account> accUpdate = new List<Account>();
        List<Account> accountWithoutInvoice = new List<Account>();
        List<CommercientSF21__Invoice__c> invoiceList = [select Id,
                                                         CommercientSF21__IVM_InvoiceDate__c from CommercientSF21__Invoice__c where CommercientSF21__Account__c=:currentRecordId[0].CommercientSF21__Account__c];
        for(Account acc : [select id,Current_Year_Revenue__c,Last_Order_Date__c,
                           Previous_Year_Revenue__c from Account where Id =:currentRecordId[0].CommercientSF21__Account__c]){
                               if(invoiceList.size() == 0){
                                   acc.Current_Year_Revenue__c = NULL;
                                   acc.Lifetime_Value_LTV__c = NULL;
                                   acc.Previous_Year_Revenue__c = NULL;
                                   accountWithoutInvoice.add(acc);
                               }
                               
                           }
        map<id,account> accmap = new map<id,account>();
        accmap.putall(accountWithoutInvoice);
        if(accmap.size()>0){
            update accmap.values();
        }
        Set<Id> currentYearRecordIds = new Set<Id>();
        Set<Id> previousYearRecordIds = new Set<Id>();
        for(integer i=0; i<invoiceList.size(); i++){ 
            Datetime record = invoiceList[i].CommercientSF21__IVM_InvoiceDate__c;
            if(record != NULL){ 
                String yearoftherecord = String.valueOf(record.Year());
                Integer curentYear = Date.Today().Year();
                String currentYearInteger = String.valueOf(curentYear);
                Integer previousyear = curentYear -1;
                String previousyearInteger = String.valueOf(previousyear);
                if(currentYearInteger == yearoftherecord ){
                    currentYearRecordIds.add(invoiceList[i].Id);
                }
                else if(previousyearInteger == yearoftherecord){
                    previousYearRecordIds.add(invoiceList[i].Id);
                }
            }
        }
     //   List<CommercientSF21__Invoice__c> dummy = [SELECT id,CommercientSF21__IVM_InvoiceAmt__c from CommercientSF21__Invoice__c
       //                                            where Id =:currentRecordId[0].CommercientSF21__Account__c];
        List<AggregateResult> currentYearAmountList = [SELECT SUM(CommercientSF21__IVM_InvoiceAmt__c )tot FROM CommercientSF21__Invoice__c WHERE Id= :currentYearRecordIds];
        System.debug(currentYearAmountList);
        List<AggregateResult> previousYearAmountList = [SELECT SUM(CommercientSF21__IVM_InvoiceAmt__c )tot FROM CommercientSF21__Invoice__c WHERE Id= :previousYearRecordIds];
        System.debug(previousYearAmountList);
        List<AggregateResult> totalAmountList = [SELECT SUM(CommercientSF21__IVM_InvoiceAmt__c )tot FROM CommercientSF21__Invoice__c WHERE Id IN : currentRecordId ];
        System.debug(totalAmountList);
        Object sumAmountCurrentYear = currentYearAmountList[0].get('tot');
        Object sumAmountPreviousYear = previousYearAmountList[0].get('tot');
        Object sumAmountTotalYear = totalAmountList[0].get('tot');
        for(Account acc: [select id,Current_Year_Revenue__c,Last_Order_Date__c,
                          Previous_Year_Revenue__c from Account where Id =:currentRecordId[0].CommercientSF21__Account__c] ){
                              acc.Previous_Year_Revenue__c = Integer.valueOf(sumAmountPreviousYear);
                              acc.Current_Year_Revenue__c = Integer.valueOf(sumAmountCurrentYear);
                              acc.Lifetime_Value_LTV__c = Integer.valueOf(sumAmountTotalYear);
                              accUpdate.add(acc);
                          }
        map<id,account> accMapToUpdate = new map<id,account>();
        accMapToUpdate.putall(accUpdate);
        if(accMapToUpdate.size()>0){
            update accMapToUpdate.values();
        }
    
    }
}
how to display n input fields in lightning component. where n is the value get from a user. Is it possible.? suppose im getting 5 as input value and I want to display 5 input field in same page. here is the small snipet. but dot know to make more changes in it. please help with this.
<aura:component >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="keywords" type="List" default="[{value:''},{value:''},{value:''},{value:''}]" />
    <aura:iteration items="{!v.keywords}" var="item" indexVar="i">
    <lightning:input label="Keyword" value="{!item.value}" />
</aura:iteration>
</aura:component>

({
	doInit : function(component, event, helper) {
      alert('1');  
      var keywords = component.get("v.keywords").map(item => item.value);
        alert('1');
	}
})
Hi Team,
I have a requirement to convert my picklist value to date in a new field.
My picklist values are as May 2020, June 2021. Now I want to convert this value to a date format in the new field as 05/01/2020.
And all my values should be converted to 1st day of the month which is selected in the picklist.
Example:

Jan 2020-  01/01/2020
March 2021- 03/01/2021
April 2013 -  04/01/2013

It should be mm/dd/yyyy. And "dd" should be always 01.
i have searched many formulas to convert it to the required format, but could not achieve.
Please help me in achieving it.

Thanks,
Mahesh