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
Terri T JilesTerri T Jiles 

Data Integration Specialist: Challenge 5: Why Is This Not Working?

Hi All,

I get the following error when attempting to check in this challenge.  However, if I test my code from workbench, it works correctly.  I've checked my spelling, order of parameters in the method signature.

Challenge Not yet complete... here's what's wrong: 
The 'ProjectRESTService' Apex REST service does not appear to be working properly. Calling the service either didn't update the opportunity correctly, return the string 'OK', create an associated project correctly or function correctly in general.

I've checked other forum post and I noticed people are searching for the project record prior to upserting it.  I don't understand why that is needed if I can upsert directly against the ProjectRef__c external id field.  I could try to copy and paste what they did to pass the challenge, but I want to understand why what I coded doesn't work.  A second pair (or more) eyes would be appreciated.  Thank you!
 
@RestResource(urlMapping='/project/*')
global with sharing class ProjectRESTService {
    @HttpPost
    global static String postProjectData(String ProjectRef, String ProjectName, String OpportunityId, Date StartDate, Date EndDate, Double Amount, String Status) {
		SavePoint sp = Database.setSavepoint();
        
        try {
	        Project__c p = new Project__c(ProjectRef__c=ProjectRef, Name=ProjectName, Opportunity__c=OpportunityId, Start_Date__c=StartDate, End_Date__c=EndDate, Billable_Amount__c=Amount, Status__c=Status);
    	    upsert p ProjectRef__c;
            Opportunity o = new Opportunity(Id=OpportunityId, DeliveryInstallationStatus__c='In Progress');
            update o;
        	return 'OK';
        } catch (Exception e) {
            System.debug('~~~ Cause: ' + e.getCause() +  ' Message: ' + e.getMessage() + ' getLineNumber: ' + e.getLineNumber() + ' StackTrace: ' + e.getStackTraceString() + ' Type: ' + e.getTypeName());
            Database.rollback(sp);
            return e.getMessage();
        }     
    }
        
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post i hope that will help you
1) https://developer.salesforce.com/forums/?id=9060G000000I526QAC
2) https://developer.salesforce.com/forums/?id=9060G000000I56rQAC
3)
 
@RestResource(urlMapping='/project/*')
global with sharing class ProjectRESTService {
  @HttpPost
  global static String postProjectData(String ProjectRef, String ProjectName, String OpportunityId, Date StartDate, Date EndDate, Double Amount, String Status) {
      List<Project__c> projects = [SELECT Billable_Amount__c,End_Date__c,Id,Name,Opportunity__c,ProjectRef__c,Start_Date__c,Status__c FROM Project__c WHERE ProjectRef__c = :ProjectRef];
      Project__c project = new Project__c();
      if(projects.size() > 0)
          project = projects.get(0);
      
      Savepoint sp = Database.setSavepoint();
      try{
          project.Billable_Amount__c = Amount;
          project.End_Date__c = EndDate;
          project.Name = ProjectName;
          project.Opportunity__c = OpportunityId;
          project.ProjectRef__c = ProjectRef;
          project.Start_Date__c = StartDate;
          project.Status__c = Status;
          upsert project;
          
          //Update Opportunity
          Opportunity opportunity = [SELECT Id, DeliveryInstallationStatus__c FROM Opportunity WHERE Id = :OpportunityId];
          opportunity.DeliveryInstallationStatus__c = 'In progress';
          update opportunity;
          
          return 'OK';
      }
      catch(exception ex){
          Database.rollback(sp);
          return ex.getMessage();
      }
   }
}


 
Terri T JilesTerri T Jiles
I solved this by putting the opportunity record and project record into separate list and performing DML on the list.
Rahul Mukherji 26Rahul Mukherji 26
@RestResource(urlMapping='/project/*')
global class ProjectRESTService {
    //Implement service logic here
    
    @HttpPost
    global static String postProjectData(String ProjectRef,String ProjectName,String OpportunityId,Date StartDate,Date EndDate,Double Amount,String Status){
       Savepoint sp1 = Database.setSavepoint();
        Project__c project = new Project__c(
           Name = ProjectName,
           ProjectRef__c = ProjectRef,
           Opportunity__c = OpportunityId,
           Start_Date__c = StartDate,
           End_Date__c =EndDate,
           Billable_Amount__c =Amount,
           Status__c =Status
       ); 
        
        try{
            Database.upsert(new List<Project__c> {project},Project__c.ProjectRef__c , true);
           Opportunity opp =[SELECT ID,    DeliveryInstallationStatus__c FROM Opportunity WHERE ID =:OpportunityId LIMIT 1];
            if(opp !=NULL){
                opp.DeliveryInstallationStatus__c ='In progress';
                Database.update(opp, true);
            }
            return 'OK';
        }
        catch(Exception ex){
            System.debug('error at line:: '+ex.getLineNumber());
            System.debug('error message: '+ex.getMessage());
            Database.rollback(sp1);
            return ex.getMessage();
        }
        
       
        
    }
}

This would do the job
santosh konathalasantosh konathala
Hi Can anyone share the full code same issue i am facing .Below is my error:
Challenge Not yet complete... here's what's wrong:
The 'ProjectRESTService' Apex REST service does not appear to be working properly. Calling the service either didn't update the opportunity correctly, return the string 'OK', create an associated project correctly or function correctly in general.And below is my code.

@RestResource(urlMapping = '/project/*')
global with sharing class ProjectRESTService {
    @HttpPost
    global static String postProjectData(String ProjectRef, String ProjectName, String OpportunityId,
                                       Date StartDate, Date EndDate, Double Amount, String Status){
        String retMsg = 'Error';
                
        SavePoint sp1 = Database.setSavePoint();
        try{
            List<Opportunity> lstOfOpps = new List<Opportunity>();
            
            if(OpportunityId != null && OpportunityId.trim().length() > 0){
                Opportunity opp = [SELECT Id, DeliveryInstallationStatus__c, Discount_Percent__c FROM Opportunity WHERE Id = :OpportunityId];
                opp.DeliveryInstallationStatus__c = 'In progress';
                                
                lstOfOpps.add(opp);
            }
            UPDATE lstOfOpps;
            
            List<Project__c> lstOfRrjts = new List<Project__c>();
            
            Project__c prjt = new Project__c();
            prjt.ProjectRef__c = ProjectRef;
            prjt.Name = ProjectName;
            prjt.Opportunity__c = OpportunityId;
            prjt.Start_Date__c = StartDate;
            prjt.End_Date__c = EndDate;
            prjt.Billable_Amount__c = Amount;
            prjt.Status__c = Status;
            
            lstOfRrjts.add(prjt);
            
            UPSERT lstOfRrjts;
            
            retMsg = 'OK';
        }catch(Exception ex){
            Database.rollback(sp1);
            retMsg = ex.getMessage();
        }
        return retMsg;
    }
}