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
Iqra TechIqra Tech 

i am getting error when i am not selected any field in my query

Error:-
|System.QueryException: unexpected token: 'null'


i am getting this error in myclass when i not selected any field
i need to this is dynamically hoe can i do that 

my query like:-

select ID,P_Edition__c,p_Opprotuny_name__c,P_Opportunity_ID__c,P_Stage__c,P_Banner__c,null from opportunitylineitem where P_Edition__c = 'Adipec 2018'

how can avoid that if any time i skeep that field and run the class did not get this error please help me
Raj VakatiRaj Vakati
GIve me the complete class  i will write it for you for null check
Iqra TechIqra Tech

public class Post_Records {
    
    Public static void Insert_Records()
    {           
      List<Floor_Plan_Mapping__c> LstFlrmapping=new list< Floor_Plan_Mapping__c>();
      LstFlrmapping=[select Id, Edition__c,Object__c,Opportunity__c,Opportunity_Id__c,Stage__c,Banner_Name__c,Stand_no__c,Edition_Event_Name__c  from Floor_Plan_Mapping__c order by createdDate DESC limit 1];
      String query= 'select ID,'+LstFlrmapping[0].Edition__c+','+LstFlrmapping[0].Opportunity__c+','+LstFlrmapping[0].Opportunity_Id__c+','+LstFlrmapping[0].Stage__c+','+LstFlrmapping[0].Banner_Name__c +','+LstFlrmapping[0].Stand_no__c +' from '+LstFlrmapping[0].Object__c+' where '+LstFlrmapping[0].Edition__c+' = \''+LstFlrmapping[0].Edition_Event_Name__c +'\'';

      String Edition=LstFlrmapping[0].Edition__c;
      String Opportunity=LstFlrmapping[0].Opportunity__c;
      String OpprtunityId=LstFlrmapping[0].Opportunity_Id__c;
      String Objects=LstFlrmapping[0].Object__c;
      String stage=LstFlrmapping[0].Stage__c;
      String BannerName=LstFlrmapping[0].Banner_Name__c;
      String StandNo=LstFlrmapping[0].Stand_no__c;
      System.debug('Query formed----> '+query); 

     List<sObject> sc= Database.query(query);
     System.debug('All Records -->'+sc);
       
     List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
     for(sObject  s : sc) 
     {
       OpportunityLineItem o = (OpportunityLineItem)s ;
       OppWrapper oppwrappervar=new OppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);
       oppWrapperVarList.add(oppwrappervar);
     }
        string jsonbody=Json.serialize(oppWrapperVarList);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
            request.setTimeout(20000);
        //request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setEndpoint('http://sfdata.iqratechnology.com/api/data');

        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
     
        request.setBody(jsonbody);
        
       HttpResponse response = http.send(request);
        system.debug('Response body '+ response.getBody());
     if (response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
      } else {
    System.debug(response.getBody());
    }
        
        
    }
    
    public class OppWrapper
    {
        public String EventName {get;set;}
        public String RecordId {get;set;}
        public String RecordName {get;set;}
        public String RecordStage {get;set;}
        public String StandNo {get;set;}
         public String BannerName {get;set;}
        public OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String StandNo,String BannerName)
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            
        }
    }
    
}

 

here is my class sir please chehk

Raj VakatiRaj Vakati
try like this
 
public class Post_Records {
    
    Public static void Insert_Records()
    {           
      List<Floor_Plan_Mapping__c> LstFlrmapping=new list< Floor_Plan_Mapping__c>();
      LstFlrmapping=[select Id, Edition__c,Object__c,Opportunity__c,Opportunity_Id__c,Stage__c,Banner_Name__c,Stand_no__c,Edition_Event_Name__c  from Floor_Plan_Mapping__c order by createdDate DESC limit 1];
	  
	   String query= 'select ID,' ;
	   if(LstFlrmapping[0].Edition__c!=null){
		   query = query+LstFlrmapping[0].Edition__c+',' ; 
	   }
	   if(LstFlrmapping[0].Opportunity__c!=null){
		   query = query+LstFlrmapping[0].Opportunity__c+',' ; 
	   }
	   if(LstFlrmapping[0].Opportunity_Id__c!=null){
		   query = query+LstFlrmapping[0].Opportunity_Id__c+',' ; 
	   }
	   if(LstFlrmapping[0].Stage__c!=null){
		   query = query+LstFlrmapping[0].Stage__c+',' ; 
	   }
	   if(LstFlrmapping[0].Banner_Name__c!=null){
		   query = query+LstFlrmapping[0].Banner_Name__c+',' ; 
	   }
	   if(LstFlrmapping[0].Stand_no__c!=null){
		   query = query+LstFlrmapping[0].Stand_no__c+',' ; 
	   }
	   if(LstFlrmapping[0].Stand_no__c!=null){
		   query = query+LstFlrmapping[0].Stand_no__c+',' ; 
	   }
	   query = query.removeEnd(',');
 
	   query =query+ ' from '+LstFlrmapping[0].Object__c+' where '+LstFlrmapping[0].Edition__c+' = \''+LstFlrmapping[0].Edition_Event_Name__c +'\'';
	  
	  
	  

      String Edition=LstFlrmapping[0].Edition__c;
      String Opportunity=LstFlrmapping[0].Opportunity__c;
      String OpprtunityId=LstFlrmapping[0].Opportunity_Id__c;
      String Objects=LstFlrmapping[0].Object__c;
      String stage=LstFlrmapping[0].Stage__c;
      String BannerName=LstFlrmapping[0].Banner_Name__c;
      String StandNo=LstFlrmapping[0].Stand_no__c;
      System.debug('Query formed----> '+query); 

     List<sObject> sc= Database.query(query);
     System.debug('All Records -->'+sc);
       
     List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
     for(sObject  s : sc) 
     {
       OpportunityLineItem o = (OpportunityLineItem)s ;
       OppWrapper oppwrappervar=new OppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);
       oppWrapperVarList.add(oppwrappervar);
     }
        string jsonbody=Json.serialize(oppWrapperVarList);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
            request.setTimeout(20000);
        //request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setEndpoint('http://sfdata.iqratechnology.com/api/data');

        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
     
        request.setBody(jsonbody);
        
       HttpResponse response = http.send(request);
        system.debug('Response body '+ response.getBody());
     if (response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
      } else {
    System.debug(response.getBody());
    }
        
        
    }
    
    public class OppWrapper
    {
        public String EventName {get;set;}
        public String RecordId {get;set;}
        public String RecordName {get;set;}
        public String RecordStage {get;set;}
        public String StandNo {get;set;}
         public String BannerName {get;set;}
        public OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String StandNo,String BannerName)
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            
        }
    }
    
}

 
Iqra TechIqra Tech
Sir Still getting then error when i run the class
Line: 71, Column: 1
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: OpportunityLineItem.Stand_Number_s__c
Raj VakatiRaj Vakati
You need to include the Stand_Number_s__c in SOQL select clause  .. please check is there any metadata config for ir and add the value in select clause 
Iqra TechIqra Tech
sir i am skeeping that field in my vf page beacuse dont know user might not be selected that fields that why i taking precatutions to skeep that  fields please chek this scrren shotUser-added image

in yellow highlighted fields i skeep to select that field please see this how to avoid if i am not selected any field but still need to post data according to selected fields
Raj VakatiRaj Vakati
Add the Stand_Number_s__c  value into the Standard no field metadata mapping 
 
Iqra TechIqra Tech
Add the Stand_Number_s__c  value into the Standard no field metadata mapping 

sorry sir i didnt get you 
Iqra TechIqra Tech
@Raj Vakati

sir u der?
Iqra TechIqra Tech
@Raj Vakati

sir actually the things need to fields optional if not selected then no imapact just post the records on selected fields only
can it be possible?
Iqra TechIqra Tech
i think the porblem is here 

 List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
050     for(sObject  s : sc)
051     {
052       OpportunityLineItem o = (OpportunityLineItem)s ;
053       OppWrapper oppwrappervar=newOppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);
054       oppWrapperVarList.add(oppwrappervar);
055     }

can you please chehk this how to avoid if any field was not selected  i mean need to do optional 
Waqar Hussain SFWaqar Hussain SF
Hi, please try below code
public class Post_Records {
    
    Public static void Insert_Records()
    {           
      List<Floor_Plan_Mapping__c> LstFlrmapping=new list< Floor_Plan_Mapping__c>();
      LstFlrmapping=[select Id, Edition__c,Object__c,Opportunity__c,Opportunity_Id__c,Stage__c,Banner_Name__c,Stand_no__c,Stand_Number_s__c,Edition_Event_Name__c  from Floor_Plan_Mapping__c order by createdDate DESC limit 1];
	  
	   String query= 'select ID,' ;
	   if(LstFlrmapping[0].Edition__c!=null){
		   query += LstFlrmapping[0].Edition__c+',' ; 
	   }
	   if(LstFlrmapping[0].Opportunity__c!=null){
		   query += LstFlrmapping[0].Opportunity__c+',' ; 
	   }
	   if(LstFlrmapping[0].Opportunity_Id__c!=null){
		   query += LstFlrmapping[0].Opportunity_Id__c+',' ; 
	   }
	   if(LstFlrmapping[0].Stage__c!=null){
		   query += LstFlrmapping[0].Stage__c+',' ; 
	   }
	   if(LstFlrmapping[0].Banner_Name__c!=null){
		   query += LstFlrmapping[0].Banner_Name__c+',' ; 
	   }
	   if(LstFlrmapping[0].Stand_no__c!=null){
		   query += LstFlrmapping[0].Stand_no__c+',' ; 
	   }
	   if(LstFlrmapping[0].Stand_Number_s__c!=null){
		   query += LstFlrmapping[0].Stand_Number_s__c+',' ; 
	   }
	   
	   query = query.removeEnd(',');
 
	   query += ' from '+LstFlrmapping[0].Object__c+' where '+LstFlrmapping[0].Edition__c+' = \''+LstFlrmapping[0].Edition_Event_Name__c +'\'';
	  
	  
	  

      String Edition=LstFlrmapping[0].Edition__c;
      String Opportunity=LstFlrmapping[0].Opportunity__c;
      String OpprtunityId=LstFlrmapping[0].Opportunity_Id__c;
      String Objects=LstFlrmapping[0].Object__c;
      String stage=LstFlrmapping[0].Stage__c;
      String BannerName=LstFlrmapping[0].Banner_Name__c;
      String StandNo=LstFlrmapping[0].Stand_no__c;
      System.debug('Query formed----> '+query); 

     List<sObject> sc= Database.query(query);
     System.debug('All Records -->'+sc);
       
     List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
     for(sObject  s : sc) 
     {
       OpportunityLineItem o = (OpportunityLineItem)s ;
       OppWrapper oppwrappervar=new OppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);
       oppWrapperVarList.add(oppwrappervar);
     }
        string jsonbody=Json.serialize(oppWrapperVarList);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
            request.setTimeout(20000);
        //request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setEndpoint('http://sfdata.iqratechnology.com/api/data');

        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
     
        request.setBody(jsonbody);
        
       HttpResponse response = http.send(request);
        system.debug('Response body '+ response.getBody());
     if (response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
      } else {
    System.debug(response.getBody());
    }
        
        
    }
    
    public class OppWrapper
    {
        public String EventName {get;set;}
        public String RecordId {get;set;}
        public String RecordName {get;set;}
        public String RecordStage {get;set;}
        public String StandNo {get;set;}
         public String BannerName {get;set;}
        public OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String StandNo,String BannerName)
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            
        }
    }
    
}

 
Iqra TechIqra Tech
@Waqar Hussain SF thanks sir for helping me out but i am still getting this error

Error:-
Line: 69, Column: 1
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: OpportunityLineItem.Stand_Number_s__c

like if skeep some field to add then it suold be run with rest selected fields i am i need to keep fields optional not mandatory
Waqar Hussain SFWaqar Hussain SF
Can you send me your class code?
Iqra TechIqra Tech
public class Post_Records {
    
    Public static void Insert_Records()
    {           
      List<Floor_Plan_Mapping__c> LstFlrmapping=new list< Floor_Plan_Mapping__c>();
      LstFlrmapping=[select Id, Edition__c,Object__c,Opportunity__c,Opportunity_Id__c,Stage__c,Banner_Name__c,Stand_no__c,Edition_Event_Name__c  from Floor_Plan_Mapping__c order by createdDate DESC limit 1];
      String query= 'select ID,'+LstFlrmapping[0].Edition__c+','+LstFlrmapping[0].Opportunity__c+','+LstFlrmapping[0].Opportunity_Id__c+','+LstFlrmapping[0].Stage__c+','+LstFlrmapping[0].Banner_Name__c +','+LstFlrmapping[0].Stand_no__c +' from '+LstFlrmapping[0].Object__c+' where '+LstFlrmapping[0].Edition__c+' = \''+LstFlrmapping[0].Edition_Event_Name__c +'\'';

      String Edition=LstFlrmapping[0].Edition__c;
      String Opportunity=LstFlrmapping[0].Opportunity__c;
      String OpprtunityId=LstFlrmapping[0].Opportunity_Id__c;
      String Objects=LstFlrmapping[0].Object__c;
      String stage=LstFlrmapping[0].Stage__c;
      String BannerName=LstFlrmapping[0].Banner_Name__c;
      String StandNo=LstFlrmapping[0].Stand_no__c;
      System.debug('Query formed----> '+query); 

     List<sObject> sc= Database.query(query);
     System.debug('All Records -->'+sc);
       
     List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
     for(sObject  s : sc) 
     {
       OpportunityLineItem o = (OpportunityLineItem)s ;
       OppWrapper oppwrappervar=new OppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);
       oppWrapperVarList.add(oppwrappervar);
     }
        string jsonbody=Json.serialize(oppWrapperVarList);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
            request.setTimeout(20000);
        //request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setEndpoint('http://sfdata.iqratechnology.com/api/data');

        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
     
        request.setBody(jsonbody);
        
       HttpResponse response = http.send(request);
        system.debug('Response body '+ response.getBody());
     if (response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
      } else {
    System.debug(response.getBody());
    }
        
        
    }
    
    public class OppWrapper
    {
        public String EventName {get;set;}
        public String RecordId {get;set;}
        public String RecordName {get;set;}
        public String RecordStage {get;set;}
        public String StandNo {get;set;}
         public String BannerName {get;set;}
        public OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String StandNo,String BannerName)
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            
        }
    }
    
}

here is my class currently it is working fine but when i skeep any fields throghs above error i need to do fields optional i am if i am not selected any field then it will skeep field and run the class on remaining wichever i selected 
Waqar Hussain SFWaqar Hussain SF
Have you tried below code?
 
public class Post_Records {
    
    Public static void Insert_Records()
    {           
      List<Floor_Plan_Mapping__c> LstFlrmapping=new list< Floor_Plan_Mapping__c>();
      LstFlrmapping=[select Id, Edition__c,Object__c,Opportunity__c,Opportunity_Id__c,Stage__c,Banner_Name__c,Stand_no__c,Edition_Event_Name__c  from Floor_Plan_Mapping__c order by createdDate DESC limit 1];
      
	  String query= 'select ID,';
	  if(LstFlrmapping[0].Edition__c != null)
	  query += LstFlrmapping[0].Edition__c+',';
	  
	  else if(LstFlrmapping[0].Opportunity__c != null)
	  query += LstFlrmapping[0].Opportunity__c+',';
	  
	  else if(LstFlrmapping[0].Opportunity_Id__c != null)
	  query += LstFlrmapping[0].Opportunity_Id__c+',';
	  
	  else if(LstFlrmapping[0].Stage__c != null)
	  query += LstFlrmapping[0].Stage__c+',';
	  
	  else if(LstFlrmapping[0].Banner_Name__c != null)
	  query += LstFlrmapping[0].Banner_Name__c +',';
	  
	  else if(LstFlrmapping[0].Stand_no__c != null)
	  query += LstFlrmapping[0].Stand_no__c+',';
	  
	  query = query.removeEnd(',');
	  
	  query += ' from '+LstFlrmapping[0].Object__c+' where '+LstFlrmapping[0].Edition__c+' = \''+LstFlrmapping[0].Edition_Event_Name__c +'\'';

      String Edition=LstFlrmapping[0].Edition__c;
      String Opportunity=LstFlrmapping[0].Opportunity__c;
      String OpprtunityId=LstFlrmapping[0].Opportunity_Id__c;
      String Objects=LstFlrmapping[0].Object__c;
      String stage=LstFlrmapping[0].Stage__c;
      String BannerName=LstFlrmapping[0].Banner_Name__c;
      String StandNo=LstFlrmapping[0].Stand_no__c;
      System.debug('Query formed----> '+query); 

     List<sObject> sc= Database.query(query);
     System.debug('All Records -->'+sc);
       
     List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
     for(sObject  s : sc) 
     {
       OpportunityLineItem o = (OpportunityLineItem)s ;
       OppWrapper oppwrappervar=new OppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);
       oppWrapperVarList.add(oppwrappervar);
     }
        string jsonbody=Json.serialize(oppWrapperVarList);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
            request.setTimeout(20000);
        //request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setEndpoint('http://sfdata.iqratechnology.com/api/data');

        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
     
        request.setBody(jsonbody);
        
       HttpResponse response = http.send(request);
        system.debug('Response body '+ response.getBody());
     if (response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
      } else {
    System.debug(response.getBody());
    }
        
        
    }
    
    public class OppWrapper
    {
        public String EventName {get;set;}
        public String RecordId {get;set;}
        public String RecordName {get;set;}
        public String RecordStage {get;set;}
        public String StandNo {get;set;}
         public String BannerName {get;set;}
        public OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String StandNo,String BannerName)
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            
        }
    }
    
}

 
Iqra TechIqra Tech
Yes i trid this code but i think the actual problem is here 

  for(sObject  s : sc)
45     {
46       OpportunityLineItem o = (OpportunityLineItem)s ;
47       OppWrapper oppwrappervar=newOppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);
48       oppWrapperVarList.add(oppwrappervar);
49     }
Waqar Hussain SFWaqar Hussain SF
What error are you getting with the above code?
Iqra TechIqra Tech
Line: 70, Column: 1
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: OpportunityLineItem.Stand_Number_s__c

when we are querying for add the fields in wrapper class on that time it is shoing error
Waqar Hussain SFWaqar Hussain SF

This means the error is coming through your API. The above class is fine. You will need to update the REST API class. Which is posting data to Salesforce.


http://sfdata.iqratechnology.com/api/data

The class that is starting with something like this
@RestResource(urlMapping='/data/')


 
Iqra TechIqra Tech

i will show you what i am doing in details ....

User-added image
 

Actually i am having this vf page (mapping scrren) i am selecting fields accoridng to whic object i have selected 
i have do all the fields are optional whatever fields i want to select i can selcte it is an optional then we save mappin in Floor Plan Mapping object(custom object) i.e api names of the fields which i have selcted and object name and Editon value it is unders first field 
i.e FM Expo 2017

User-added image

accoridng to this fields api name and Object name and value(Fm Expo 2017) we fecth the records from Opportuniy line item object and post the records in third party API
so the thing is when i not selcte any fields in ,mapping scrren(VF) so it may be optional do not throgh any error skeep that field and POst the records in thirdd party API on the basis of selected fields

please see this

Iqra TechIqra Tech
       OpportunityLineItem o = (OpportunityLineItem)s ;
       OppWrapper oppwrappervar= new           OppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);

can we do this to like this ??

 String query= 'select ID,' ;
       if(LstFlrmapping[0].Edition__c!=null){
           query += LstFlrmapping[0].Edition__c+',' ; 
       }
       if(LstFlrmapping[0].Opportunity__c!=null){
           query += LstFlrmapping[0].Opportunity__c+',' ; 
       }
       if(LstFlrmapping[0].Opportunity_Id__c!=null){
           query += LstFlrmapping[0].Opportunity_Id__c+',' ; 
       }
       if(LstFlrmapping[0].Stage__c!=null){
           query += LstFlrmapping[0].Stage__c+',' ; 
       }
       if(LstFlrmapping[0].Banner_Name__c!=null){
           query += LstFlrmapping[0].Banner_Name__c+',' ; 
       }
       if(LstFlrmapping[0].Stand_no__c!=null){
           query += LstFlrmapping[0].Stand_no__c+',' ; 
       }
      
to avoid if not selected for wrapper class tooo????

     
Waqar Hussain SFWaqar Hussain SF
Ok got it. Please check the API name of Stand Number field on both objects. and try by updating line 8 to 
String query= 'select ID,Stand_Number_s__c,';

Let me know if this solves your problem.
Iqra TechIqra Tech

Stand_no__c is floor plan mapping object field
Stand_Number_s__c is opp line itme object field
 
Iqra TechIqra Tech
actually the problem is from wrapper class now we need to wrapper class also dynamic 

OppWrapper oppwrappervar = newOppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);

this thing i need to dymanic apart from this all are working fine  how can we do this dynamic?
Iqra TechIqra Tech
EXCEPTION_THROWN|[52]|System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: OpportunityLineItem.Stand_Number_s__c
15:26:13.1 (52330382)|HEAP_ALLOCATE|[52]|Bytes:114
15:26:13.1 (52347462)|METHOD_EXIT|[1]|01p7E0000029MAF|Post_API_Class.Insert_Records()
15:26:13.1 (52357905)|SYSTEM_MODE_EXIT|false
15:26:13.1 (52562497)|FATAL_ERROR|System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: OpportunityLineItem.Stand_Number_s__c

Class.Post_API_Class.Insert_Records: line 52, column 1


after debugging i found the error on this 
OppWrapper oppwrappervar = newOppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);

now we must have to this to do dynamic how can we do this to dynamic?
Waqar Hussain SFWaqar Hussain SF
The error is in the class Post_API_Class
on line 52. You will have to update that class

To make that wrapper code dynamic you will have to use dynamic apex to get object and fields name. 
Iqra TechIqra Tech
The error is in the class Post_API_Class
on line 52. You will have to update that class

To make that wrapper code dynamic you will have to use dynamic apex to get object and fields name. 

ok can you please show me how to do that 

line no 52:- newOppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);
Iqra TechIqra Tech
@Waqar Hussain SF sir u der???
 
Waqar Hussain SFWaqar Hussain SF
Can you paste your code here, please paste the Post_API_Class class code
Iqra TechIqra Tech
public class Post_API_Class {
    
    Public static void Insert_Records()
    {           
      List<Floor_Plan_Mapping__c> LstFlrmapping=new list< Floor_Plan_Mapping__c>();
      LstFlrmapping=[select Id, Edition__c,Object__c,Opportunity__c,Opportunity_Id__c,Stage__c,Banner_Name__c,Stand_no__c,Edition_Event_Name__c  from Floor_Plan_Mapping__c order by createdDate DESC limit 1];
      
       String query= 'select ID,' ;
       if(LstFlrmapping[0].Edition__c!=null){
           query += LstFlrmapping[0].Edition__c+',' ; 
       }
       if(LstFlrmapping[0].Opportunity__c!=null){
           query += LstFlrmapping[0].Opportunity__c+',' ; 
       }
       if(LstFlrmapping[0].Opportunity_Id__c!=null){
           query += LstFlrmapping[0].Opportunity_Id__c+',' ; 
       }
       if(LstFlrmapping[0].Stage__c!=null){
           query += LstFlrmapping[0].Stage__c+',' ; 
       }
       if(LstFlrmapping[0].Banner_Name__c!=null){
           query += LstFlrmapping[0].Banner_Name__c+',' ; 
       }
       if(LstFlrmapping[0].Stand_no__c!=null){
           query += LstFlrmapping[0].Stand_no__c+',' ; 
       }
      
       
       query = query.removeEnd(',');
 
       query += ' from '+LstFlrmapping[0].Object__c+' where '+LstFlrmapping[0].Edition__c+' = \''+LstFlrmapping[0].Edition_Event_Name__c +'\'';
      
      
      

      String Edition=LstFlrmapping[0].Edition__c;
      String Opportunity=LstFlrmapping[0].Opportunity__c;
      String OpprtunityId=LstFlrmapping[0].Opportunity_Id__c;
      String Objects=LstFlrmapping[0].Object__c;
      String stage=LstFlrmapping[0].Stage__c;
      String BannerName=LstFlrmapping[0].Banner_Name__c;
      String StandNo=LstFlrmapping[0].Stand_no__c;
      System.debug('Query formed----> '+query); 

     List<sObject> sc= Database.query(query);
     System.debug('All Records -->'+sc);
       
     List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
     for(sObject  s : sc) 
     {
       OpportunityLineItem o = (OpportunityLineItem)s ;
       OppWrapper oppwrappervar=new OppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);
       oppWrapperVarList.add(oppwrappervar);
     }
        string jsonbody=Json.serialize(oppWrapperVarList);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
            request.setTimeout(20000);
        //request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setEndpoint('http://sfdata.iqratechnology.com/api/data');

        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
     
        request.setBody(jsonbody);
        
       HttpResponse response = http.send(request);
        system.debug('Response body '+ response.getBody());
     if (response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
      } else {
    System.debug(response.getBody());
    }
        
        
    }
    
    public class OppWrapper
    {
        public String EventName {get;set;}
        public String RecordId {get;set;}
        public String RecordName {get;set;}
        public String RecordStage {get;set;}
        public String StandNo {get;set;}
         public String BannerName {get;set;}
        public OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String StandNo,String BannerName)
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            
        }
    }
    
}
Iqra TechIqra Tech
and here is my first class 

public class Post_Records {
    
    Public static void Insert_Records()
    {           
      List<Floor_Plan_Mapping__c> LstFlrmapping=new list< Floor_Plan_Mapping__c>();
      LstFlrmapping=[select Id, Edition__c,Object__c,Opportunity__c,Opportunity_Id__c,Stage__c,Banner_Name__c,Stand_no__c,Edition_Event_Name__c  from Floor_Plan_Mapping__c order by createdDate DESC limit 1];
      String query= 'select ID,'+LstFlrmapping[0].Edition__c+','+LstFlrmapping[0].Opportunity__c+','+LstFlrmapping[0].Opportunity_Id__c+','+LstFlrmapping[0].Stage__c+','+LstFlrmapping[0].Banner_Name__c +','+LstFlrmapping[0].Stand_no__c +' from '+LstFlrmapping[0].Object__c+' where '+LstFlrmapping[0].Edition__c+' = \''+LstFlrmapping[0].Edition_Event_Name__c +'\'';

      String Edition=LstFlrmapping[0].Edition__c;
      String Opportunity=LstFlrmapping[0].Opportunity__c;
      String OpprtunityId=LstFlrmapping[0].Opportunity_Id__c;
      String Objects=LstFlrmapping[0].Object__c;
      String stage=LstFlrmapping[0].Stage__c;
      String BannerName=LstFlrmapping[0].Banner_Name__c;
      String StandNo=LstFlrmapping[0].Stand_no__c;
      System.debug('Query formed----> '+query); 

     List<sObject> sc= Database.query(query);
     System.debug('All Records -->'+sc);
       
     List<OppWrapper> oppWrapperVarList = new List<OppWrapper>();
     for(sObject  s : sc) 
     {
       OpportunityLineItem o = (OpportunityLineItem)s ;
       OppWrapper oppwrappervar=new OppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);
       oppWrapperVarList.add(oppwrappervar);
     }
        string jsonbody=Json.serialize(oppWrapperVarList);
        system.debug('Json body---->'+jsonbody);
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
            request.setTimeout(20000);
        //request.setEndpoint('http://fpdatamap.salesforceoutsourcing.in/api/FPDatas');
        request.setEndpoint('http://sfdata.iqratechnology.com/api/data');

        request.setMethod('POST');
        request.setHeader('Content-Type','application/json');
     
        request.setBody(jsonbody);
        
       HttpResponse response = http.send(request);
        system.debug('Response body '+ response.getBody());
     if (response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
      } else {
    System.debug(response.getBody());
    }
        
        
    }
    
    public class OppWrapper
    {
        public String EventName {get;set;}
        public String RecordId {get;set;}
        public String RecordName {get;set;}
        public String RecordStage {get;set;}
        public String StandNo {get;set;}
         public String BannerName {get;set;}
        public OppWrapper(String EventName,String RecordId,String RecordName,String RecordStage,String StandNo,String BannerName)
        {
            this.EventName =EventName;
            this.RecordId =RecordId;
            this.RecordName =RecordName;
            this.RecordStage=RecordStage;
            this.StandNo=StandNo;
            this.BannerName=BannerName;
            
        }
    }
    
}
Waqar Hussain SFWaqar Hussain SF
Can you also send me the debug log of query
System.debug('Query formed----> '+query);

 
Iqra TechIqra Tech
:01.1 (9018248)|HEAP_ALLOCATE|[16]|Bytes:18
16:34:01.1 (9027588)|HEAP_ALLOCATE|[16]|Bytes:181
16:34:01.1 (9076754)|USER_DEBUG|[16]|DEBUG|Query formed----> select ID,P_Edition__c,p_Opprotuny_name__c,P_Opportunity_ID__c,P_Stage__c,P_Banner__c,Stand_Number_s__c from opportunitylineitem where P_Edition__c = 'Adipec 2018'
Waqar Hussain SFWaqar Hussain SF
Have you tried by updating the below line of code?
String query= 'select ID,Stand_Number_s__c,';

 
Iqra TechIqra Tech
Look when i selected all the fields 
    OppWrapper oppwrappervar=new OppWrapper(o.P_Edition__c,o.P_Opportunity_ID__c,o.p_Opprotuny_name__c,o.P_Stage__c,o.Stand_Number_s__c,o.P_Banner__c);

then there is no error  but if skeep any field in that time thoghs n error
suppose not selected fields is stand no on that  time throgs error 

Line: 70, Column: 1
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: OpportunityLineItem.Stand_Number_s__c
 
Raj VakatiRaj Vakati
in Opportunity line item  SOQL query add all the fields 

Select id , Stand_Number_s__c ,OtherFields from OpportunityLineItem
Amit Awasthi 7Amit Awasthi 7

contactTriggerAccountRelation: execution of BeforeInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Phone Trigger.contactTriggerAccountRelation: line 5, column 1
Rakesh Kumar 335Rakesh Kumar 335
Error:  SObject row was retrieved via SOQL without querying the requested field:
Can you help me with proper syntax, please? Seems like the field needs to be in the query.

Current code:
 public static void AddOrderNumber(Map<Id, Order__c> oldMap,list<Order__c> issListIn,boolean isnew,boolean isODSvccall) {
       // List<Order__c> issListIn = [select id,Order_Number__c, from Order__c where id in :Ordermap.keyset()];
*The Query is in the commented line. Trying to save it, but unable to do so