• Vivek Garg 13
  • NEWBIE
  • 0 Points
  • Member since 2023
  • Developer


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 5
    Replies
i didnt understand this scenario ,Can any one know how to create this by using flow and how to send chatter to this users SSAM Implementation Specialist or Account Manager ?

project is a Object & Create IP is field

Create a new chatter when: 
Create IP(field)  = 'True'
Chatter the SSAM Implementation Specialist or Account Manager
 @ Person
'The TIC has completed the creation of the IP Package for (Project Name)'
Link to Project

I'm parsing multiple invoices with N Products in each. I want to create Opportunities based on each invoice and each invoices product data. So I expect my Opportunities and OpportunityLineItems to be in relations like this:
- TestCorp (opportunity name):
- Products:
- Pen
- Book
- BioTech:
- Products:
- Laser
- Optical Fiber
- Beam Splitter etc...
But I'm getting
- TestCorp:
- Pen
- Pen
- BioTech:
- Book
- Book
- Book
Here is the logic to parse and store the data:

public with sharing class InvoiceParser {
    public static HttpResponse response = InvoiceCallout.makeGetCallout();
    public static JSONParser parser = JSON.createParser(response.getBody());
    public static Invoice invoiceJson = (Invoice)parser.readValueAs(Invoice.class);
    private static List<Opportunity> opportunities = createOpportunity();

    public static Pricebook2 createPriceBook() {
        Pricebook2 pricebook2 = new Pricebook2(Name='New Price Book', IsActive=true);
        insert pricebook2;
        return pricebook2;
    }

    public static List<Opportunity> createOpportunity() {
        List<Opportunity> opportunities = new List<Opportunity>();
        System.debug('invoiceJson.files --- '  + invoiceJson.files);
        for (Invoice.Files file : invoiceJson.files) {
            opportunities.add(new Opportunity(
                    Name=file.bill_from != null ? file.bill_from : 'New Opportunity',
                    StageName='Closed Won',
                    CloseDate=Date.valueOf(file.invoice_date)
            ));
        }
        System.debug(opportunities);
        insert opportunities;
        return opportunities;
    }

    public static List<PricebookEntry> createPriceBookEntries() {
        List<PricebookEntry> pricebookEntries = new List<PricebookEntry>();
        List<PricebookEntry> standardPriceBookEntries = new List<PricebookEntry>();
        Pricebook2 pricebook2 = createPriceBook();
        Pricebook2 standardPricebook2 = [SELECT Id FROM Pricebook2 WHERE IsStandard=TRUE];
        for (Invoice.Files file : invoiceJson.files) {
            for (Opportunity opportunity : opportunities) {
                System.debug('FILE ============' + file);
                List<Product2> product2s = createProducts(file, opportunity);
                for (Integer j = 0; j < product2s.size(); j++) {
                    System.debug('Product2 ===== ' + product2s);
                    Decimal lineTotal = file.items[j]?.get('line_total') != null ? Decimal.valueOf(file.items[j]?.get('line_total')) : null;
                    Decimal amount = file.items[j]?.get('amount') != null ? Decimal.valueOf(file.items[j]?.get('amount')) : null;
                    standardPriceBookEntries.add(new PricebookEntry(
                            UnitPrice = 10,
                            Pricebook2Id = standardPricebook2.Id,
                            Product2Id = product2s[j].Id,
                            IsActive=true
                    ));
                    pricebookEntries.add(new PricebookEntry(
                            UnitPrice = lineTotal != null ? lineTotal : amount,
                            Pricebook2Id = pricebook2.Id,
                            Product2Id = product2s[j].Id,
                            IsActive=true
                    ));
                }
                opportunity.Pricebook2Id = pricebook2.Id;
            }
        }
        insert standardPriceBookEntries;
        insert pricebookEntries;
        System.debug('PBEs ======' + pricebookEntries);
        update opportunities;
        return pricebookEntries;
    }

    public static List<Product2> createProducts(Invoice.Files file, Opportunity opportunity) {
        System.debug('OPPORTUNITIES ++++++++++++++ '+opportunities);
        List<Product2> product2s = new List<Product2>();
        for (Integer i = 0; i < file.items.size(); i++) {
            String item = file.items[i]?.get('item') != null ? file.items[i]?.get('item') : null;
            String itemAndDescription = file.items[i]?.get('item_and_description') != null ? file.items[i]?.get('item_and_description') : null;
            if (item != null) {
                product2s.add(new Product2(Name=item,Opportunity__r = opportunity));
            }
            if (itemAndDescription != null) {
                product2s.add(new Product2(Name=itemAndDescription, Opportunity__r = opportunity));
            }
        }
        insert product2s;
        System.debug('PRODUCTS ========= ' + product2s);
        return product2s;
    }

    @AuraEnabled
    public static void sendInvoiceData() {
        List<OpportunityLineItem> oppLineItems = new List<OpportunityLineItem>();
        List<PricebookEntry> pricebookEntries = createPriceBookEntries();
        for (Invoice.Files file : invoiceJson.files) {
            for (Integer i  = 0; i < file.items.size(); i++) {
                Decimal quantity = file.items[i]?.get('quantity') != null ? Decimal.valueOf(file.items[i]?.get('quantity')) : null;
                Decimal qty      = file.items[i]?.get('qty') != null ? Decimal.valueOf(file.items[i]?.get('qty')) : null;
                Decimal tax = file?.tax == null ? 0.0 : Decimal.valueOf(file?.tax);
                oppLineItems.add(new OpportunityLineItem(
                        Bill_From__c = file.bill_from != null ? file.bill_from : null, Bill_To__c = file.bill_to,
                        Invoice_Number__c = file.invoice_number != null ? file.invoice_number : null,
                        Invoice_Date__c = Date.valueOf(file.invoice_date),
                        Invoice_Total__c = file.total != null ? Decimal.valueOf(file.total) : null,
                        Invoice_Tax__c = tax,
                        InvoiceAmountDue__c = file.amount_due != null ? Decimal.valueOf(file.amount_due) : Decimal.valueOf(file.balance_due),
                        Invoice_Subtotal__c = file.subtotal != null ? Decimal.valueOf(file.subtotal) : Decimal.valueOf(file.sub_total),
                        Item_Name__c =  file.items[i]?.get('item') != null ? file.items[i].get('item') : file.items[i].get('item_and_description'),
                        Description__c = file.items[i]?.get('description') != null ? file.items[i].get('description') : '',
                        Quantity = quantity != null ? quantity : qty,
                        Item_Cost__c = file.items[i]?.get('unit_cost') != null ? Decimal.valueOf(file.items[i]?.get('unit_cost')) :
                                Decimal.valueOf(file.items[i]?.get('rate')),
                        TotalPrice = file.items[i]?.get('line_total') != null ? Decimal.valueOf(file.items[i]?.get('line_total')) :
                                Decimal.valueOf(file.items[i]?.get('amount')),
                        OpportunityId = opportunities[i]?.Id,
                        PricebookEntryId=pricebookEntries[i].Id
                ));
            }
        }
        System.debug(response.getBody());
        System.debug('OppLineItems ===== ' + oppLineItems);
        insert oppLineItems;
    }
}


Btw system.debug() says that Products and Opportunities creating correctly but it seems that I need to link them correctly but how? I've tried to create custom field on the Product to link with opportunity but it doesn't work anyway.

6:42:35.913 (3743236068)|USER_DEBUG|[67]|DEBUG|OPPORTUNITIES ++++++++++++++ (Opportunity:{Name=TestCorp, StageName=Closed Won, CloseDate=2022-08-05 00:00:00, Id=0067R00001wS7y6QAC}, Opportunity:{Name=New Opportunity, StageName=Closed Won, CloseDate=2021-04-30 00:00:00, Id=0067R00001wS7y7QAC}, Opportunity:{Name=BioTech_Inc., StageName=Closed Won, CloseDate=2022-09-23 00:00:00, Id=0067R00001wS7y8QAC}, Opportunity:{Name=TestCorp, StageName=Closed Won, CloseDate=2022-08-05 00:00:00, Id=0067R00001wS7y9QAC}, Opportunity:{Name=New Opportunity, StageName=Closed Won, CloseDate=2021-04-30 00:00:00, Id=0067R00001wS7yAQAS}, Opportunity:{Name=BioTech_Inc., StageName=Closed Won, CloseDate=2022-09-23 00:00:00, Id=0067R00001wS7yBQAS})
16:42:35.913 (3755913867)|USER_DEBUG|[42]|DEBUG|Product2 ===== (Product2:{Name=Laser, Id=01t7R00000A06uCQAR}, Product2:{Name=Beam Splitter, Id=01t7R00000A06uDQAR}, Product2:{Name=Optical Fiber, Id=01t7R00000A06uEQAR})
16:42:35.913 (3756134383)|USER_DEBUG|[42]|DEBUG|Product2 ===== (Product2:{Name=Laser, Id=01t7R00000A06uCQAR}, Product2:{Name=Beam Splitter, Id=01t7R00000A06uDQAR}, Product2:{Name=Optical Fiber, Id=01t7R00000A06uEQAR})
16:42:35.913 (3756274503)|USER_DEBUG|[42]|DEBUG|Product2 ===== (Product2:{Name=Laser, Id=01t7R00000A06uCQAR}, Product2:{Name=Beam Splitter, Id=01t7R00000A06uDQAR}, Product2:{Name=Optical Fiber, Id=01t7R00000A06uEQAR})

What's wrong here?

I have a class that selects target date and case id from an object CaseMilestone and then it puts it into a list and then it mappes the target date as the milestone date... I want to write a test class for it but my test class only covers 57% of the code... It is unable to enter the IF condition present in the main class

THE CODE FOR THE CLASS IS:
public class DisplayCaseMilestoneDateCtrl {
    @AuraEnabled(cacheable=true)
    public static string getCaseMilestoneDate(String caseId){
        List<CaseMilestone> cmList = [SELECT targetDate,caseId FROM CaseMilestone where caseId =: caseId];
        if(cmList != null && cmList.size() > 0){
            DateTime dt = cmList[0].targetDate;
            string milestoneDate = dt.format('d-MMM-yyyy hh:mm a');
            return milestoneDate;
        }
        else{
            return 'No milestone date to show.';
        }
    }
}

 

And THE CODE FOR THE TEST CLASS WITH 57% code coverage is:
@isTest
public class DisplayCaseMilestoneDateCtrlTest {
    static testmethod void testmethod1() {
        List<Id> idss = new List<Id>();
        CaseMilestone cms = new CaseMilestone();
        idss.add(cms.CaseId);
        Test.startTest();
        DisplayCaseMilestoneDateCtrl.getCaseMilestoneDate('500N000000J0MkfIAF');
        Test.stopTest();
        
    }
    
}

Hello All hope are doing good, I have several log files in a webDAV (salesforce SFDC logs repo) and I want to drop all of them to a linux server, so that I can feed them to Splunk Server Class. I am not sure if there is a better idea than this, please advise.

Thanks
Sarahjohn