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
abhi patilabhi patil 

Illegal assignment from Integer to date?

  public void newinsert(string oppName, date newd){
        
        Opportunity opp = new opportunity();
        
        opp.Name = oppName;
        opp.Amount= 344556;
        opp.CloseDate= newd;
        
        insert opp;
RangaRayapatiRangaRayapati
Hi,

In anonymous window execute the code below.

=====================================
public void newinsert(string oppName, date newd){
    
    Opportunity opp = new opportunity();
    
    opp.Name = oppName;
    opp.Amount= 344556;
    opp.CloseDate= newd;
    opp.StageName = 'Prospecting';
    insert opp;
    system.debug('opp id' + opp.id);
    
}

newinsert('Test1', System.today());
======================================
SwethaSwetha (Salesforce Developers) 
HI Abhi,

The error "Illegal assignment from Integer to date" occurs because you are trying to assign an Integer value to a Date field.

In your code snippet, the newd parameter is of type date, which represents a specific date. However, when assigning the value to the CloseDate field of the Opportunity object, it seems that you are passing an Integer value instead of a valid date.

To fix this issue, make sure that the newd parameter is passed as a valid Date object.
public void newinsert(String oppName, Date newd) {
    Opportunity opp = new Opportunity();
    opp.Name = oppName;
    opp.Amount = 344556;
    opp.CloseDate = newd;
    insert opp;
}

Ensure that when calling the newinsert method, you provide a valid Date value for the newd parameter.

example:
Date closingDate = Date.newInstance(2023, 5, 31);
newinsert('New Opportunity', closingDate);

If this information helps, please mark the answer as best. Thank you
Eden WheelerEden Wheeler
It appears that there is an error in the code you provided. The error message suggests that there is an illegal assignment from an Integer to a Date data type. This means that you are trying to assign an Integer value to a variable that is expecting a Date value.

To fix this issue, you need to make sure that the variable newd is of the correct data type, which should be Date. Here's the corrected code:
public void newInsert(String oppName, Date newd) {
    Opportunity opp = new Opportunity();
    
    opp.Name = oppName;
    opp.Amount = 344556;
    opp.CloseDate = newd;
    
    insert opp;
}

In this corrected code, I assumed that Opportunity is a custom class with properties such as Name, Amount, and CloseDate. Also, make sure that you have imported the java.util.Date class at the top of your file.

I hope this will help you.and also check this: az 300 training (https://www.igmguru.com/cloud-computing/microsoft-azure-solution-architect-az-300-training/)
Arun Kumar 1141Arun Kumar 1141

Hi abhi patil,

Please go through the below modified code:

 

public void newinsert(string oppName, Date newd){
    Opportunity opp = new Opportunity();
    
    opp.Name = oppName;
    opp.Amount = 344556;
    opp.CloseDate = Date.newInstance(newd.year(), newd.month(), newd.day());
    
    insert opp;
}
 

Hope it helps.

Thanks! 

kate angilakate angila
I used this code on my webpage (https://thetravellino.com/faisal-movers-sukkur/). It helped me alot and solved my most crucial problem on the webpage.