如何使用soql

时间:2019-05-29 06:28:33

标签: salesforce apex

我想在两个自定义对象项目和todo对象之间创建查找关系,但不能这样做

这是在两个对象之间创建查找关系的代码。它包含分别用于创建项目和创建任务的第二类方法

public class ProjectAssignment { *//class ProjectAssignment*

     public static void createProject(){ *//method to create an object*
         Project__c pr=new Project__c();
         pr.Name='IseeIt'; //name of project
         pr.Start_Date__c= date.today()`
         pr.Description__c='This Is To Do Kind Project';
         pr.End_Date__c=date.today()+5;
         insert pr;
    }

    public static void createTask(){ *//create todo method
         //Code to fetch data*
        Project__c pr = [Select Id from Project__c]; *//query for lookup relationship between project and todo*
         ToDo__c td = new ToDo__c();
         td.Project__c= pr.Id; *//fetchhing project id*
         td.Start_Date__c=date.today();
         td.Description__c='This is the first task';
         td.End_Date__c=date.today()+2;
         insert td;

    }

}
  

“ System.QueryException:列表具有多于1行以分配给SObject”   因为它们是我在项目对象中创建的两个项目

1 个答案:

答案 0 :(得分:0)

尝试一下。

Project__c pr = [Select Id from Project__c];
    For this, you have to Select the query in where clause and put LIMIt 1 so that they give you only one record.
    Project__c pr = [Select Id from Project__c where name = 'IseeIt' LIMIT 1];