• Won Young Jung
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
public class CreatePayableInvoiceController {
    
     private final Multi_Vendor_Invoice__c MVI;
        public ID newRecordId {get;set;}
    public CreatePayableInvoiceController(ApexPages.StandardController controller){
                        this.MVI = (Multi_Vendor_Invoice__c)controller.getRecord();

    }
    
    public pagereference createPayableInvoice(){
        
        if(MVI.Status__c != 'Approved'){
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'This Invoice must be Approved before a Payable Invoice can be generated.'));
            return Null;        
        }
        
        
        Date Today = Date.Today();
            //get invoice setting
            Vendor_Invoice_Settings__c settings = Vendor_Invoice_Settings__c.getValues('Default Vendor Settings');
        
        
            Map<String, String> GLAsMap = new Map<String, String>();
       
        
        //and a list 
        List<String> GLAsMaplist = new List<String>();
        GLAsMaplist.add(settings.Billable_Expenses_GLA__c);
        GLAsMaplist.add(settings.Non_Billable_Expenses_GLA__c);
        GLAsMaplist.add(settings.Adjustment_GLA__c);
        //GLAsMaplist.add(settings.TimeDefaultGLAAccount__c);
        
        //get GLA's
       List<c2g__codaGeneralLedgerAccount__c> GLAs = [SELECT id, Name 
                                                               from c2g__codaGeneralLedgerAccount__c
                                                               where Name in: GLAsMaplist];
       
        for(c2g__codaGeneralLedgerAccount__c GLA : GLAs){
            GLAsMap.put(GLA.Name, GLA.id);
        }
        
        
         
        
        //create Payable Invoice
        c2g__codaPurchaseInvoice__c PI = new c2g__codaPurchaseInvoice__c();
        PI.c2g__Account__c = MVI.Account__c;
        //PI.ThisWilWork__c = MVI.id;
        PI.c2g__Reference1__c = MVI.Name;
        //PI.ffap__DeriveDueDate__c = true;
        //PI.ffap__DerivePeriod__c = true;
        PI.c2g__InvoiceDate__c = MVI.Invoice_Date__c;
        PI.c2g__AccountInvoiceNumber__c = MVI.Vendor_Invoice_Number__c;
        PI.c2g__Period__c = MVI.Period__c;
       PI.c2g__InvoiceCurrency__c = MVI.Accounting_Currency_lookup__c;
        //PI.ffap__DeriveCurrency__c = true;
       PI.c2g__InvoiceDescription__c = MVI.Invoice_Description__c;
        system.debug('Invoice trying to create: ' + PI);
        try{
            insert PI;
            system.debug('Created Invoice: ' + PI);
        }catch(DMLException e){
            system.debug('FAILED TO CREATE INVOICE: ' + PI + 'GOT ERROR: ' + e);
            ApexPages.addMessages(e);
        
            
            Return Null;
        }
        
        
        //get a list of all the invoice items
            List<Multi_Vendor_Invoice_Item__c> MVIIs = [Select id, Timecard__c, Vendor_Invoice__c, Line_Type__c, Quantity__c, Rate__c, Value__c, Expense__c, CurrencyIsoCode, Project_Name__c, Region_Name__c, Line_Description__c, Billable__c, Resource__r.Name, First_Date__c, Last_Date__c 
                                                       from Multi_Vendor_Invoice_Item__c
                                                       WHERE Vendor_Invoice__c =: MVI.id];
        
        //create Payable Invoice Items List (regular) and add to the list depending on the MVII line type
        List<c2g__codaPurchaseInvoiceLineItem__c> PILIs = New List<c2g__codaPurchaseInvoiceLineItem__c>();
        
        //create Payable Invoice Expense Line Items list and add to the list depending on the MVII line type
        List<c2g__codaPurchaseInvoiceExpenseLineItem__c> PIELs = New List<c2g__codaPurchaseInvoiceExpenseLineItem__c>();
        
        //Create list of Regions Reflected by Invoice Items
        Set<String> RegionNames = New Set<String>();
        
        For(Multi_Vendor_Invoice_Item__c MVI : MVIIs){
            RegionNames.add(MVI.Region_Name__c);
        }
       
        //get a list of dimensions from the region name list
        List<c2g__codaDimension1__c> Dimensions = [SELECT Id, Name, c2g__ExternalId__c 
                                                from c2g__codaDimension1__c 
                                                where Name in: RegionNames];
        
        //Create map for dimensions and Regions
        Map<String, c2g__codaDimension1__c> DimensionsAndRegions = new Map<String, c2g__codaDimension1__c>();
        for(c2g__codaDimension1__c Dimension : Dimensions){
            if(RegionNames.contains(Dimension.Name)){
                DimensionsAndRegions.put(Dimension.Name, Dimension);
            }
        }
        
        //create resource Set - Resource name as unique value
        //
        Set<String> ResourceSet = new Set<String>();
        //Create project set - Projcet name as unique value
        Set<String> ProjectSet = new Set<String>();
        
        
        //add relevant items to the lists
        for(Multi_Vendor_Invoice_Item__c MVII : MVIIs ){
            
            //Add to Resource Set
            ResourceSet.add(MVII.Resource__r.Name);
            ProjectSet.add(MVII.Project_Name__c);            
            
                //Create Purchase invoice item and assign properties 
                              c2g__codaPurchaseInvoiceExpenseLineItem__c PIEL = New c2g__codaPurchaseInvoiceExpenseLineItem__c();
 
             if(MVII.Line_Type__c == 'Timecard'){
                //Add to resource Map
                 
                 
                PIEL.c2g__NetValue__c = MVII.Value__c;
                PIEL.Timecard_Split__c = MVII.Timecard__c;
                PIEL.CurrencyIsoCode = MVII.CurrencyIsoCode; 
                PIEL.c2g__PurchaseInvoice__c = PI.id;
                 PIEL.ffpsai__Analysis1__c = MVII.Project_Name__c;
                 
                 if(DimensionsAndRegions.get(MVII.Region_Name__c) != NULL){
                      PIEL.c2g__Dimension1__c = DimensionsAndRegions.get(MVII.Region_Name__c).id;
 
                 }
                 
                 PIEL.c2g__LineDescription__c = MVII.Line_Description__c;
                // PIEL.c2g__GeneralLedgerAccount__c = GLAsMap.get(settings.TimeDefaultGLAAccount__c);
                
                system.debug(PIEL.id);
                system.debug(PIEL.c2g__PurchaseInvoice__c);
                PIELs.add(PIEL);
            }
            if(MVII.Line_Type__c == 'Expense'){
                
                
                PIEL.c2g__NetValue__c = MVII.Value__c;
                PIEL.ffpsai__Expense__c = MVII.Expense__c;
                PIEL.c2g__PurchaseInvoice__c = PI.id;
                PIEL.ffap__SetGLAToDefault__c = FALSE;
                PIEL.ffpsai__Analysis1__c = MVII.Project_Name__c;
                if(DimensionsAndRegions.get(MVII.Region_Name__c) != NULL){
                    PIEL.c2g__Dimension1__c = DimensionsAndRegions.get(MVII.Region_Name__c).id;
                    
                 }
                PIEL.c2g__LineDescription__c = MVII.Line_Description__c; 
                if(MVII.Billable__c == TRUE){
                    PIEL.c2g__GeneralLedgerAccount__c = GLAsMap.get(settings.Billable_Expenses_GLA__c);
                }else{
                    PIEL.c2g__GeneralLedgerAccount__c = GLAsMap.get(settings.Non_Billable_Expenses_GLA__c);
                }
                system.debug('PIEL: ' + PIEL);
                
                PIELs.add(PIEL);
            }
            if(MVII.Line_Type__c == 'Invoice Adjustment'){
                if(MVII.Value__c == NULL){
                    PIEL.c2g__NetValue__c = 0.00;
                }else{
                    PIEL.c2g__NetValue__c = MVII.Value__c; 
                }
                 PIEL.c2g__PurchaseInvoice__c = PI.id;
                 PIEL.ffap__SetGLAToDefault__c = FALSE;
                 PIEL.c2g__GeneralLedgerAccount__c = GLAsMap.get(settings.Adjustment_GLA__c);
                 PIEL.c2g__LineDescription__c = MVII.Line_Description__c;
                 PIEL.ffpsai__Analysis1__c = MVII.Project_Name__c;
                   
                 if(DimensionsAndRegions.get(MVII.Region_Name__c) != NULL){
                    PIEL.c2g__Dimension1__c = DimensionsAndRegions.get(MVII.Region_Name__c).id;
                    
                 }
                 PIELs.add(PIEL);


            }
            
        }
        
        try{
                insert PILIs;
            }catch(DMLException e){
                system.debug(e);
                ApexPages.addMessages(e);
                return NULL;

            }
        
        try{
            insert PIELs;
        }catch(DMLException e){
            system.debug(e);
            ApexPages.addMessages(e);
            return NULL;
        }
        
        MVI.Payable_Invoice__c = PI.id;
        
        try{
            update MVI;
        }catch(DMLException e){
            system.debug(e);
        }
        
        //add all keys from resource map to set and then iterate over them to grab values from map
        String descriptionString = '';
        for(String project : ProjectSet){
            descriptionString = descriptionString + ', ' + project; 
        }
        //remove the first ","
        descriptionString = descriptionString.removeStart(',');
        descriptionString = descriptionString + ': ';
            
        String ResourceList = '';
        for(String resource: ResourceSet){
            ResourceList = ResourceList + ', ' + resource;
        }
        
        ResourceList = ResourceList.removeStart(',');//remove the leading comma
        //add the two strings together to make the full description
        descriptionString = descriptionString + ResourceList;
        
        system.debug('invoice description' + descriptionString);
                //set description
        PI.c2g__InvoiceDescription__c = descriptionString;
        
        system.debug('PI.c2g__InvoiceDescription__c' + PI.c2g__InvoiceDescription__c);
        try{
            update PI; 
        }catch(DMLException e){
            system.debug(e);
        }
        return new PageReference('/'+PI.id);
    }
}

Trying to upload this piece of code and 

User-added image

Bunch of test methods failing on me.

The code is exactly what the production has right now.

So technically there is no change associated but it fails.

I am doing this because initally
//create Payable Invoice
        c2g__codaPurchaseInvoice__c PI = new c2g__codaPurchaseInvoice__c();
        PI.c2g__Account__c = MVI.Account__c;
        //PI.ThisWilWork__c = MVI.id;
I was trying to make PI.ThisWilWork thing work but now I have it commented out to see if the original was even properly working.

I know of test start method and test stop method but I am not quite sure where I would put them.

Note that I have not written the test codes and the class and I am only trying to add a line to a class in order to make something work.

It will be appreciated if you could tell me what I am doing wrong.

Thank You
I wrote a class 
public with sharing class SlackOpportunityPush {
    
    
    // To publish Opportunity values in form of a String//
    public static final String slackURL = 'https://hooks.slack.com/services/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';
    
    public class Oppty {
        @InvocableVariable(label='Opportunity Name')
        public String opptyName;
        
        @InvocableVariable(label='Owner')
        public String Owner;
        
        @InvocableVariable(label='Opportunity Amount')
        public String Amount;
        
        @InvocableVariable(label='Account Development Owner')
        public String AccountDE;
        
        @InvocableVariable(label='Account')
        public String Account;
    }
     
    @InvocableMethod(label='Push to Slack')
    
    public static void postToSlack(List<Oppty> oppties) { 
        Oppty o = oppties[0]; // If bulk, only post first to avoid overloading Slack channel
        Map<String,Object> msg = new Map<String,Object>();
        msg.put('text',  '\n Opportunity Won !  Congrats to Opportunity Owner !  :-  ' +o.Owner +   '\n and Account Development Owner:-  '+ o.AccountDE + '\n who just closed Opportunity:-  ' +o.opptyName +   '\n associated to Account:- ' + o.Account +'\n for Opportunity Amount:- $ ' + o.Amount);
        
        System.debug('Called msg.put');
        System.debug('OpportunityOwner '+o.Owner);
        System.debug('OpportunityAccount '+o.Account);
        System.debug('OpportunityAmount '+o.Amount);
        
        
        msg.put('mrkdwn',true);
        String body = JSON.serialize(msg);    
        System.enqueueJob(new QueueableSlackCall(slackURL,'POST',body));
    
    
    
    }
     
    public class QueueableSlackCall implements System.Queueable, Database.AllowsCallouts {
         
        public final String url;
        public final String method;
        public final String body;
         
        public QueueableSlackCall(String url,String method,String body) {
            this.url = url;
            this.method = method;
            this.body = body;
        }
         
        public void execute(System.QueueableContext ctx) {
            HttpRequest req = new HttpRequest();
            req.setEndpoint(url);
            req.setMethod(method);
            req.setBody(body);
            Http http = new Http();
            HttpResponse res = http.send(req);
        }
 
    }
    
}
Test class 
@isTest
private class SlackOpportunityTest {
    

static testMethod void testpostToSlack() {
SlackOpportunityPush.Oppty opt = new SlackOpportunityPush.Oppty();
opt.opptyName = 'Unit Test Opt';
opt.Owner = 'Unit Test Owner';


List<SlackOpportunityPush.oppty> lis = new List<SlackOpportunityPush.oppty>();
lis.add(opt);
SlackOpportunityPush.postToSlack(lis);

System.assertEquals(lis, lis); 
    
}
     
}

Code coverage is now 100% but while deploying the classes I get error:

​Methods defined as TestMethod do not support Web service callouts 
Stack Trace: null