+ Start a Discussion
Marry SteinMarry Stein 

apex update does not work

Hello guys, 
pretty simple, but i can not figure out, where the error comes from.  
 
public class UpdateBillingAdress {
    public static void retrieveAdress (Opportunity opp){
        Account billAcc = [
        Select 
        ShippingStreet,
        ShippingCity, 
        ShippingPostalCode,
        ShippingCountry
        FROM Account
        WHERE Id =: opp.umsatz_durch_Account__c
        ];

        Account shipAcc = [
            Select 
            BillingStreet,
            BillingCity, 
            BillingPostalCode,
            BillingCountry
            FROM Account
            WHERE Id =: opp.AccountId
        ];

        Boolean condition = (
            !String.isBlank(billAcc.ShippingStreet) &&
            !String.isBlank(billAcc.ShippingPostalCode) &&
            !String.isBlank(billAcc.ShippingCity) &&
            !String.isBlank(billAcc.ShippingCountry)
        );

        if(condition){
            shipAcc.BillingStreet = billAcc.ShippingStreet;
            shipAcc.BillingPostalCode = billAcc.ShippingPostalCode;
            shipAcc.BillingCity = billAcc.ShippingCity;
            shipAcc.BillingCountry = billAcc.ShippingCountry;
            shipAcc.alternative_Rechnungsanschrift__c = true;
            update shipAcc;
        }
    }
}
 
@isTest
public class TestUpdateBillingAdress {
    @isTest static void testUpdate(){
        Account billAcc =  DataFactory.createAccount();
        Account shipAcc =  DataFactory.createAccount();
        insert billAcc;
        insert shipAcc;
        Opportunity opp = DataFactory.createOpportunity('40 - Converted/Demo agreed', shipAcc.Id);
        opp.umsatz_durch_Account__c = billAcc.Id;
        insert opp;

        shipAcc.BillingStreet = ' ';
        shipAcc.BillingCity = ' ';
        shipAcc.BillingPostalCode = ' ';
        shipAcc.BillingCountry = ' ';
        shipAcc.alternative_Rechnungsanschrift__c = true;
        update shipAcc;

        Boolean condition = (
            !String.isBlank(billAcc.ShippingStreet) &&
            !String.isBlank(billAcc.ShippingPostalCode) &&
            !String.isBlank(billAcc.ShippingCity) &&
            !String.isBlank(billAcc.ShippingCountry)
        );
        System.debug('condition' + condition);
        UpdateBillingAdress.retrieveAdress(opp);
        System.assertEquals(shipAcc.BillingStreet, billAcc.ShippingStreet); // this should be true
    }
}

I would appreciate your help !

Thank you, 
Marry
Best Answer chosen by Marry Stein
Marry SteinMarry Stein
The problem was the Id at line 10. I had to create a variable like this for the qury 
Id billId = opp.umsatz_durch_Account__c;

 

All Answers

SwethaSwetha (Salesforce Developers) 
HI Marry,
What is the error message you are seeing?
Thanks
Marry SteinMarry Stein
System.assertEquals(shipAcc.BillingStreet, billAcc.ShippingStreet); 

// this should be true, but the first value is still null
Line 27 

Thank you swetha
 
MoonpieMoonpie
Hi Marry,

Your function checks for the condition that ALL of your shipping fields are NOT blank.  It appears that if even ONE of them is blank, then the condition is false.  If the condition is false, then your shipping address info is not copied into your billing address info.

So in your test, are you 100% sure that ALL of your shipping fields are NOT blank?  We can't see what your data factory returns, so we don't know whether your default account has shipping address information in every field.
David Zhu 🔥David Zhu 🔥
I assume UpdateBillingAddress is trigger handler. You will have to retrieve shipAcc and billAcc after UpdateBillingAdress.retrieveAdress(opp);

UpdateBillingAdress.retrieveAdress(opp);
shipAcc = [select BillingStreet from Account where id =:shipAcc.id];
billAcc= [select BillingStreet from Account where id =:billAcc.id];  //you may not need this line.

 System.assertEquals(shipAcc.BillingStreet, billAcc.ShippingStreet); // this should be true
SwethaSwetha (Salesforce Developers) 
HI Marry,
Were you able to fix the issue based on above inputs? If so, please mark the helpful answer as best to close this thread. I am happy to look further if you need assistance.Thanks
SSP AdminSSP Admin
Hi Marry,
It looks like our team of experts can help you resolve this ticket. We have Salesforce global help-desk support and you can log a case and our Customer Success Agents will help you solve this issue. You can also speak to them on live chat. Click on the below link to contact our help-desk. Trust me it is a support service that we are offering for free! 

https://jbshelpdesk.secure.force.com

Thanks,
Jarvis SFDC team
Marry SteinMarry Stein
The problem was the Id at line 10. I had to create a variable like this for the qury 
Id billId = opp.umsatz_durch_Account__c;

 
This was selected as the best answer