• Melanie VOYMANT
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies
Good morning all,

I can't seem to figure out what's wrong with my code, could you help me please? I would like in my controller to update a field after generating my pdf. Did I do well?

My code below :
Apex Class :
public with sharing class LC_QuoteController {

    @AuraEnabled
    public static Quote generateQuoteDate(Quote quote, Id recordId) {

        Datetime refNow = System.now();

        Quote updatedQuo = new Quote(
            Id = recordId,
            Devis_edite_le__c = refNow
        );

        update updatedQuo;
        return updatedQuo;
    }

    @AuraEnabled
    public static Id genererPDF(Id idDevis, Boolean showPrices, Boolean isEstimation) {
        List<Quote> quotes = [
            SELECT Id, Name, QuoteNumber, Account.Name, Editer_enUS__c, Devis_edite_le__c
            FROM Quote WHERE Id = :idDevis];

        if(quotes.isEmpty()) {
            throw new AuraHandledException('Aucun devis');
        }

        Quote quote = quotes.get(0);

        // Appellation
        String quoteName = quote.QuoteNumber + ' - ' + quote.Account.Name;
        DocumentPageHelper pageHelper = null;
        if (quote.Editer_enUS__c) {
            pageHelper = new DocumentPageHelper(Page.VF03_QuotePDF_enUS);
        } else {
            pageHelper = new DocumentPageHelper(Page.VF02_DevisPDF);
        }


        Map<String, String> mapParams = new Map<String, String> {
            'id' => String.valueOf(idDevis)
        };
        if (!showPrices) {
            mapParams.put('showPrices', 'false');
        }
        if (isEstimation) {
            mapParams.put('esti', 'true');
        }

        return pageHelper.getDocumentFromPage(mapParams, quoteName, idDevis);
    }
}
Aura component JS :
({
    handleRecordUpdated: function (component, event, helper) {
        var eventParams = event.getParams();
        if (eventParams.changeType === 'LOADED') {
            component.set('v.showSpinner', false);
            var visiteEff = component.get('v.simpleRecord.Date_visite_F_operationnelle__c');
            component.set('v.isEstimation', $A.util.isUndefinedOrNull(visiteEff));
        } else if (eventParams.changeType === 'CHANGED') {
        } else if (eventParams.changeType === 'REMOVED') {
        } else if (eventParams.changeType === 'ERROR') {
            component.set('v.showSpinner', false);
            component.set('v.hasError', true);
            component.set('v.errorCode', 'Erreur Lightning Data Service');
            component.set('v.errorDescription', component.get('v.recordError'));
        }
    },
    confirm: function (component, event, helper) {
        component.set('v.confirmed', true);
        component.set('v.showSpinner', true);
        var action = component.get('c.generateQuoteDate');
        var recordId = component.get('v.recordId');
        action.setParams({
            idDevis: recordId
        });
        helper
            .invokeApexMethod(action)
            .then(function (retour) {
                action = component.get('c.genererPDF');
                var recordId = component.get('v.recordId');
                var showPrices = component.get('v.showPrices');
                var isEstimation = component.get('v.isEstimation');
                action.setParams({
                    idDevis: recordId,
                    showPrices: showPrices,
                    isEstimation: isEstimation
                });
                helper.invokeApexMethod(action).then(function (retour) {
                    component.set('v.showSpinner', false);
                    component.set('v.hasError', false);
                    var navEvt = $A.get('e.force:navigateToSObject');
                    navEvt.setParams({
                        recordId: retour
                    });
                    navEvt.fire();
                });
            })
            ['catch'](function (errors) {
                component.set('v.showSpinner', false);
                component.set('v.hasError', true);
                if (errors) {
                    console.log('errors', errors);
                    if (!$A.util.isUndefinedOrNull(errors[0].message)) {
                        var msg = errors[0].message.split(';');
                        switch (msg[0]) {
                            case 'Aucun devis':
                                component.set('v.errorCode', 'Aucun devis ne correspond.');
                                component.set(
                                    'v.errorDescription',
                                    "Vérifier que le devis ne vient pas d'être supprimé."
                                );
                                break;
                        }
                    } else if (!$A.util.isUndefinedOrNull(errors[0].pageErrors)) {
                        component.set('v.errorCode', errors[0].pageErrors[0].statusCode);
                        component.set('v.errorDescription', errors[0].pageErrors[0].message);
                    }
                }
            });
    }
});
Thank you guys ! 

 
Hi there, 

I did a test class for my controller but I can't find my error, could you please help me? I'll give you an example of my controller and my test class.

Controller : 

public with sharing class AdvancedAnalyseController {
    @AuraEnabled
    public static void cloneThisAnalyse(Id analyseId, Integer nombreA) {
        String queryAnalyse = 'SELECT Id, ';
        queryAnalyse += buildCustomFieldsQuery(Analyse__c.getSObjectType());
        queryAnalyse = queryAnalyse.removeEnd(', ') + ' FROM Analyse__c WHERE Id = \'' + analyseId + '\'';
        mDebugUntruncated(queryAnalyse);
        List<Analyse__c> aList = Database.query(queryAnalyse);
        if (aList.isEmpty()) {
            AuraHandledException ex = new AuraHandledException('NO_ANALYSE');
            ex.setMessage('NO_ANALYSE');
            throw ex;
        }
        List<Analyse__c> analyseClone = new List<Analyse__c>();
         for (Integer i=0; i<nombreA; i++) {
            Analyse__c newAnalyse = aList.get(0).clone();
            newAnalyse.Clone__c = true;
            analyseClone.add(newAnalyse);
         }
        insert analyseClone;
        
    }
    @TestVisible
    private static String buildAllFieldsQuery(Schema.SObjectType targetSObjectType) {
        Map<String, Schema.SObjectField> fieldMap = targetSObjectType.getDescribe().fields.getMap();
        String query = '';
        for (String key : fieldMap.keySet()) {
            Schema.SObjectField field = fieldMap.get(key);
            Schema.DescribeFieldResult result = field.getDescribe();
            query += result.getName() + ', ';
        }
        return query.removeEnd(', ');
    }
    @TestVisible
    private static String buildCustomFieldsQuery(Schema.SObjectType targetSObjectType) {
        Map<String, Schema.SObjectField> fieldMap = targetSObjectType.getDescribe().fields.getMap();
        String query = '';
        for (String key : fieldMap.keySet()) {
            Schema.SObjectField field = fieldMap.get(key);
            Schema.DescribeFieldResult result = field.getDescribe();
            if (result.isCustom()) {
                query += result.getName() + ', ';
            }
        }
        return query.removeEnd(', ');
    }
    public static void mDebugUntruncated(String sMsg) {
        for (Integer i = 0; i < sMsg.length(); i=i+300) {
            Integer iEffectiveEnd = (i+300 > (sMsg.length()-1) ? sMsg.length()-1 : i+300);
            System.debug(sMsg.substring(i,iEffectiveEnd));
        }
    }
}

Test class : 

@isTest
private class AdvancedAnalyseControllerTest {
    @TestSetup
    static void makeData() {
        Account acc = new Account (Name='Test_Account', Phone='0123456789');
        insert acc;
        PAD.ApexForcedBypass.remove('AP001');
        Agence__c ag = new Agence__c(Code_agence__c = 'IDF',Code_agence_telephone__c = '98254');
        insert ag;
        Bulle__c bulle = new Bulle__c();
        bulle.Name = 'Bulle de test';
        bulle.Date_cr_ation__c = Date.today();
        insert bulle;
        opportunity opp = new Opportunity(Name='Test_Opp', Code_agence__c='IDF', StageName='Prospection', accountid=acc.Id,
                                            CloseDate=Date.today() , Site_Ville__c='Paris');
        opp.Bulle__c = bulle.Id;
        opp.Agence_code__c = ag.Id;
        insert opp;
        Affaire__c affaire1 = new Affaire__c();
        affaire1.Name = 'Affaire test 1';
        affaire1.Bulle__c = bulle.Id;
        affaire1.Agence__c = ag.Id;
        affaire1.Opportunit__c = opp.Id;
        insert affaire1;
        Chantier__c chantier = new Chantier__c();
        chantier.Name = 'Chantier Test';
        chantier.Compte__c = acc.Id;
        chantier.Opportunite__c = opp.Id;
        chantier.Code_agence__c = ag.Id;
        chantier.Bulle_del__c = bulle.Id;
        chantier.Affaire__c = affaire1.Id;
        insert chantier;
        Analyse__c analyse = new Analyse__c();
        analyse.Etat__c = 'Création = bon de commande';
        analyse.Nature__c = 'Mesure environnementale';
        analyse.Chantier__c = chantier.Id;
        insert analyse;
    }
    @isTest
    static void testmDebugisOK() {
        AdvancedAnalyseController.mDebugUntruncated('test');
    }
    @isTest
    static void testbuildFullListisOk() {
        String fieldList = AdvancedAnalyseController.buildAllFieldsQuery(Analyse__c.getSObjectType());
        System.assertNotEquals(null, fieldList);
    }
    @isTest
    static void testbuildCustomListisOk() {
        String fieldList = AdvancedAnalyseController.buildCustomFieldsQuery(Analyse__c.getSObjectType());
        System.assertNotEquals(null, fieldList);
    }
    @isTest
    static void testCloneAnalyseisOk() {
        PAD.ApexForcedBypass.remove('AP001');
        Analyse__c analyse = [SELECT Id, Chantier__c FROM Analyse__c];
        Test.startTest();
        List<Analyse__c> analyseClone = AdvancedAnalyseController.cloneThisAnalyse();
        for (Integer i=0; i<10; i++) {
            Analyse__c newAnalyse = analyse.clone();
            newAnalyse.Clone__c = true;
            analyseClone.add(newAnalyse);
         }
        Test.stopTest();
        List<Analyse__c> analyses = [SELECT Id FROM Analyse__c];
        System.assertEquals(1, analyses.size());
    }
    @isTest
    static void testCloneAnalyseisNOK() {
        PAD.ApexForcedBypass.remove('AP001');
        Analyse__c analyse = [SELECT Id FROM Analyse__c];
        delete analyse;
        Test.startTest();
        try {
        List<Analyse__c> analyseClone = AdvancedAnalyseController.cloneThisAnalyse();
            for (Integer i=0; i>10; i++) {
            Analyse__c newAnalyse = analyse.clone();
            newAnalyse.Clone__c = true;
            analyseClone.add(newAnalyse);
         }
        } catch (AuraHandledException ex) {
            System.assertEquals('NO_ANALYSE', ex.getMessage());
            return;
        }
        Test.stopTest();
        System.assert(false);
    }
}
Hi guys :) 

I need your help please :( 

I have a code coverage at 52 % but i don't see how to have more than 75%... Could you help me please ? Thank you !

My apex class : 

public with sharing class VFC01_Etiquette_Amiante {
        public BSDA__c process;
    public Integer addition;
    public VFC01_Etiquette_Amiante() {
        setPdfContent(ApexPages.currentPage().getParameters().get('id').escapeHtml4());
    }
    public VFC01_Etiquette_Amiante(ApexPages.StandardController controller) {
        setPdfContent(controller.getRecord().id);
    }
    public String getCity() {
        String address = process.Chantier__r.Adresse__c;
        String[] splittedAddress = address.split(',');
        if (splittedAddress.size() > 2) {
        return splittedAddress.get(1);}
    return '';
    }
    public void setPdfContent(String id) {
        List<BSDA__c> processes = [
            SELECT Id, Name, Chantier__r.Adresse__c
            FROM BSDA__c
            WHERE Id = :id
        ];
        if (!processes.isEmpty()) {
            process = processes.get(0);
            addition = 3;
        }
        Apexpages.currentPage().getHeaders().put( 'content-disposition', 'Attachment; filename=PT_' + process.Name + '.pdf');
    }
}

My Test class : 

@isTest
private class VFC01_Etiquette_AmianteTest {
    @TestSetup
    static void makeData() {
        FactureTestUtils.makeData();
    }
    @isTest
    private static void testVF01PageisOk() {
        PAD.ApexForcedBypass.add('AP001');
        chantier__c chantier = [SELECT Id, Name, Agence_codee__c, Adresse__c FROM chantier__c LIMIT 1];
        BSDA__c bsda = new BSDA__c();
        bsda.Chantier__c = chantier.Id;
        bsda.Agence__c = chantier.Agence_codee__c;
        bsda.Retour_client__c = Datetime.now().date();
        insert bsda;

        Test.startTest();
        ApexPages.StandardController sc = new ApexPages.StandardController(bsda);
        VFC01_Etiquette_Amiante vfpvCtrl = new VFC01_Etiquette_Amiante(sc);
        
        PageReference pageRef = Page.VF01_Etiquette_Amiante;
        pageRef.getParameters().put('id', String.valueOf(bsda.Id));
        pageRef.getParameters().put('isFinal', 'true');
        Test.setCurrentPage(pageRef);
        Test.stopTest();
    }
}
Good morning all,

I can't seem to figure out what's wrong with my code, could you help me please? I would like in my controller to update a field after generating my pdf. Did I do well?

My code below :
Apex Class :
public with sharing class LC_QuoteController {

    @AuraEnabled
    public static Quote generateQuoteDate(Quote quote, Id recordId) {

        Datetime refNow = System.now();

        Quote updatedQuo = new Quote(
            Id = recordId,
            Devis_edite_le__c = refNow
        );

        update updatedQuo;
        return updatedQuo;
    }

    @AuraEnabled
    public static Id genererPDF(Id idDevis, Boolean showPrices, Boolean isEstimation) {
        List<Quote> quotes = [
            SELECT Id, Name, QuoteNumber, Account.Name, Editer_enUS__c, Devis_edite_le__c
            FROM Quote WHERE Id = :idDevis];

        if(quotes.isEmpty()) {
            throw new AuraHandledException('Aucun devis');
        }

        Quote quote = quotes.get(0);

        // Appellation
        String quoteName = quote.QuoteNumber + ' - ' + quote.Account.Name;
        DocumentPageHelper pageHelper = null;
        if (quote.Editer_enUS__c) {
            pageHelper = new DocumentPageHelper(Page.VF03_QuotePDF_enUS);
        } else {
            pageHelper = new DocumentPageHelper(Page.VF02_DevisPDF);
        }


        Map<String, String> mapParams = new Map<String, String> {
            'id' => String.valueOf(idDevis)
        };
        if (!showPrices) {
            mapParams.put('showPrices', 'false');
        }
        if (isEstimation) {
            mapParams.put('esti', 'true');
        }

        return pageHelper.getDocumentFromPage(mapParams, quoteName, idDevis);
    }
}
Aura component JS :
({
    handleRecordUpdated: function (component, event, helper) {
        var eventParams = event.getParams();
        if (eventParams.changeType === 'LOADED') {
            component.set('v.showSpinner', false);
            var visiteEff = component.get('v.simpleRecord.Date_visite_F_operationnelle__c');
            component.set('v.isEstimation', $A.util.isUndefinedOrNull(visiteEff));
        } else if (eventParams.changeType === 'CHANGED') {
        } else if (eventParams.changeType === 'REMOVED') {
        } else if (eventParams.changeType === 'ERROR') {
            component.set('v.showSpinner', false);
            component.set('v.hasError', true);
            component.set('v.errorCode', 'Erreur Lightning Data Service');
            component.set('v.errorDescription', component.get('v.recordError'));
        }
    },
    confirm: function (component, event, helper) {
        component.set('v.confirmed', true);
        component.set('v.showSpinner', true);
        var action = component.get('c.generateQuoteDate');
        var recordId = component.get('v.recordId');
        action.setParams({
            idDevis: recordId
        });
        helper
            .invokeApexMethod(action)
            .then(function (retour) {
                action = component.get('c.genererPDF');
                var recordId = component.get('v.recordId');
                var showPrices = component.get('v.showPrices');
                var isEstimation = component.get('v.isEstimation');
                action.setParams({
                    idDevis: recordId,
                    showPrices: showPrices,
                    isEstimation: isEstimation
                });
                helper.invokeApexMethod(action).then(function (retour) {
                    component.set('v.showSpinner', false);
                    component.set('v.hasError', false);
                    var navEvt = $A.get('e.force:navigateToSObject');
                    navEvt.setParams({
                        recordId: retour
                    });
                    navEvt.fire();
                });
            })
            ['catch'](function (errors) {
                component.set('v.showSpinner', false);
                component.set('v.hasError', true);
                if (errors) {
                    console.log('errors', errors);
                    if (!$A.util.isUndefinedOrNull(errors[0].message)) {
                        var msg = errors[0].message.split(';');
                        switch (msg[0]) {
                            case 'Aucun devis':
                                component.set('v.errorCode', 'Aucun devis ne correspond.');
                                component.set(
                                    'v.errorDescription',
                                    "Vérifier que le devis ne vient pas d'être supprimé."
                                );
                                break;
                        }
                    } else if (!$A.util.isUndefinedOrNull(errors[0].pageErrors)) {
                        component.set('v.errorCode', errors[0].pageErrors[0].statusCode);
                        component.set('v.errorDescription', errors[0].pageErrors[0].message);
                    }
                }
            });
    }
});
Thank you guys !