Showing posts with label Real time Scenarios. Show all posts
Showing posts with label Real time Scenarios. Show all posts

Saturday, 15 July 2023

Duplicate id in list

 

Error 'System.ListException: Duplicate id in list' in Apex :

list can hold duplicate values, but if you try to add duplicate sObject IDs in the list and then update the list you'll get the error. 

Duplicate id in list' in Apex.

List should not hold multiple instances of the same object.

Suppose you are trying to update Account record 

id aid = '0010o000030nWBe';

list <account> al = new list <account>();

for(account a : [select id from account where id ='0010o000030nWBe']){
account acc = new account(id = aid);
    al.add(a);
    al.add(acc);
}
update al;

You will receive error '


To resolve this, we need to create a map and then update

id aid = '0010o000030nWBe';
list <account> al = new list <account>();

for(account a : [select id from account where id ='0010o000030nWBe']){
account acc = new account(id = aid);
    al.add(a);
    al.add(acc);
}
//update al;
map<id,account> accmap = new map<id,account>();

//put all the values from the list to map. 
accmap.putall(al);
if(accmap.size()>0){
update accmap.values();
}

Saturday, 28 January 2023

Holiday Consideration in salesforce

 

Recently, I came across one requirement, where we need to consider holiday.

Suppose, we have one custom object, which has SubmittedDate field, DueDate field.

We have to complete all cases before DueDate, but suppose, if there is any company holidays, we get less time to complete Cases.

Sol:

First we have create Business Hours. To create Business hours, you can search in quick find box “business hours”. On Satuday and Sunday, we don’t have working hours. So , we have metioned “no hours”.



Then we have to create Holidays and associate with business hours.




Suppose, we are submitting the case on 27th January 2023, the due date should be 03rd February 2023, without considering Holidays. It should be 5 business days mainly, excluding holidays and weekends.

With Holiday consideration, the duedate will be 06th February. As, 30th January( Test1), i have considered holiday.




addGmt(businessHoursId, startDate, intervalMilliseconds) :

Adds an interval of milliseconds from a start Datetime traversing business hours only. Returns the result Datetime in GMT.

Duplicate id in list

  Error 'System.ListException: Duplicate id in list' in Apex : list  can hold  duplicate values, but if you try to add duplicate  sO...