+ Start a Discussion
Michael MMichael M 
Hello, I am trying to query by Discharge__c (custom object) record with the most recent discharge_date__c. 

This is the query I am using:

    Discharge__c dis = [select id, name_dob_formula__c, date_of_birth__c, discharge_date__c from discharge__c where Name_DOB_formula__c =: namePlusDob order by discharge_date__c desc limit 1];    

However it does not seem to be working. It is querying old discharge__c records with later discharge_date__c's  Is there something I should change in the query?
 
Best Answer chosen by Michael M
VinayVinay (Salesforce Developers) 
Hi Michael,

Do you see any errors or no results?

Can you try removing limit and desc.  'Name_DOB_formula__c' is used to calculate dates or what it does?

Thanks,
Monishan MMonishan M 
Hi All,

I am receiving above error in below code
global class RetrieveMedicalInquiryStatusBatchMVN implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful, Schedulable {

    String query = 'SELECT Id,Account_vod__c, Status_vod__c, MA_Response_AZ__c ' +
                    // Query Closing
                    'FROM Medical_Inquiry_vod__c ' +
                    'WHERE Sent_to_GMIP_MVN__c = TRUE ' +
                    'AND Id IN :Ids';

    List <String> statuses;
    List<String> Ids = new List<String>('a0tU000000784Gbdue' , 'a0tU00000090WERThc') ;

Can you please help me in sorting this out

Thanks,
Monisha
Best Answer chosen by Monishan M
RituSharmaRituSharma
Replace below line:
List<String> Ids = new List<String>('a0tU000000784Gbdue' , 'a0tU00000090WERThc') ;

With:
List<String> Ids = new List<String>{'a0tU000000784Gbdue' , 'a0tU00000090WERThc'} ;
Aman wadhwa 18Aman wadhwa 18 
Hi, 
I have a before update trigger on an account number field which increments the number field by 1 every time a record is updated or created new.
Also, a process builder on the same field and doing the same increments on the same field by one so when I put value "1" in that field it updates it to 4 so does that means trigger executes again after process builder updates that field 
Thanks in advance 
Best Answer chosen by Aman wadhwa 18
Andrew GAndrew G
Short version - yes. 

Longer version
1 is placed in the field.  
Save is pressed so the before trigger fires and updates the field to 2.
Your Process builder fires and updates the field to 3.  Since this is a DML, it fires the before trigger again.  From the reference Danish supplied:

When a process or flow executes a DML operation, the affected record goes through the save procedure.

So, the Trigger then updates to 4.

This one of the reason why when mixing Apex with PB and Flows, that you need to consider one option or the other , or when to disable the recursion of triggers by using booleans in the triggers.

regards
Andrew
Akshay Vasu 1Akshay Vasu 1 
I have created a wrapper class to create an Object and send it as a request to a third party system. It was working well. But after I added a two new arguments of the Datatype Date, I am getting the below error.

    Constructor not defined: [SFDC_DataObject.CustomerAccountObject].<Constructor>(Id, String, Id, String, Id, String, Integer, NULL, String, String, Id, String, NULL, String, String, String, String)

The request that I am creating and sending is as below.

    SFDC_DataObject.CustomerAccountObject cusAccObj = new SFDC_DataObject.CustomerAccountObject(o.AccountId, o.Customer_Name__c, o.Agency_Name__r.Id,o.Agency_Name_OB__c, o.Opportunity.OwnerId, o.Opportunity.Owner.FederationIdentifier, PrimarySalesSplitPercent, null, secSOSalesforceId.get(o.OpportunityId), secSOSalesforceEmail.get(o.OpportunityId), o.Opportunity.Customer_Success_Manage__r.Id, o.Opportunity.Customer_Success_Manage__r.FederationIdentifier, null, o.Billing_Email__c, o.Billing_Phone__c, o.Bill_To_Name__c, o.Billing_Notes__c);

My wrapper class for the same object is as below.

    public class CustomerAccountObject {
            public String  sfCustomerId;
            public String  customerName;
            public String  sfAgencyId;
            public String  agencyName;
            public String  sfPrimarySalesOwnerId;
            public String  primarySalesOwnerEmail;
            public Integer primarySalesOwnerPercentage;
            public Date    primarySalesOwnerEffectiveFrom;
            public String  sfSecondarySalesOwnerId;
            public String  secondarySalesOwnerEmail;
            public Date    secondarySalesOwnerEffectiveFrom;
            public String  sfAccountManagerId;
            public String  accountManagerEmail;
            public String  billingEmail;
            public String  billingPhone;
            public String  billingName;
            public String  billingNotes;
    
            public CustomerAccountObject() {}
    
            public CustomerAccountObject(String sfCustomerId, String customerName, String sfAgencyId, String agencyName, String sfPrimarySalesOwnerId, String primarySalesOwnerEmail, Integer primarySalesOwnerPercentage, Date primarySalesOwnerEffectiveFrom, String sfSecondarySalesOwnerId, String secondarySalesOwnerEmail, Date secondarySalesOwnerEffectiveFrom, String sfAccountManagerId, String accountManagerEmail, String billingEmail, String billingPhone, String billingName, String billingNotes) {
                this.sfCustomerId                     = sfCustomerId;
                this.customerName                     = customerName;
                this.sfAgencyId                       = sfAgencyId;
                this.agencyName                       = agencyName;
                this.sfPrimarySalesOwnerId            = sfPrimarySalesOwnerId;
                this.primarySalesOwnerEmail           = primarySalesOwnerEmail;
                this.primarySalesOwnerPercentage      = primarySalesOwnerPercentage;
                this.primarySalesOwnerEffectiveFrom   = primarySalesOwnerEffectiveFrom;
                this.sfSecondarySalesOwnerId          = sfSecondarySalesOwnerId;
                this.secondarySalesOwnerEmail         = secondarySalesOwnerEmail;
                this.secondarySalesOwnerEffectiveFrom = secondarySalesOwnerEffectiveFrom;
                this.sfAccountManagerId               = sfAccountManagerId;
                this.accountManagerEmail              = accountManagerEmail;
                this.billingEmail                     = billingEmail;
                this.billingPhone                     = billingPhone;
                this.billingName                      = billingName;
                this.billingNotes                     = billingNotes;
            }
        }

I began getting the error after I added the null for the Date arguments I.e primarySalesOwnerEffectiveFrom and **secondarySalesOwnerEffectiveFrom** during the Object creation.

Can anyone please let me know what am I doing wrong here.
Best Answer chosen by Akshay Vasu 1
chanchal_:)chanchal_:)
you should always pass paratmeters in order only. 

you are not passing null to date. 
'secondarySalesOwnerEffectiveFrom'  is your last 7th parameter and you are passing null to last 5th parameter. 





SFDC_DataObject.CustomerAccountObject cusAccObj = new SFDC_DataObject.CustomerAccountObject(o.AccountId, o.Customer_Name__c, o.Agency_Name__r.Id,o.Agency_Name_OB__c, o.Opportunity.OwnerId, o.Opportunity.Owner.FederationIdentifier, PrimarySalesSplitPercent, null, secSOSalesforceId.get(o.OpportunityId), secSOSalesforceEmail.get(o.OpportunityId), null, o.Opportunity.Customer_Success_Manage__r.Id, o.Opportunity.Customer_Success_Manage__r.FederationIdentifier, o.Billing_Email__c, o.Billing_Phone__c, o.Bill_To_Name__c, o.Billing_Notes__c);


 
iliyas bagwaniliyas bagwan 
I want to find specific contact using on the basis of email or phone number
Best Answer chosen by iliyas bagwan
RituSharmaRituSharma
Your query should be like below:
Select Id,Name,Email,Phone from Contact where Email='abc@test.com' or Phone='1231333'
Vinay Temburnikar 7Vinay Temburnikar 7 

Need a query that basically says

(ExposeCase = true OR CreatedByID = UserInfo.getUserId()) AND isClosed = false

which would be in the following syntax and sort of a combination of these:

soql += ' Where CreatedById = \'' + UserInfo.getUserId() + '\' OR Expose_Case__c = true';

soql += ' Where Expose_Case__c = true AND isClosed = false';

I am unable to figure out the syntax. I've tried the following 

soql += ' Where CreatedById = \'' + UserInfo.getUserId() + '\' OR Expose_Case__c = true' + 'AND isClosed = false';

Best Answer chosen by Vinay Temburnikar 7
Abdul KhatriAbdul Khatri
Hi Vinay,

Using Dynamic SOQL
soql = 'SELECT Id FROM .... '; 
soql += ' WHERE (CreatedById = \'' + UserInfo.getUserId() + '\'' OR Expose_Case__c = true)'; 
soql += ' AND isClosed = false';

Using Static SOQL
[SELECT Id FROM .... WHERE (CreatedById = :UserInfo.getUserId() OR Expose_Case__c = true) AND isClosed = false];

I hope this helps.
 
Ann BeattieAnn Beattie 
User cannot change record owner If user is not System Admin, Industry is null and Account created after 1/1/2017
I tried this formula where the Profile Id is for System Administrator  but it fires even if the System Admin is editing the record.
$User.ProfileId  <> "0ei0000001DLN6"&&
 ISCHANGED( OwnerId )&&
 ISBLANK(TEXT(Industry ))&&
  DATEVALUE(CreatedDate) > DATE(2017,01,01)
 
Best Answer chosen by Ann Beattie
Maharajan CMaharajan C
Hi Ann,

Your formula looks good. But don't use the hardcoded id in formula. May be you are using the wrong id.

I tried the below formula in my org it's working fine. Try the below one.
 
AND(
$Profile.Name <> 'System Administrator',
ISCHANGED( OwnerId ),
ISBLANK(TEXT(Industry )),
DATEVALUE(CreatedDate) > DATE(2017,01,01)
)

Thanks,
Maharajan.C
chaitanya motupalli 13chaitanya motupalli 13 
Hi, i have written trigger on opportunity while inset or update. this logic compare opp with already existed prodcuts on org. if it maches we are throughing an error message on opp record. but i am getting null pointer excpetion. please any help

trigger helloOppTrg on Opportunity (after insert,after update) {
    
    list<Id>accIdlst          = new list<Id>();
    list<string>oppOrdernolst = new list<string>();
    for (Opportunity objOpp : Trigger.new) {
        oppOrdernolst.add(objOpp.OrderNumber__c);
        accIdlst.add(objOpp.AccountId);
    }
    system.debug('opp order no entering'+oppOrdernolst);
    system.debug('account id entering'+accIdlst);
    list<Opportunity> oppwithaccount = new list<Opportunity>();
    oppwithaccount = [SELECT Id,AccountId,OrderNumber__c,Account.Name FROM Opportunity WHERE AccountId=:accIdlst];
    list<string>accname= new list<string>();
    for(Opportunity acc:oppwithaccount){
        accname.add(acc.Account.Name);
    }
    system.debug('account name'+accname);
    list<Product2> matchProduct = new list<Product2>();
    matchProduct = [SELECT Id, IsActive, ProductCode, Name FROM Product2
                    WHERE ProductCode =:oppOrdernolst AND Name =:accname];
    system.debug('product records'+matchProduct);
    
    for(Opportunity objOpp:oppwithaccount) {
        system.debug('opp id top'+objOpp.id);
     
        for(Product2 proloop : matchProduct) {
            system.debug('inside second loop');
            if(proloop.IsActive==false && proloop.ProductCode==objOpp.OrderNumber__c
               && proloop.Name==objOpp.Account.Name){
                   system.debug('inside third loop');
                   system.debug('opp id'+objOpp.id); // Id value is showing properly
                    Opportunity oppfromtriggernewmap = Trigger.newMap.get(objOpp.id); // but here it reurns null value
                    system.debug('just opp before error msg'+oppfromtriggernewmap); // Null
                   oppfromtriggernewmap.OrderNumber__c.addError('check is false so can not update/insert record MACHED');// Null pointer exception 
               }
        }
    }
    
}
Best Answer chosen by chaitanya motupalli 13
chaitanya motupalli 13chaitanya motupalli 13
Hi Maharajan,

Thanks for reply. your thought worked but i i made change in SOQL from "Trigger.newMap" to "Trigger.new" since i am getting error message saything 'IN operator must be used with an iterable expression' So final answer is 
 
trigger helloOppTrg on Opportunity (after insert,after update) {
    
    list<Id>accIdlst          = new list<Id>();
    list<string>oppOrdernolst = new list<string>();
    for (Opportunity objOpp : Trigger.new) {
        oppOrdernolst.add(objOpp.OrderNumber__c);
		if(objOpp.AccountId != null)   // Add the null check
			accIdlst.add(objOpp.AccountId);
    }
    system.debug('opp order no entering'+oppOrdernolst);
    system.debug('account id entering'+accIdlst);
    list<Opportunity> oppwithaccount = new list<Opportunity>();
	oppwithaccount = [SELECT Id,AccountId,OrderNumber__c,Account.Name FROM Opportunity WHERE AccountId=:accIdlst AND Id IN: Trigger.New];    
	list<string>accname= new list<string>();
    for(Opportunity acc:oppwithaccount){
        accname.add(acc.Account.Name);
    }
    system.debug('account name'+accname);
    list<Product2> matchProduct = new list<Product2>();
    matchProduct = [SELECT Id, IsActive, ProductCode, Name FROM Product2
                    WHERE ProductCode =:oppOrdernolst AND Name =:accname];
    system.debug('product records'+matchProduct);
    
    for(Opportunity objOpp:oppwithaccount) {
        system.debug('opp id top'+objOpp.id);
     
        for(Product2 proloop : matchProduct) {
            system.debug('inside second loop');
            if(proloop.IsActive==false && proloop.ProductCode==objOpp.OrderNumber__c
               && proloop.Name==objOpp.Account.Name){
                   system.debug('inside third loop');
                   system.debug('opp id'+objOpp.id); // Id value is showing properly
				    if(Trigger.newMap.ContainsKey(objOpp.id))  // To Avoid the Null Exception use this check
					{
						Opportunity oppfromtriggernewmap = Trigger.newMap.get(objOpp.id); // but here it reurns null value
						system.debug('just opp before error msg'+oppfromtriggernewmap); // Null
						oppfromtriggernewmap.OrderNumber__c.addError('check is false so can not update/insert record MACHED');// Null pointer exception 
					}
               }
        }
    }
    
}

 
Gonzalo Di GesuGonzalo Di Gesu 
Hello. I've been trying, for a few days now, to make an Apex HTTPRequest to the Star Wars API but keep getting error 301.
I looked everywhere for a solution but can't find one.

My class declaration is the following

@AuraEnabled
    public static Object getContactData(integer id){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://swapi.dev/api/people/' + id);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        List<Object> contact = new List<Object>();
    // If the request is successful, parse the JSON response.
    if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'animals' key as a list
     Map<String, Object> person = (Map<String, Object>)results.get('namel');
     contact.add(person);
    }
        return contact;
    }

Keep getting an empty Array. And if I use the debug console, I get the 301 error. I tried a similar code with the heroku animal API and it worked.

Thanks and sorry for the inconvenience, I'm new to Apex
Best Answer chosen by Gonzalo Di Gesu
VinayVinay (Salesforce Developers) 
Can you check if endpoint should be something like below
http swapi.dev/api/planets/1/
https://swapi.dev/documentation

Thanks,
Debjani Banerjee 16Debjani Banerjee 16 
Field Reps create Tasks from Salesforce App and they hit the button twice creating lot of duplicates. is there a way to block duplicate task creration without Trigger.
Best Answer chosen by Debjani Banerjee 16
AbhishekAbhishek (Salesforce Developers) 
Banerjee,

Without a trigger, it's not possible right now.

You can raise an Idea for it, Based on the votes it will be implemented in the future.

For reference check this too,

https://salesforce.stackexchange.com/questions/208106/how-to-avoid-creating-duplicate-task-when-risk-score-is-changes

https://salesforce.stackexchange.com/questions/247004/trigger-to-prevent-duplicate-and-add-add-task-to-existing

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.