function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jasril Dane CaliwagJasril Dane Caliwag 

hello everyone, i need to create a test class of my class. please help me out

here is my class: 
public class VendorCreationController implements Database.AllowsCallouts, Queueable{
    public String retURL {get; set;}
    public Boolean canProceed {get; set;}
    public String errorMessage {get; set;}
    public Case caseInfo {get; set;}
    public String recordStatus {get; set;}
    
    private String accString;
    private String caseString;

    public VendorCreationController(ApexPages.StandardController controller){
        caseInfo = (Case)controller.getRecord();
    }  

    public PageReference changeStatus() {
        //Check and get case information
        try{
            caseInfo = [SELECT Id, Status, Sub_Status__c, Supplier_Product_Article__c, SAP_Sync_Status__c FROM Case WHERE Id =: caseInfo.Id];
        }catch(Exception e){
            caseInfo = null;
        }
        //check if caseInfo is not null
        if(caseInfo != null){
            try{
                //Case Status
                recordStatus = caseInfo.Status;
                
                //Validate case status/case SAP status
                if (caseInfo.SAP_Sync_Status__c == Label.Sync_in_Progress) {                        
                    canProceed = false;
                    errorMessage = Label.Case_is_already_Sync_in_Progress;
                } 
                else if (caseInfo.Status == Label.In_Review) {                        
                    canProceed = false;
                    errorMessage = Label.Case_is_In_Review;
                } 
                else if (caseInfo.SAP_Sync_Status__c == Label.Sync_Complete) {                        
                    canProceed = false;
                    errorMessage = Label.Case_is_already_Sync_Completed;
                }     
                else {       
                    canProceed = true;
                    caseInfo.SAP_Sync_Status__c = Label.Sync_in_Progress;
                    caseInfo.Status = Label.Sync_with_SAP;
                    update caseInfo;                    
                }   
                
                retURL = '/' + caseInfo.Id;
                PageReference redirectPage = new PageReference(retURL);
                redirectPage.setRedirect(true);
                return redirectPage;
            }catch(Exception e){
                canProceed = false;
                errorMessage = e.getMessage();
                retURL = '/' + caseInfo.Id;
                PageReference redirectPage = new PageReference(retURL);
                redirectPage.setRedirect(true);
                return redirectPage;
            }
            
        }
        return null;
    }
    

    //constructor
    public VendorCreationController(String accStr, String caseStr) {
        this.caseString = caseStr;
        this.accString = accStr;
    }
    

    public void execute(QueueableContext context) {
        Account acc = new Account();
        Case caseRec = new Case();
        Case newCaseRec = new Case();
        Contact conRec = new Contact();
        
        //Custom settings for integration
        Integration_Management__c integrationManagement;
        
        //Check if endpoint url is present
        try{
            integrationManagement = Integration_Management__c.getValues(Label.Vendor_Creation);        
        }catch(Exception e){
            integrationManagement = null;
            //log exception to application log utility
            ApplicationLogUtility.logError('VendorCreationController', 'sendToSAP', e, e.getMessage(), '', 0);
            ApplicationLogUtility.commitLog();
        }
        //Check if endpoint is present in the custom settings
        if(integrationManagement !=null){
            try{
                acc = (Account) JSON.deserialize(accString, Account.Class);
                caseRec = (Case) JSON.deserialize(caseString, Case.Class);
            }catch(Exception e){
                acc = null;
                caseRec = null;
            }
            if(acc != null && caseRec !=null){
                try{
                    newCaseRec = [SELECT Id, ContactId, Status, SAP_Sync_Status__c FROM Case WHERE Id=: caseRec.Id];    
                }catch(Exception e){
                    newCaseRec = null;
                }
                
                if(newCaseRec !=null){
                    try{
                        conRec = [SELECT Id, Phone, HomePhone, OtherPhone, Fax, Email FROM Contact WHERE Id=: newCaseRec.ContactId];            
                    }catch(Exception e){
                        conRec = null;
                    }
                }
                
                try{
                    system.debug('VendorCreationController caseRec: ' + caseRec);            
                    system.debug('VendorCreationController conRec: ' + conRec);
                    system.debug('VendorCreationController sendToSAP enter');
                    sapComDocumentSapRfcFunctionsVendor.Z_VEN_CREATION_BINDING bob = new sapComDocumentSapRfcFunctionsVendor.Z_VEN_CREATION_BINDING();
                    sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_LFA1 IS_LFA1_FIELDS = new sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_LFA1();
                    sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_LFB1 IS_LFB1_FIELDS = new sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_LFB1();
                    sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_LFM1 IS_LFM1_FIELDS = new sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_LFM1();
                    sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_POSTAL_ADD ADDRESS_FLDS = new sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_POSTAL_ADD();
                    sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_COMM COMM_FIELDS = new sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_COMM();
                    
                    sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_PHONE_TT phone = new sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_PHONE_TT();
                    sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_FAX_TT fax = new sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_FAX_TT();
                    sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_EMAIL_TT email = new sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_EMAIL_TT();
                    
                    sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_PHONE contactPhone = new sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_PHONE();
                    sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_FAX contactFax = new sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_FAX();
                    sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_EMAIL contactEmail = new sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_EMAIL();
                    
                    if(conRec !=null){
                        contactPhone.TELEPHONE =checkIfNull(conRec.Phone, 30);
                        contactPhone.COUNTRY = '';
                        contactPhone.EXTENSION = '';
                        if(contactPhone.TELEPHONE!=''){
                            contactPhone.STD_NO = 'X';
                        }
                        
                        contactFax.COUNTRY = '';
                        contactFax.FAX =checkIfNull(conRec.Fax, 30);
                        if(contactFax.FAX!=''){
                            contactFax.STD_NO = 'X';
                        }
                        
                        contactEmail.E_MAIL =checkIfNull(conRec.Email, 241);
                        if(contactEmail.E_MAIL!=''){
                            contactEmail.STD_NO = 'X';
                        }
                        
                        List<sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_PHONE> phoneList = new List<sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_PHONE>(); 
                        phoneList.add(contactPhone);
                        phone.item = phoneList;
                        
                        List<sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_EMAIL> emailList = new List<sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_EMAIL>(); 
                        emailList.add(contactEmail);
                        email.item = emailList;
                        
                        List<sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_FAX> faxList = new List<sapComDocumentSapRfcFunctionsVendor.ZMAS_VEN_CR_FAX>(); 
                        faxList.add(contactFax);
                        fax.item = faxList;
                        
                        COMM_FIELDS.PHONE = phone;
                        COMM_FIELDS.FAX = fax;
                        COMM_FIELDS.EMAIL = email;
                        system.debug('VendorCreationController sendToSAP COMM_FIELDS: ' + COMM_FIELDS);
                        
                    }
                    
                    IS_LFA1_FIELDS.STCEG=checkIfNull(acc.ABN__c, 20);
                    
                    IS_LFA1_FIELDS.BEGRU=Label.SMKT; //Need confirmation (defaulted)
                    IS_LFA1_FIELDS.KTOKK=Label.SMKT; //Account Group needs to be added (Questionnaire)   
                    
                    IS_LFA1_FIELDS.STCEG= checkIfNull(acc.ABN__c, 20);
                    IS_LFA1_FIELDS.COMM_FIELDS = COMM_FIELDS;
                    ADDRESS_FLDS.NAME= checkIfNull(acc.TradingName__c, 40);
                    ADDRESS_FLDS.CITY=checkIfNull(acc.BillingCity, 40);
                    ADDRESS_FLDS.POSTL_COD1=checkIfNull(acc.BillingPostalCode, 10);
                    ADDRESS_FLDS.STREET=checkIfNull(acc.BillingStreet, 60);
                    //ADDRESS_FLDS.STR_SUPPL1='STREET2'; //Mat to Comeback
                    ADDRESS_FLDS.COUNTRY=checkIfNull(acc.Country__c, 3);
                    ADDRESS_FLDS.LANGU=Label.English; //Constant
                    ADDRESS_FLDS.REGION=checkIfNull(acc.BillingState, 3);
                    ADDRESS_FLDS.SORT1=Label.Tilde_Sign; //Tilde Sign
                    String referenceVendor = '';
                    if(acc.Trading_Term__c != null){
                        system.debug('Trading Term Not Null');	                   
                        Trading_Terms__c tradingTerm = [SELECT Id, Reference_Vendor__c, PaymentMethod__c, PaymentTermsCode__c FROM Trading_Terms__c WHERE Id=: acc.Trading_Term__c];
                        referenceVendor = tradingTerm.Reference_Vendor__c;
                        IS_LFB1_FIELDS.UZAWE = tradingTerm.PaymentMethod__c;
                        IS_LFB1_FIELDS.ZTERM = tradingTerm.PaymentTermsCode__c;
                        referenceVendor = tradingTerm.Reference_Vendor__c;
                        system.debug('Vendor referenceVendor ' + referenceVendor);
                    }
                    
                    ADDRESS_FLDS.SORT2='';
                    IS_LFA1_FIELDS.ADDRESS_FIELDS=ADDRESS_FLDS;
                    
                    //Australia
                    if(acc.Country__c == Label.AU){
                        IS_LFB1_FIELDS.BUKRS=Label.AU_Company_Code;
                    }
                    //New Zealand
                    else if(acc.Country__c == Label.NZ){
                        IS_LFB1_FIELDS.BUKRS= Label.NZ_Company_Code; 
                    }
                    
                    IS_LFM1_FIELDS.EKORG=Label.X1000; 
                    bob.timeout_x = Integer.valueOf(Label.Timeout);
                    String IV_USER_ID = '';
                    try{
                        User curUser = [SELECT Id, ActiveDirectoryId__c FROM User WHERE Id =: UserInfo.getUserId() LIMIT 1];
                        IV_USER_ID = curUser.ActiveDirectoryId__c;                
                    }catch(Exception e){
                        IV_USER_ID = '';
                    }
                    system.debug('VendorCreationController sendToSAP IV_USER_ID: ' + IV_USER_ID);
                    
                    sapComDocumentSapRfcFunctionsVendor.BAPIRET2_T response = new sapComDocumentSapRfcFunctionsVendor.BAPIRET2_T();
                    response = bob.Z_VEN_CREATION(IS_LFA1_FIELDS, IS_LFB1_FIELDS,IS_LFM1_FIELDS,Label.Default_Logic_Key,referenceVendor,IV_USER_ID);
                    //String IV_REF_VENDOR
                    
                    parseResponse(response, acc, caseRec);
                }catch(Exception e){
                    Web_Service_Log__c logs = new Web_Service_Log__c();
                    //Custom Settings
                    logs.Endpoint__c =  integrationManagement.SAP_Endpoint__c;
                    logs.Error_Message__c= Label.ERROR;
                    logs.Error_Message__c += e;
                    logs.Log_Type__c= Label.Case_Vendor_Create_Error;
                    logs.Service_Name__c = Label.Vendor_Webservice_Error;
                    logs.Status_Code__c = CaseRec.id;
                    insert logs;
                    
                    caseRec.Integration_Error_Message__c = e.getMessage();
                    caseRec.SAP_Sync_Status__c = Label.Sync_Error;
                    caseRec.Status = Label.Approved;
                    update caseRec;
                }
            }
        }        
    }
    

    public static String checkIfNull(String str, Integer maxSize){
        if(str == null){
            str = '';
        }
        else{
            if(str.length() > maxSize ){
                str = str.substring(0, maxSize);
            }
        }
        return str;
    }

    public static void parseResponse(sapComDocumentSapRfcFunctionsVendor.BAPIRET2_T response, Account acc, Case caseRec){
        //Get Integration Management Custom Settings
        Integration_Management__c integrationManagement;        
        try{
            //Get Endpoint URL
            integrationManagement = Integration_Management__c.getValues(Label.Vendor_Creation);        
        }catch(Exception e){
            //log exception to application log utility
            ApplicationLogUtility.logError('VendorCreationController', 'parseResponse', e, e.getMessage(), '', 0);
            ApplicationLogUtility.commitLog();
            integrationManagement = null;
        }
        
        //Check if custom settings not null
        if(integrationManagement !=null){
            if(response !=null){
                if(response.item !=null){
                    system.debug('VendorCreationController parseResponse response: ' + response);
                    system.debug('VendorCreationController parseResponse response: ' + response.item);
                    String message = '';
                    Boolean hasError = false;
                    for (sapComDocumentSapRfcFunctionsVendor.BAPIRET2 item : response.item){
                        system.debug('VendorCreationController parseResponse item: ' + item);
                        system.debug('VendorCreationController parseResponse MESSAGE: ' + item.MESSAGE);
                        system.debug('VendorCreationController parseResponse TYPE_X: ' + item.TYPE_x);
                        if(item.TYPE_x == 'E'){
                            hasError = true;
                        }
                        
                        if(item.MESSAGE!=null){
                            message = item.MESSAGE;                    
                        }
                    }
                    
                    if(!hasError){
                        String vendorNumber = message.replace('Vendor Number created ', '');
                        vendorNumber = vendorNumber.replace('.','');
                        //Update Legacy Vendor Number
                        //Update to Sync Completed
                        //Update Account Vendor Number
                        system.debug('VendorCreationController parseResponse vendorNumber: ' + vendorNumber);
                        acc.VendorNumber__c = vendorNumber;
                        update acc;
                        
                        //Case Record Update
                        caseRec.Integration_Error_Message__c = Label.Vendor_Successful;
                        caseRec.SAP_Sync_Status__c = Label.Sync_Complete;
                        caseRec.Status = 'Sync With SAP Completed';
                        update caseRec;
                        //Lock case
                        Approval.UnlockResult unlockCase = Approval.unlock(caseRec, false);
                        if (unlockCase.isSuccess()) {
                            //Operation was successful, so get the ID of the record that was processed
                            System.debug('Successfully unlocked case with ID: ' + unlockCase.getId());
                        }
                        else {
                            //Operation failed, so get all errors                
                            for(Database.Error err : unlockCase.getErrors()) {
                                System.debug('The following error has occurred.');                    
                                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                                System.debug('Case fields that affected this error: ' + err.getFields());
                            }
                        }
                        
                        //Update Web Service Logs
                        Web_Service_Log__c logs = new Web_Service_Log__c();
                        //Custom Settings
                        logs.Endpoint__c =  integrationManagement.SAP_Endpoint__c;
                        logs.Error_Message__c = message;
                        logs.Log_Type__c= Label.Case_Vendor_Create_Successful;
                        logs.Service_Name__c = Label.Vendor_Webservice_Success;
                        logs.Status_Code__c = CaseRec.id;
                        insert logs;
                        
                        system.debug('VendorCreationController parseResponse Account Vendor Number Updated');
                    }
                    else{
                        //Update Web Service Logs
                        Web_Service_Log__c logs = new Web_Service_Log__c();
                        //Custom Settings
                        logs.Endpoint__c =  integrationManagement.SAP_Endpoint__c;
                        logs.Error_Message__c= Label.ERROR;
                        logs.Error_Message__c += message;
                        logs.Log_Type__c= Label.Case_Vendor_Create_Error;
                        logs.Service_Name__c = Label.Vendor_Webservice_Error;
                        logs.Status_Code__c = CaseRec.id;
                        insert logs;
                        
                        //Update case
                        caseRec.Integration_Error_Message__c = message;
                        caseRec.SAP_Sync_Status__c = Label.Sync_Error;
                        caseRec.Status = Label.Approved;
                        update caseRec;
                    }
                }
                else{
                    //No Response
                    caseRec.Integration_Error_Message__c = Label.No_Response_Received_From_The_Webservice;
                    caseRec.SAP_Sync_Status__c = Label.Sync_Error;
                    caseRec.Status = Label.Approved;
                    update caseRec;
                }
            }
            else{
                //No Response
                caseRec.Integration_Error_Message__c = Label.No_Response_Received_From_The_Webservice;
                caseRec.SAP_Sync_Status__c = Label.Sync_Error;
                caseRec.Status = Label.Approved;
                update caseRec;
            }
        }
    }
}

please help me out thank you :) 
Saurabh Sood 12Saurabh Sood 12
hi
you know the flow of all transaction it's better to write by yourself ..
Happy Coding !!