 ShowAll Questionssorted byDate Posted Ryan Greene

# Set a number then add 1 to it

Hello,
I am having difficulty adding a number to a set, then once it is set how do I add 1 to that number?

Code below; I receive error on line 3 of: void add(Decimal) from the type Set<Integer>
and error on line 11 of: void valueOf(Set<Integer>) from the type assign, which is directly related to line 3 not being set
```Set<Integer> assignid = new Set<Integer>();
for(Lead lead : [SELECT AssignIDDateTime__c, Assignment_ID__c FROM Lead WHERE AssignIDDateTime__c != null ORDER BY AssignIDDateTime__c DESC LIMIT 1]){
}

if(l.Status == 'Underwriting'){
if(l.AssignIDDateTime__c == null){
l.Assignment_ID__c = decimal.valueOf(1);
}else{
l.Assignment_ID__c = decimal.valueOf(assignid) + decimal.valueOf(1);
}
}

}```
Thank you! Akhilesh Reddy Baddigam
Hi Ryan Greene this error might be due to Assignment_ID__c field type can you make sure that Assignment_ID__c is of type Integer and check again.
thank you venkat-D Subham Agarwal 19
replace line 3 with the following code

Please choose this as the best answer if it solves the problem! :) Ryan Greene
Thanks @venky409 !

Any thoughts on adding 1 to the value? Now the error just lies on line 11. The error I get now is Illegal assignment from Set to Decimal

To clarify:
Assignment_ID__c is a number field, so Apex seems to take it as a Decimal
when I add that field to the Set<Integer> it gets converted to an Integer

So on line 11 do I need to convert back to a decimal, and then add 1? venkat-D
Yes, you need to convert it back to decimal and add 1 or you can change your set to Decimal as well. Ryan Greene
Thanks @venky409
I tried a few different things like integer.valueOf(assignId) and decimal.valueOf(assignId). Continue to receive erros :(

How can I convert it back to decimal? venkat-D
Ryan i was just trying to solve your exact problem . Can you tell me why you are using set ? or functionality you are trying to achieve? Ryan Greene
Yes, so the AssignID field is actually just a number and can only be 1, 2, or 3. Then I have a process builder that Assigns an Underwriter based on the number Example: 1 would go to Joe, 2 would go to Bob, and 3 would go to Steve. They need to go in order so a random number would not work.

The SOQL finds the last record that was numbered and adds 1 to it, therefore going in order 1 2 3 (I'll add a statement later that if the previous assignid = 3 then make the next one 1).

Now that Im writing this out, do I really need a Set? I guess I would, right?

Heres an updated code. No error on page but I get an error when a record moves to the "Underwriting" stage, something like it needs a decimal, but it's returning an Integer. So, if we can figure out how to convert back to decimal.......
```trigger assign on Lead (before insert, before update) {
Set<Integer> assignid = new Set<Integer>();
for(Lead lead : [SELECT AssignIDDateTime__c, Assignment_ID__c FROM Lead WHERE AssignIDDateTime__c != null ORDER BY AssignIDDateTime__c DESC LIMIT 1]){
}

if(l.Status == 'Underwriting'){
l.Assignment_ID__c = integer.valueOf(assignid);
l.AssignIDDateTime__c = DateTime.now();
}
}
}``` venkat-D
```integer counter = 1;
if(l.status == 'Underwriting'){
if(counter<=3) {
l.assignment_id__c = counter;
}else {
counter = 0;
}
counter++;
}

}

can you try this. What i am doing is using counter i am assigning counter to assignment id. it resets to 1 if its equals 3. let me know how this works.``` venkat-D
If you want to get last records assignment id then rather asigning counter to 1 assign that query value to counter ex : it can be 2 or 3 and it resets once it reaches 3. Ryan Greene
@venkat-D Sorry it took me a while to get back to this thread. Lots of other implementations to focus on!

So I tried the counter, thank you for creating it. I'm receiving the same error with the counter as I was before. "System.TypeException: Invalid integer: common.apex.runtime.impl.SetValue@2:" It seems to know the value is 2 where it says "SetValue@2" but again this field is set as decimal, so does this need to be converted to a decimal? How? I think if it gets converted back correctly it will work! Almost there!
```trigger assign on Lead (before insert, before update) {
Set<Integer> assignid = new Set<Integer>();
for(Lead lead : [SELECT AssignIDDateTime__c, Assignment_ID__c FROM Lead WHERE AssignIDDateTime__c != null ORDER BY AssignIDDateTime__c DESC LIMIT 1]){
}
}
integer counter = integer.valueOf(assignid);
if(l.status == 'Underwriting'){
if(counter<=3) {
l.Assignment_ID__c = counter;
}else {
counter = 0;
}
counter++;
}
}
}``` venkat-D
Try chaging l.Assignment__c = (Decimal) counter; Ryan Greene
:( same error venkat-D
Can you post screenshot of field setup for Assignment_ID__c? Ryan Greene   venkat-D
Try this l.Assignment__c = Decimal.valueof(counter.format());